Binding to events and executing Python/Bash scripts

Discussions about RaZberry - Z-Wave board for Raspberry computer
Post Reply
scorch
Posts: 2
Joined: 08 Nov 2013 23:44

Binding to events and executing Python/Bash scripts

Post by scorch »

Hi,

I have a Vision Security ZD2102EU Door Sensor. Now I want to bind to its SwitchBinary value and call a python oder bash script depending on the current value of SwitchBinary.
Is there any way to get this working with razberry? I've already tried to use the zway.devices[X].instances[0].commandClasses.SwitchBinary.data[1].level.bind() function and then call my script but it won't execute the script, because of security restrictions. I've already read about creating the .syscommands file and tried adding python to it, but it still didn't work.

Right now I'm using a script that request the value but it would be much nicer to react to an event because I don't like having that script running all the time...
krambriw
Posts: 46
Joined: 03 Apr 2013 18:14

I have done it successfully in this way

Post by krambriw »

1) Edit main.js in /opt/z-way-server/automation and add the binding code at the end (my working example below, I have other devices so you have to modify accordingly)2) Edit .syscommands (must be in the same directory) and add the key word 'python'3) Eventually you have to change the properties of your python script to 0755My addings to main.jsvar zone = '192.168.10.255';var port = 33333;zway.devices[2].instances[1].SwitchBinary.data.level.bind(function() { state = 'on'; if (this.value == '0') state = 'off'; eventString = 'Device_2_Instance_1_' + state; try { system( "python /home/pi/Desktop/network_send.py", eventString, this.value, zone, port ); return; } catch(err) { debugPrint("Failed to execute script system call: " + err); }}); zway.devices[2].instances[2].SwitchBinary.data.level.bind(function() { state = 'on'; if (this.value == '0') state = 'off'; eventString = 'Device_2_Instance_2_' + state; try { system( "python /home/pi/Desktop/network_send.py", eventString, this.value, zone, port ); return; } catch(err) { debugPrint("Failed to execute script system call: " + err); }}); zway.devices[3].instances[1].commandClasses.SensorBinary.data[1].level.bind(function() { eventString = 'Device_3_Instance_1'; try { system( "python /home/pi/Desktop/network_send.py", eventString, this.value, zone, port ); return; } catch(err) { debugPrint("Failed to execute script system call: " + err); }});zway.devices[3].instances[2].commandClasses.SensorBinary.data[1].level.bind(function() { eventString = 'Device_3_Instance_2'; try { system( "python /home/pi/Desktop/network_send.py", eventString, this.value, zone, port ); return; } catch(err) { debugPrint("Failed to execute script system call: " + err); }});zway.devices[3].instances[3].commandClasses.SensorMultilevel.data[1].val.bind(function() { eventString = 'Device_3_Instance_3_temperature' try { system( "python /home/pi/Desktop/network_send.py", eventString, this.value, zone, port ); return; } catch(err) { debugPrint("Failed to execute script system call: " + err); }}); My .syscommandspython My sample python script (used to simply broadcast events on my local network)# Call this python script with parameters:# eventString, this.value, zone, port# As an example:# var zone = '192.168.10.255';# var port = 33333;## zway.devices[2].instances[1].SwitchBinary.data.level.bind(function() {# state = 'on';# if (this.value == '0')# state = 'off';# eventString = 'Device_2_Instance_1_' + state; # try {# system(# "python /home/pi/Desktop/network_send.py",# eventString,# this.value,# zone,# port# );# return;# } catch(err) {# debugPrint("Failed to execute script system call: " + err);# }# });# After any changes, restart Z-Way : /etc/init.d/Z-Way restartimport sysimport socketzone = str(sys.argv[3])port = int(sys.argv[4])addr = (zone, port)sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Create socketsock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)sock.settimeout(2.0) eventString = str(sys.argv[1])payloadString = ''try: payloadString = str(sys.argv[2])except: passtry: for i in range(5): sock.sendto(eventString + '&&' + payloadString, addr) time.sleep(2.0) sock.close() sys.exit(0)except: sock.close() sys.exit(0)
scorch
Posts: 2
Joined: 08 Nov 2013 23:44

Thanks for your help and the

Post by scorch »

Thanks for your help and the examples, you helped me alot! Now I have it running like I wanted.
nullpointer17
Posts: 1
Joined: 26 Apr 2017 16:49

Post by nullpointer17 »

Sorry for necro... but if someone needs it... you can do also a more 'compressed' version... like this:

var callback = function(type, ids) {
eventString = "ZWayVDev_zway_" + ids[0] + "-" + ids[1] + "-" + ids[2];
if (ids[3] != 0) {
eventString = eventString + "-" + ids[3];
}
try {
system(
"python3.4 /home/pi/blaine/notifier.py",
"--id", eventString,
"--value", this.value
);
return;
} catch(err) {
debugPrint("Failed to execute script system call: " + err);
}

};

// Motion sensors
zway.devices[31].instances[0].commandClasses[48].data[12].level.bind(callback, [31, 0, 48, 12]);
zway.devices[32].instances[0].commandClasses[48].data[12].level.bind(callback, [32, 0, 48, 12]);
zway.devices[34].instances[0].commandClasses[48].data[12].level.bind(callback, [34, 0, 48, 12]);
zway.devices[35].instances[0].commandClasses[48].data[12].level.bind(callback, [35, 0, 48, 12]);
zway.devices[36].instances[0].commandClasses[48].data[12].level.bind(callback, [36, 0, 48, 12]);
zway.devices[39].instances[0].commandClasses[48].data[12].level.bind(callback, [39, 0, 48, 12]);

// Temperature sensors
zway.devices[31].instances[0].commandClasses[49].data[1].val.bind(callback, [31, 0, 49, 1]);
zway.devices[32].instances[0].commandClasses[49].data[1].val.bind(callback, [32, 0, 49, 1]);
zway.devices[34].instances[0].commandClasses[49].data[1].val.bind(callback, [34, 0, 49, 1]);
zway.devices[35].instances[0].commandClasses[49].data[1].val.bind(callback, [35, 0, 49, 1]);
zway.devices[36].instances[0].commandClasses[49].data[1].val.bind(callback, [36, 0, 49, 1]);
zway.devices[39].instances[0].commandClasses[49].data[1].val.bind(callback, [39, 0, 49, 1]);

// Light sensors
zway.devices[31].instances[0].commandClasses[49].data[3].val.bind(callback, [31, 0, 49, 3]);
zway.devices[32].instances[0].commandClasses[49].data[3].val.bind(callback, [32, 0, 49, 3]);
zway.devices[34].instances[0].commandClasses[49].data[3].val.bind(callback, [34, 0, 49, 3]);
zway.devices[35].instances[0].commandClasses[49].data[3].val.bind(callback, [35, 0, 49, 3]);
zway.devices[36].instances[0].commandClasses[49].data[3].val.bind(callback, [36, 0, 49, 3]);
zway.devices[39].instances[0].commandClasses[49].data[3].val.bind(callback, [39, 0, 49, 3]);

// Heater
zway.devices[33].instances[0].commandClasses[37].data.level.bind(callback, [33, 0, 37, 0]);

// Light
zway.devices[29].instances[2].commandClasses[37].data.level.bind(callback, [29, 2, 37, 0]);
Post Reply