Delta temp+hum from devices stored as virtual device?

Moderator: leecollings

Post Reply
landmer
Posts: 6
Joined: Monday 05 October 2015 20:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: Sweden
Contact:

Delta temp+hum from devices stored as virtual device?

Post 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?
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Delta temp+hum from devices stored as virtual device?

Post 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.
landmer
Posts: 6
Joined: Monday 05 October 2015 20:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: Sweden
Contact:

Re: Delta temp+hum from devices stored as virtual device?

Post 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);
}
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Delta temp+hum from devices stored as virtual device?

Post 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&param=switchlight&idx=".$idx."&switchcmd=Toggle");
    else curl(api."json.htm?type=command&param=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&param=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&param=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;}
 
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests