I'm currently running a script which continuously polls the z-way api for changes - as you can imagine this uses a lot of resources and isn't nearly as responsive as an event-driven system would be. So I've been scouring the internet to figure out a way of converting my polling system to an event-driven one, but can't find a decent example for the Z-Way server.
What I'm looking for is just a really simple example of wiring up a motion sensor so that every time it gets triggered it makes a call to an external URI. I've seen similar questions asked frequently on this board but haven't seen it properly answered, so a real working example could go a long way to helping out us newbies. From what I've read this should be possible using a custom automation module and a virtual device, but I'm very hazy on the exact implementation. Please, if you have something similar won't you paste an example so I can use it as a base? I'll update this topic with my findings in a hope that this answers the other queries I've seen as well.
Currently I'm just running repeated queries to a SwitchBinary resource such as "http://raspberrypi:8083/ZWaveAPI/Run/de ... nstances[0]", grabbing the data and then evaluating it. I'd really like to be able to fire off HTTP events whenever the switch status changes.
Please let me know if I can provide any further info - any help would be greatly appreciated :]
Simple event module
Re: Simple event module
Have you already read developer documentation?
I'm omitting the module wrapper details here, but the essence would be like this:
I'm omitting the module wrapper details here, but the essence would be like this:
Code: Select all
zway.devices[12].instances[0].whatever.bind(function(type, arg) {
http.request({
url: "http://whatever.com/...",
method: "POST",
async: true,
data: {
"value": this.value
}
});
});
-
- Posts: 3
- Joined: 08 Sep 2014 01:42
Re: Simple event module
Thanks for your reply, that looks like what I'm after. It's actually the module wrapper details that I'd find useful as I'm struggling to put this code in the right context. I have read realms of developer documentation but a lot of it is out of date, incomplete or has been superseded by a later version of the software. I'll do some more investigation later this evening and will post back my findings here.
Re: Simple event module
Yes, that might be true. The modules are changing, especially before the major 2.0 release.
But you may take an existing module (like DummyDevice or Notification, they're both pretty simple) and write your code based on it.
But you may take an existing module (like DummyDevice or Notification, they're both pretty simple) and write your code based on it.
-
- Posts: 3
- Joined: 08 Sep 2014 01:42
Re: Simple event module
In case anyone else is ever looking for something similar, here's what I ended up with;
/opt/z-way-server/automation/userModules/ApiEventLogger/index.js
/opt/z-way-server/automation/userModules/ApiEventLogger/module.js
Also, I found the following links really useful;
Update your z-way-server software
Static IP address for the raspberrypi
https://www.modmypi.com/blog/tutorial-h ... ip-address
Sample files from a similar question asked on this site
http://forum.zwave.me/viewtopic.php?t=20528&p=51792
How to make a network share from files on your 'pi
http://raspberrywebserver.com/serveradm ... twork.html
Update your rpi firmware, packages, etc
http://stevenhickson.blogspot.co.uk/201 ... kages.html
And for debugging:
I should point out that this is a very rudimentary script which is just meant to transfer data to an external API. I've already seen better ways of doing this, perhaps using virtual devices and I think there should be a way of just emitting node events and trapping them elsewhere, but those are for another post if I figure them out.
/opt/z-way-server/automation/userModules/ApiEventLogger/index.js
Code: Select all
/* constructor(s) */
function ApiEventLogger(id, controller) {
ApiEventLogger.super_.call(this, id, controller);
};
inherits(ApiEventLogger, AutomationModule);
_module = ApiEventLogger;
/* init module */
ApiEventLogger.prototype.init = function (config) {
ApiEventLogger.super_.prototype.init.call(this, config);
console.log("ApiEventLogger loading...");
var devices = {
'sensor1': { 'id': 2, 'type': 'motion', 'region': 'lower', value: null },
'temp1': { 'id': 3, 'type': 'temp', 'temperature': null, value: null },
'sensor2': { 'id': 9, 'type': 'motion', 'region': 'middle', value: null },
'sensor3': { 'id': 10, 'type': 'motion', 'region': 'top', value: null }
};
for (device in devices) {
if (devices[device].type == 'motion') {
console.log("Binding motion device: " + devices[device].id);
zway.devices[devices[device].id].instances[0].commandClasses[48].data[1].level.bind(function () {
if (devices[device].value != this.value) {
devices[device].value = this.value;
if (this.value === true) {
console.log("Motion event triggered [deviceId:" + devices[device].id + "], value:" + this.value);
http.request({
url: "http://remoteserver:3000/api/test",
method: "POST",
async: true,
data: {
"value": this.value,
"device": JSON.stringify(devices[device])
}
});
}
}
});
}
else
{
console.log("Not binding device: " + devices[device].id + " (no binding found)");
}
}
}
//______________________________________________________________________________________________________________________________________________ stop
ApiEventLogger.prototype.stop = function () {
// do nothing - could probably unbind the events
};
Code: Select all
{
"autoload" : true,
"singleton" : true,
"userView" : false,
"defaults" : {
"title" : "API Event logger",
"description" : "Starts up an event loop which logs motion sensors to an external API",
"deviceType" : "switchBinary"
},
"schema" : null,
"options" : null
}
Update your z-way-server software
Code: Select all
wget -q -O - razberry.z-wave.me/install | sudo bash
https://www.modmypi.com/blog/tutorial-h ... ip-address
Sample files from a similar question asked on this site
http://forum.zwave.me/viewtopic.php?t=20528&p=51792
How to make a network share from files on your 'pi
http://raspberrywebserver.com/serveradm ... twork.html
Update your rpi firmware, packages, etc
http://stevenhickson.blogspot.co.uk/201 ... kages.html
And for debugging:
Code: Select all
sudo /etc/init.d/z-way-server restart
sudo tail -f /var/log/z-way-server.log