My approach is to use a small PHP script to extract the variable from a json response, and store the result in a uservariable so it can be used in my Lua scripts.
The PHP script is triggered by an OnAction.
In this example i'm storing the current dimmer-level of a device.
1. First put the following PHP script (update_variable.php) in your Domoticz scripts folder (don't forget to edit the ip and port):
Code: Select all
#!/usr/bin/php
<?php
$json_string = file_get_contents('http://DOMOTICZ_IP:DOMOTICZ_PORT/json.htm?type=devices&rid='.$argv[1]);
$parsed_json = json_decode($json_string, true);
$parsed_json = $parsed_json['result'][0];
$val = $parsed_json[$argv[2]];
file_get_contents('http://DOMOTICZ_IP:DOMOTICZ_PORT/json.htm?type=command¶m=updateuservariable&idx='.$argv[3].'&vname='.$argv[4].'&vtype='.$argv[5].'&vvalue='.$val);
?>
3. Set a OnAction for the device you want to get a variable from, point it to the PHP script, and pass 5 arguments:
Replace the arguments to your needs:script:///PATH_TO_DOMOTICZ/scripts/update_variable.php RID "DEVICE_VARIABLE" IDX_USERVARIABLE "VARIABLE_NAME" VARIABLE_TYPE
RID -> The device ID you want to get the variable of
"DEVICE_VARIABLE" -> The device' variablename you want to store
IDX_USERVARIABLE "VARIABLE_NAME" VARIABLE_TYPE -> The info of the uservariable you created in the second step
Example of how it might look:
Now when you turn on the device or change te dimmer level, the new value of the device is updated to the uservariable.script:///volume5/@appstore/domoticz/var/scripts/update_variable.php 10 "Level" 4 "Light_level" 0
In your Lua scripts, you can now use uservariables['your_variable'].
I hope this is helpful.
Since i'm no expert, I would appreciate feedback on this approach..