Virtual sensor with vaules, received from shell

Discussions about Z-Way software and Z-Wave technology in general
Post Reply
andrusha7778
Posts: 16
Joined: 14 Nov 2018 20:41

Virtual sensor with vaules, received from shell

Post by andrusha7778 »

Hi all!
I have razberry v 3.2.1 with latest raspbian.
I need to create virtual sensor with several values, which can be received by execution shell sripts, which return some json, it looks like

Code: Select all

pi@razberry:~ $ python3 /home/pi/myscript.py 
{"Param1": "Text1", "Param2": "Text2", "Param3": "100", "Param4": "7000"}
Param 1 and 2 are text, params 3 and 4 - integer.
So I understand, that i need some way to execute shell command on razberry and somehow transfer this 4 parameters to my sensor. I can create four virtual devices like sensor multilevel, but how to get data from shell and transfer to sensors values?
andrusha7778
Posts: 16
Joined: 14 Nov 2018 20:41

Re: Virtual sensor with vaules, received from shell

Post by andrusha7778 »

Hi, some updates.
I was search the forum, and have make some things, it works but not like expected.
SO I have added line to /opt/z-way-server/automation/.syscommands file, like

Code: Select all

/home/pi/myscript.sh
then I created DummyDevice_13, and add simple script application with such code:

Code: Select all

### DummyDevice_13

var Myexecstring = system("/home/pi/myscript.sh")[1];
var Ca = JSON.parse(Myexecstring).Ca;
var Vi = JSON.parse(Myexecstring).Vi;
var Vo = JSON.parse(Myexecstring).Vo;
vdev("Code_Device_sensorMultilevel_32").set(Ca);
vdev("Code_Device_sensorMultilevel_33").set(Vo);
vdev("Code_Device_sensorMultilevel_34").set(Vi);
Then I created 3 virtual JS devices with sensor multilevel, so they are:
Code_Device_sensorMultilevel_32, Code_Device_sensorMultilevel_33, Code_Device_sensorMultilevel_34
If I add direct code to Code_Device_sensorMultilevel_32 like this:

Code: Select all

JSON.parse(system("/home/pi/myscript.sh")[1]).Ca
it works as expected, but when I try to do it by DummyDevice switch on or off the data didnt chancge on UI, but in z-way server logs we can see that changes have been applied!

Code: Select all

[2021-11-03 10:23:33.541] [I] [core] ---  DummyDevice_13 performCommand processing: {"0":"off","1":{}}
[2021-11-03 10:23:38.276] [I] [core] ---  Code_Device_sensorMultilevel_32 performCommand processing: {"0":"exact","1":{"level":"76"}}
[2021-11-03 10:23:38.281] [I] [core] ---  Code_Device_sensorMultilevel_33 performCommand processing: {"0":"exact","1":{"level":"52"}}
[2021-11-03 10:23:38.281] [I] [core] ---  Code_Device_sensorMultilevel_34 performCommand processing: {"0":"exact","1":{"level":"G"}}
So why are levels not updates on the UI after .set method?
And I need some sript to be polled every several minutes and then set all the levels to virtual sensors.
micky1500
Posts: 298
Joined: 07 Feb 2016 16:29
Location: England

Re: Virtual sensor with vaules, received from shell

Post by micky1500 »

This might apply !
Post by PoltoS » 19 Mar 2020 00:03
you can change a switch, but not a sensor. Sensor is a read-only feature. switch is an actor and you can modify it's state.

https://forum.z-wave.me/viewtopic.php?f ... set#p86514
Raspi 4 - (Buster - 32 Bit) Zwave Version 4.1.1, Raz 7 Pro, Serial API Version: 07.38
andrusha7778
Posts: 16
Joined: 14 Nov 2018 20:41

Re: Virtual sensor with vaules, received from shell

Post by andrusha7778 »

Hi! Thanks, now i understand.
But now i try to did it another way: i have virtual device (Code_Device_sensorMultilevel_31) which receives JSON from shell, it works as expected, every several minutes. This virtual device is hidden. Is there any way to use JSON.Parse with this virtual device - another virtual device should JSON.Parse this virtual device?
somethink like this

Code: Select all

JSON.Parse(Code_Device_sensorMultilevel_31.value).description
Because for this i receive:

Code: Select all

 [2021-11-03 12:56:54.040] [I] [core] ReferenceError: Code_Device_sensorMultilevel_31 is not defined
If i will try to use vdev like this:

Code: Select all

JSON.parse(vdev("Code_Device_sensorMultilevel_31").value()).description
i receive in log:

Code: Select all

[2021-11-03 13:02:53.344] [I] [core] ReferenceError: vdev is not defined
Update
This script works fine, but it didnt update virtual devices. Virtual devices need to be updated by themselves.

Code: Select all

### DummyDevice_13
var Ca = JSON.parse(vdev("Code_Device_sensorMultilevel_31").value()).Ca;
var Vi = JSON.parse(vdev("Code_Device_sensorMultilevel_31").value()).Vi;
var Vo = JSON.parse(vdev("Code_Device_sensorMultilevel_31").value()).Vo;
vdev("Code_Device_sensorMultilevel_32").set(Ca);
vdev("Code_Device_sensorMultilevel_33").set(Vo);
vdev("Code_Device_sensorMultilevel_34").set(Vi);
User avatar
PoltoS
Posts: 7579
Joined: 26 Jan 2011 19:36

Re: Virtual sensor with vaules, received from shell

Post by PoltoS »

You can do a hack:

Code: Select all

var Myexecstring = system("/home/pi/myscript.sh")[1];
var Ca = JSON.parse(Myexecstring).Ca
vdev("Code_Device_sensorMultilevel_34").dev.set("metrics:level", Ca)
But I sould suggest to use CodeDevice instead - it can execute periodically your script and update the sensor based on it
Post Reply