Page 1 of 2
Simple Scripting trigger vdev ID
Posted: 25 Apr 2022 16:35
by gugrim
Hi!
The Simple Scripting app looks promising but I haven't found much documentation. I'd like to write a very simple script that notifies an external REST API whenever one of several devices change state and trigger events.
I've seen that you can list multiple event triggers but how do I know which device caused the script to run?
Example:
Code: Select all
### ZWayVDev_zway_10-0-37
### ZWayVDev_zway_5-0-37
var vdev_id = ?????
var value = vdev(vdev_id).value();
http.request({method: "GET", async: true, url: "http://some-url?id=vdev_id&value="+value});
The above script Is triggered when either "ZWayVDev_zway_10-0-37" or "ZWayVDev_zway_5-0-37" changes state but how do I know which one?
TIA,
Gunnar
Re: Simple Scripting trigger vdev ID
Posted: 25 Apr 2022 20:30
by PoltoS
It is supposed that you just check the state of every device.
Re: Simple Scripting trigger vdev ID
Posted: 25 Apr 2022 20:46
by gugrim
I sure hope there is some way to get the ID of the vdev that caused the event. I have many devices and I want to report all changes for statistics and other purposes. Checking the value of every device still wouldn't tell me which one caused the event. ZWay has just called my script because of some device state change so the device ID should be known. Anyone knows of a way to get hold of it?
Re: Simple Scripting trigger vdev ID
Posted: 25 Apr 2022 21:57
by PoltoS
You can add one more closure here
https://github.com/Z-Wave-Me/home-autom ... dex.js#L68 and add vDevId parameter in the onEvent function (or do before calling it: self.triggeredDeviceID = vDevId)
Re: Simple Scripting trigger vdev ID
Posted: 28 Apr 2022 16:30
by gugrim
Thanks for your reply. Sadly I'm not a JavaScript expert. I tried to make it work with some changes but I must have done some mistake:
I changed the onEvent function to take a trigger:
Code: Select all
this.onEvent = function(trigger) {
The trigger is passed to the script:
Code: Select all
(function(global, self, trigger, vdev, setTimer, stopTimer) {
_script(global, trigger, vdev, setTimer, stopTimer, "on", "off");
})(EasyScripting.globals, undefined, trigger, self.vDevHelper, self.setTimer, self.stopTimer);
In the functions passed to forEach I create a wrapper function that should pass the vDevID to the trigger parameter. I then pass the wrapper to the ..devices.on function. I assume that the on-function will call my wrapper without parameters and that my wrapper would then call the modified onEvent that takes a trigger:
Code: Select all
var onEvent = function() { self.onEvent(vDevID); };
self.controller.devices.on(vDevId, "change:metrics:level", onEvent);
My script is now not invoked at all. Nothing is logged in /var/log/z-way-server.log.
What am I doing wrong?
TIA,
Gunnar
Re: Simple Scripting trigger vdev ID
Posted: 30 Apr 2022 03:18
by PoltoS
The rework requires quite big change and subsequent tests. I can add it to the R&D queue.
But why not to separate it intoultiple identical scripts with different triggers? Sounds like an easy solution
Re: Simple Scripting trigger vdev ID
Posted: 30 Apr 2022 09:58
by gugrim
I can do that as a workaround. Easy but a bit ugly. I have a lot of devices and if I duplicate the script and then want to change it I have to go through all of them. And if I for some reason would like to deactivate the script for a while, I have do deactivate all of them.
Re: Simple Scripting trigger vdev ID
Posted: 30 Apr 2022 19:08
by PoltoS
True... we will think about it
Re: Simple Scripting trigger vdev ID
Posted: 30 Apr 2022 20:34
by gugrim
I finally got it to work. The problem was with my attempt to invoke the script and pass the trigger as a parameter. Now I simply set it on self inside the onEvent function.
The complete diff:
Code: Select all
47c47
< this.onEvent = function() {
---
> this.onEvent = function(trigger) {
54a55
> self.trigger = trigger;
68c69,70
< self.controller.devices.on(vDevId, "change:metrics:level", self.onEvent);
---
> var onEvent = function() { self.onEvent(vDevId); };
> self.controller.devices.on(vDevId, "change:metrics:level", onEvent);
82c84,85
< self.controller.devices.off(vDevId, "change:metrics:level", self.onEvent);
---
> var onEvent = function() { self.onEvent(vDevId); };
> self.controller.devices.off(vDevId, "change:metrics:level", onEvent);
For some reason the script is called now and then even if the device doesn't change state, but I can live with that.
Thanks again for pointing me in the right direction.
/Gunnar
Re: Simple Scripting trigger vdev ID
Posted: 30 Apr 2022 20:55
by gugrim
Also noticed that deactivating the script isn't enough to stop it from being invoked. I have to restart the z-way-server service after deactivation otherwise the deactivation seems to be ignored.