Page 1 of 1
Delta temp+hum from devices stored as virtual device?
Posted: Wednesday 16 November 2016 12:47
by landmer
Im quite new to domoticz. I have two temperature + humidity 433mhz sensors added to Domoticz.
I would like to get the difference between these two as a virutal device.
Adding the virutal device was easy, but now I don't really know how to get the difference stored to it.
Seems its not possible to do "maths" (minus) with blockly so I must use some kind of Lua script?
Re: Delta temp+hum from devices stored as virtual device?
Posted: Wednesday 16 November 2016 15:39
by Egregius
lua, php, or any other language.
In my pass2php script it would look like this:
Code: Select all
function tempdiff(){
global $s,$i;
$dif=$s['temp1']-$s['temp2'];
if($dif!=$s['tempdiff'])ud($i['tempdiff'],$dif);
}
This would update the dummy thermometer called tempdiff with the difference between temp1 and temp2 on each update of one of those 2 thermometers.
Re: Delta temp+hum from devices stored as virtual device?
Posted: Thursday 17 November 2016 12:46
by landmer
Thank you! I tried to understand how to set your script up, is this correct?
1) Make sure i have JSON.lua and ee5_base64.lua in my script folder
2) Install php5 and php5-cli
3) Save script_device_pass2php.lua in domoticz script folder with my own paths
Code: Select all
JSON=loadfile('/volume1/@appstore/domoticz/var/scripts/lua/JSON.lua')()
base64=loadfile('/volume1/@appstore/domoticz/var/scripts/lua/ee5_base64.lua')()
changed=base64.encode(JSON:encode(devicechanged))
devices=base64.encode(JSON:encode(otherdevices))
idx=base64.encode(JSON:encode(otherdevices_idx))
lastupdate=base64.encode(JSON:encode(otherdevices_lastupdate))
os.execute( '/volume1/web/secure/pass2php.php "'..changed..'" "'..devices..'" "'..idx..'" "'..lastupdate..'" &')
commandArray = {}
return commandArray
4) Save your above code as pass2php.php or do I need the whole pass2php.php that is on your web site and add these rows to it?
Code: Select all
function tempdiff(){
global $s,$i;
$dif=$s['temp1']-$s['temp2'];
if($dif!=$s['tempdiff'])ud($i['tempdiff'],$dif);
}
Re: Delta temp+hum from devices stored as virtual device?
Posted: Thursday 17 November 2016 14:15
by Egregius
Almost good
You need everything from the small example.
In your case that should be something like:
Code: Select all
#!/usr/bin/php
<?php error_reporting(E_ALL);ini_set("display_errors","on");date_default_timezone_set('Europe/Brussels');
define('api',"http://127.0.0.1:8084/");
$t=microtime(true);$micro=sprintf("%03d",($t-floor($t))*1000);define('stamp',strftime("%Y-%m-%d %H:%M:%S.", $t).$micro);
$c=json_decode(base64_decode($argv[1]),true);$s=json_decode(base64_decode($argv[2]),true);$i=json_decode(base64_decode($argv[3]),true);$t=json_decode(base64_decode($argv[4]),true);$a=$s[key($c)];$devidx=$i[key($c)];
//Create the array of events. Wich idx calls wich function?
//Fill in the idx's of your 2 thermometers and point them both to the same function
$events=array(123=>'tempdiff',234=>'tempdiff');
if(isset($events[$devidx]))call_user_func($events[$devidx]);
//Start of user functions
function tempdiff(){
global $s,$i;
$dif=$s['temp1']-$s['temp2'];
if($dif!=$s['tempdiff']){
ud($i['tempdiff'],$dif);
print stamp.' New temperature difference = '.$dif.PHP_EOL;
}
}
}
//End of user functions
// ========== FUNCTIONS ==========
/* sw switches $idx on/off. If no action is provided a toggle is made. $info is optional logging */
function sw($idx,$action="",$info=""){
$t=microtime(true);$micro=sprintf("%03d",($t-floor($t))*1000);$stamp=strftime("%Y-%m-%d %H:%M:%S.", $t).$micro;
print $stamp." Switch ".$idx." (".ucfirst($info).") ".strtoupper($action)."
";
if(empty($action)) curl(api."json.htm?type=command¶m=switchlight&idx=".$idx."&switchcmd=Toggle");
else curl(api."json.htm?type=command¶m=switchlight&idx=".$idx."&switchcmd=".$action);
usleep(400000);
}
/* sl sets dimmer $idx to level $level. $info is optional logging */
function sl($idx,$level,$info=""){
$t=microtime(true);$micro=sprintf("%03d",($t-floor($t))*1000);$stamp=strftime("%Y-%m-%d %H:%M:%S.", $t).$micro;
print $stamp." Set Level ".$idx." ".ucfirst($info)." ".$level."
";
curl(api . "json.htm?type=command¶m=switchlight&idx=".$idx."&switchcmd=Set%20Level&level=".$level);
usleep(400000);
}
/* ud updates a device $idx with $nvalue and $svalue. $info is optional logging */
function ud($idx,$nvalue,$svalue,$info=""){
$t=microtime(true);$micro=sprintf("%03d",($t-floor($t))*1000);$stamp=strftime("%Y-%m-%d %H:%M:%S.", $t).$micro;
if(!in_array($idx, array(395,532,534))) print $stamp." --- UPDATE ".$idx." ".$info." ".$nvalue." ".$svalue."
";
curl(api.'json.htm?type=command¶m=udevice&idx='.$idx.'&nvalue='.$nvalue.'&svalue='.$svalue);
usleep(400000);
}
function curl($url){dl("curl.so");$headers=array('Content-Type: application/json',);$ch=curl_init();curl_setopt($ch,CURLOPT_URL,$url);curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);$data=curl_exec($ch);curl_close($ch);return $data;}