PHP script to get device variable for use of Lua

Moderator: leecollings

Post Reply
Robbie
Posts: 29
Joined: Friday 04 September 2015 1:15
Target OS: NAS (Synology & others)
Domoticz version: 2.2980
Contact:

PHP script to get device variable for use of Lua

Post by Robbie »

In some scenarios I want to use device variables which are not passed to Lua. The dimmer level for instance can be very useful.

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&param=updateuservariable&idx='.$argv[3].'&vname='.$argv[4].'&vtype='.$argv[5].'&vvalue='.$val);
?>
2. Create a uservariable to store your value to. Note the variable IDX, variable name, and variable type.

3. Set a OnAction for the device you want to get a variable from, point it to the PHP script, and pass 5 arguments:
script:///PATH_TO_DOMOTICZ/scripts/update_variable.php RID "DEVICE_VARIABLE" IDX_USERVARIABLE "VARIABLE_NAME" VARIABLE_TYPE
Replace the arguments to your needs:
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:
script:///volume5/@appstore/domoticz/var/scripts/update_variable.php 10 "Level" 4 "Light_level" 0
Now when you turn on the device or change te dimmer level, the new value of the device is updated to the uservariable.
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..
Bram81
Posts: 22
Joined: Saturday 02 May 2015 11:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Utrecht, NL
Contact:

Re: PHP script to get device variable for use of Lua

Post by Bram81 »

Hey, thank you very much for this post!! I've been looking for a solution like this for about a week now. The approach works fine, I my case I'm using this to update a uservariable from a virtual sensor which means creating a dummy switch with a specific On Action is an extra step. Today is the first time I've used php so I'm no expert, but the best thing for me would be if the updating of the uservariable could be done from within the script and maybe use crontab to run the php script every couple of hours. In exambles on the web it seems that in order to do that you have to add variables to a json url just like you do through the command line, but I haven't figured out the complete logic yet..

If you know how to approach this, I'd be happy to learn but for now it's working so thanks again!!
Robbie
Posts: 29
Joined: Friday 04 September 2015 1:15
Target OS: NAS (Synology & others)
Domoticz version: 2.2980
Contact:

Re: PHP script to get device variable for use of Lua

Post by Robbie »

You can pass variables through commandline in the exact same way as in the example above.

A cronjob that runs every hour will loook like this:

0 */1 * * * /usr/bin/php -q /PATH_TO_DOMOTICZ/domoticz/var/scripts/update_variable.php 10 "Level" 4 "Light_level" 0
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest