Page 1 of 1
how to retrieve the historic of a temperature sensor
Posted: Sunday 03 January 2016 21:13
by jmleglise
Hi,
Did someone knows how to retrieve the values of the log of temperature in a "device lua script" ?
I'm looking to check on the 20 last statements if the temperature has gone under 0°C. (It's a condition to use a rolling shutter for veranda roof)
I planned to decode Json of
http://127.0.0.1:8081/json.htm?type=gra ... &range=day
But I didn't succeed with this script. (as I have explaned here :
viewtopic.php?f=23&t=9662)
Code: Select all
commandArray={}
if (devicechanged['ManualLaunchScript'] == 'On' and otherdevices['ManualLaunchScript'] == 'On') then
commandArray['ManualLaunchScript']='Off'
local handle=assert(io.popen("curl.exe http://127.0.0.1:8081/json.htm?type=graph&sensor=temp&idx=69&range=day"))
local JMLresponse = handle:read('*all')
handle:close()
print(JMLresponse) --debug
end
return commandArray
Did Someone has a solution ?
Re: how to retrieve the historic of a temperature sensor
Posted: Monday 04 January 2016 16:37
by Toulon7559
The way you formulate your question seems to imply that
- you would like to store the 20 latest-measured temperature values
- check whether at any time the temperature was below 0
- if True, take measures
Is that correct interpretation of your question?
Implies that you have to store 20 values, to refresh them at arrival of a new value, and then have to check all of them whether any value is going below 0.
Perhaps simpler is application of a 'filter'.
In Domoticz then a simple structure required:
- 1 input value called LatestTemp, for which you could take the value from an svalue available in Domoticz under 'Setup/Events'
- 1 uservariable called OldAvgTemp (to be defined under 'Setup/UserVariables')
- 1 variable called NewAvgTemp
Example algoritmic setup
# Initiation:
OldAvgTemp = 5 # to be sure that you start the algorithm 'at distance' from your threshold-Temperature
# Running algorithm, tunable by combination of parameter Filter (with e.g. value between 0.5 and 0.1) and boolean Condition:
While Condition
Call OldAvgTemp
Read LatestTemp
NewAvgTemp = (1-Filter) * OldAvgTemp + Filter * LatestTemp
OldAvgTemp = NewAvgTemp
If (OldAvgTemp < 0) then 'take measures' else 'reverse measures'
Write OldAvgTemp
Advantage is that this algorithm not instantly applies the temperature, but follows the temperature-tendency and has a built-in hysteresis:
with parameter 'Filter' you can control the speed of the filter + the size of the hysteresis.
Be aware that above example text is not a real script, but just a 'symbolic' algorithm which you have to adapt to the type of script you would like to apply!
That makes a lot of difference whether you want a Python-script or a lua-script or ..........
Re: how to retrieve the historic of a temperature sensor
Posted: Monday 04 January 2016 22:05
by jmleglise
Thank you !!! Your method may solve my problem in a way that I had never anticipated BUT ...
Your method is based on a Time script. But I'd rather avoid to execute a script every minute (or the like) to update a calcul when I need this calcul only to open a roller blind (perhaps twice a day). So my first idea was to execute the calcul only when I need it, to save CPU time and because Domoticz already store the historic of the temperature. To be clear here is what I talk about :

- TemperatureLog.JPG (54.29 KiB) Viewed 8443 times
The data are available at this URL in JSON,
http://user:
[email protected]:yourport/json.htm?type=graph&sensor=temp&idx=69&range=day
that I didn't succeed to access, because It seems that Lua can't self-request itself.
So I would like to know if there is a solution to retrieve this JSON in Lua.
Re: how to retrieve the historic of a temperature sensor
Posted: Monday 04 January 2016 22:29
by Egregius
Read the data directly from the database in bash or php and use a cron every x minutes.
Edit: didn't read your last line that it's available thru json. Give me a minute to write a php script.
Re: how to retrieve the historic of a temperature sensor
Posted: Monday 04 January 2016 22:56
by Egregius
here we go, the power of PHP
// = comment
Code: Select all
#!/usr/bin/php
<?php
$domoticzurl='http://127.0.0.1:8080/'; //adjust ip and port
$idxthermometer=22; //idx of thermometer
$idxroof=52; //idx of root
// get the temps
$temps=json_decode(file_get_contents($domoticzurl.'json.htm?type=graph&sensor=temp&idx='.$idxthermometer.'&range=day'),true);
// clean the array to only keep the results
$temps=$temps['result'];
//reverse sort the array so we can break after 20
rsort($temps);
//set a variable to false
$cold = false;
// and a counter
$count = 0;
// Loop thru the received temps
foreach($temps as $temp) {
if($temp['te']<0) $cold = true; // if temp is less than 0 set variable cold to true
$count = $count + 1; // add a count
if($count==20) break; // stop after 20 temps
}
if($cold==true) {
//get current status of roof
$roof=json_decode(file_get_contents($domoticzurl.'json.htm?type=devices&rid='.$idxroof),true);
//if status of roof isn't 'On' switch it 'On' (adjust to closed if nescessary)
if($roof['result'][0]['Status']!='On') Schakel($idxroof, 'On');
}
//just one of the functions of my script to set a switch. Sorry for the Dutch term 'Schakel', PHP doesn't like the 'Switch' name as that is a built-in function.
function Schakel($idx,$cmd) {
global $domoticzurl;
$reply=json_decode(file_get_contents($domoticzurl.'json.htm?type=command¶m=switchlight&idx='.$idx.'&switchcmd='.$cmd),true);
if($reply['status']=='OK') $reply='OK';else {$reply='ERROR';$errors=$errors+1;}
return $reply;
}
Execute by cron: */15 means every 15 minutes.
*/15 * * * * /var/scripts/roof.php
Re: how to retrieve the historic of a temperature sensor
Posted: Tuesday 05 January 2016 0:58
by Minglarn
Are you willing to share your code?
I'm looking for basically the same.i just want to extract the lowest and highest temp in one sensor.
Best regards.
Sent from space using an app.
Re: how to retrieve the historic of a temperature sensor
Posted: Tuesday 05 January 2016 15:17
by Toulon7559
@ jmleglise
If you write
"I'm looking to check on the 20 last statements if the temperature has gone under 0°C"
at my side the question arises, how often do you check?
"20 last statements" implies that periodically a value is added to the list of values, and checking changes in such array 'smells' like a timed run of a script, where you could discuss the method of timing, e.g. (triggered by the entry of a new value) vs (periodic check)?
Therefore the question (just for curiosity): why not a time-script?
Re: how to retrieve the historic of a temperature sensor
Posted: Tuesday 05 January 2016 19:17
by jmleglise
@Egregius Thank you for your answer and your time. I had already well noted in your other posts to the forum that you promote php
For me, as I have invested some times in learning of LUA, I will persevere a bit more until a formal answer that it is impossible by the design of domoticz.
Re: how to retrieve the historic of a temperature sensor
Posted: Tuesday 05 January 2016 20:59
by jmleglise
Hi Toulon7559
I must check only when the roller shutter is about to move. (around only twice per day). That is the reason why I thought of a device_script.
Here is my needs :
1 - My temperature sensor sends temperature every 30sec to domoticz. Domoticz store the historic of the temperature with 1 measure every 5 minutes. This is native with Domoticz.
2 - My roller shutter may be broken if operated when there is ice. So before automating my roller shutter, I have to find a way to prevent it from moving if it freezes or if it froze these last hours.
So , either I use your solution that runs a script every 5 minutes, either I must found a way on device_change to analyse the xx last measures simply stored by domoticz (in LUA !).
Re: how to retrieve the historic of a temperature sensor
Posted: Tuesday 05 January 2016 21:14
by alfred_j_kwak
This might be stupid idea, but how about script_time that check if temperature is below zero. If so then set user variable to 'SOMETHING'. Then in script_device you can check when this user variable is last updated. If it is updated less than hour ago then temperature has been under zero.
Re: how to retrieve the historic of a temperature sensor
Posted: Wednesday 06 January 2016 17:29
by Toulon7559
@jmleglise
Domoticz even runs time-scripts every 1 minute, but

computers do not get tired.
And by insertion in such time-script of an interval-function you can set the interval for checking to any higher value you like!
You can also design the subject interval-function in such way that it checks the clock against a specific set of day-times, e.g. at sunrise and at sunset.
In my opinion, the only other aspect you have to pay attention is whether you allow (between the checks) that a temperature value below 0 is allowed to be missed or not, like hinted in the message of alfred_j_kwak
Re: how to retrieve the historic of a temperature sensor
Posted: Wednesday 06 January 2016 17:41
by alfred_j_kwak
It is also possible to set notification into temperature device itself?
If set notification as http and from Setup->Settings->Notifications->Custom HTTP Action. There you could define URL action for notification - maybe use this to update User Variable and get even better resolution than with script_time.
Re: how to retrieve the historic of a temperature sensor
Posted: Saturday 23 January 2016 16:01
by jmleglise
Thank you very much everyone for all your answers.
If someone is interesting, here is the implementation I made of the advices given to me.
Code: Select all
-- require : Virtual / Type General / Subtype Alert / name : Freeze Alert
function timedifference (s)
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = os.difftime (t1, t2)
return difference
end
commandArray = {}
if (devicechanged['Temp Sensor']) then
local idx = '80' -- Device ID (Type Alert on virtual hardware)
--local sValue = 'Risk : Freeze '
--local nValue = 4 -- 0 grey / 1 green / 2 yellow / 3 orange / 4 red
if tonumber(otherdevices_svalues['Temp Sensor']) <1 then
commandArray['UpdateDevice'] = idx .. '|4|Risk Freeze'
elseif timedifference(otherdevices_lastupdate['Freeze Alert']) > 3*60*60 then -- No freeze since 3 hours
commandArray['UpdateDevice'] = idx .. '|1|No Freeze'
end
end
return commandArray
PS : This resolve my situation, but don't answer my question. I am already interested to know how to access the historic of data that Domoticz store.
Re: how to retrieve the historic of a temperature sensor
Posted: Tuesday 24 January 2017 14:58
by jake
jmleglise wrote:Thank you very much everyone for all your answers.
PS : This resolve my situation, but don't answer my question. I am already interested to know how to access the historic of data that Domoticz store.
Have you succeeded to retrieve historical data? I found this topic in need of the same information. In my case I want to split up the P1 Gas meter in both hot water heating and room heating. I got it working now, by storing the actual gas meter value in a variable. With the next 'devicechanged' I compare the svalue with the variable, do my calculation and afterwards update my new virtual gas meter and the variable again.
Instead of the variable I'd prefer to compare the updated value with the 'second-last' value of the gas meter sensor, but how...
Re: how to retrieve the historic of a temperature sensor
Posted: Friday 16 June 2017 10:56
by McFranky
This code work for me. It calculates the average temperature for the last 7 days.
Code: Select all
local function DeviceUpdate(idx, value1)
local cmd = string.format("%d|0|%.2f", idx, value1)
--print(cmd)
table.insert (commandArray, { ['UpdateDevice'] = cmd } )
end
----------------------------------------------------------------------------------------------------------
-- Script parameters
----------------------------------------------------------------------------------------------------------
Debug = "NO" -- Turn debugging on ("YES") or off ("NO")
----------------------------------------------------------------------------------------------------------
-- CommandArray
----------------------------------------------------------------------------------------------------------
commandArray = {}
-- Variablen deklarieren
local temperatur_name = 'Aussentemperatur_Mittelwert'
local temperatur_idx = otherdevices_idx[temperatur_name]
local messwert_name = 'Aussentemperatur'
local messwert_idx = otherdevices_idx[messwert_name]
--Abfrage der Tages-Tabellen-Werte
local sAbfrage = 'curl "http://192.168.178.29:8080/json.htm?type=graph&sensor=temp&idx='..tostring(messwert_idx)..'&range=day"'
local handle=assert(io.popen(sAbfrage))
local raw = handle:read('*all')
handle:close()
--JSON Auswertung der erhaltenen Daten
json = assert(loadfile '/home/pi/domoticz/scripts/lua/JSON.lua')()
local jsonData = json:decode(raw)
if Debug=='YES' then
print (jsonData.title)
print (jsonData.status)
print (jsonData.result[1].d)
print (jsonData.result[1].te)
end
--Durchschnittswert der letzten 7 Tage ermitteln
local sum = 0
local ave = 0
local elements = #jsonData.result
for i = 1, elements do
sum = sum + jsonData.result[i].te
end
ave = sum / elements
if Debug=='YES' then
print('Mittelwert: '..tostring(ave))
end
--Wert speichern in Zielvariable
DeviceUpdate(temperatur_idx,ave)
return commandArray
Greetings from Berlin