Page 1 of 1
How to cut output of svalues in LUA
Posted: Friday 31 March 2017 15:51
by lassiko
Hi,
Can someone point me a right direction with the problem i have:
I am using a LUA script to send DHT22 data to esp easy with LCD display.
Code: Select all
commandArray = {}
if devicechanged['outtemp'] then
commandArray['OpenURL']='http://192.168.0.124/control?cmd=lcd,1,1,Temp:'..otherdevices_svalues['outtemp']..''
end
return commandArray
As "outtemp" is DHT22, sValues includes humidity as well: 2.5;74.0;0
So on a display i get this: Temp:2.5;74.0;0
I would only like to see temp OR is there a way to display it Temp:2.5c Hum:74.0% This would be even better.
Thank you!
Re: How to cut output of svalues in LUA
Posted: Friday 31 March 2017 16:27
by Toulon7559
Perhaps you first have to separately extract Temp and Humidity from the DHT-set of 3 svalues.
Have myself a simple stepwise script along that line operating on output of a meteo-sensor which provides 4 svalues.
Following is an extract from that lua-script of those segments relevant for you.
In practise I have made 2 scripts, tuned to the designated line for the OLED: one for Temp, one for Humidity
Below you see the version valid/active for Temp, to be tuned for your configuration and application.
Code: Select all
-- Lua-script for info-upload to ESP8266 with OLED SS1306
-- (c)2017 Toulon7559 rev. 01
-- Definition of function(s) and Setting of references
function round(num, dec)
if num == 0 then
return 0
else
local mult = 10^(dec or 0)
return math.floor(num * mult + 0.5) / mult
end
end
baseurl = "http://192.168.x.y/control?cmd=oled"
-- Call Meteo-info from Domoticz-database
Binnen_Temp_RV_Baro = 'WS7000_Temp_RV_Baro'
sTemp, sRV, sComfort, sBaro = otherdevices_svalues[Binnen_Temp_RV_Baro]:match("([^;]+);([^;]+);([^;]+);([^;]+)")
sTemp = round(tonumber(sTemp),1);
print ('Temp = '.. sTemp)
-- sRV = round(tonumber(sRV),1);
-- print ('RV = '.. sRV)
-- sComfort = tonumber(sComfort);
-- print ('Comfort = '.. sComfort)
-- sBaro = tonumber(sBaro);
-- print ('Baro = '.. sBaro)
UploadURL0A = baseurl .. ",3,1,T=".. sTemp .."C"
-- UploadURL0B = baseurl .. ",4,1,V=".. sRV .."%"
-- Perform upload
commandArray = {}
commandArray['OpenURL']= UploadURL0A
-- commandArray['OpenURL']= UploadURL0B
return commandArray
Re: How to cut output of svalues in LUA
Posted: Friday 31 March 2017 16:57
by lassiko
Thanks for your fast reply.
I am very novice with LUA, but trying to modify this to my needs...
Log shows this error:
Error: EventSystem: in temphum_lcd: [string "--..."]:39: attempt to perform arithmetic on local 'num' (a nil value)
Code: Select all
function round(num, dec)
if num == 0 then
return 0
else
local mult = 10^(dec or 0)
return math.floor(num * mult + 0.5) / mult
end
end
baseurl = "http://192.168.0.124/control?cmd=lcd"
-- Call Meteo-info from Domoticz-database
Ulkolampo = 'Ulkolämpötila'
sTemp, sRV, sComfort, sBaro = otherdevices_svalues[Ulkolampo]:match("([^;]+);([^;]+);([^;]+);([^;]+)")
sTemp = round(tonumber(sTemp),1);
print ('Temp = '.. sTemp)
-- sRV = round(tonumber(sRV),1);
-- print ('RV = '.. sRV)
-- sComfort = tonumber(sComfort);
-- print ('Comfort = '.. sComfort)
-- sBaro = tonumber(sBaro);
-- print ('Baro = '.. sBaro)
UploadURL0A = baseurl .. ",1,1,T=".. sTemp .."C"
-- UploadURL0B = baseurl .. ",4,1,V=".. sRV .."%"
-- Perform upload
commandArray = {}
commandArray['OpenURL']= UploadURL0A
-- commandArray['OpenURL']= UploadURL0B
return commandArray
Re: How to cut output of svalues in LUA
Posted: Friday 31 March 2017 17:58
by jvdz
Don't think you get the shown error on this version of the script as there is no line 39.
My guess would be that variable sTemp is nil due to the content of otherdevices_svalues[Ulkolampo] ad the regex not returning any value for it.
So what is the exact content of Ulkolampo?
You could add a print( otherdevices_svalues[Ulkolampo]) statement before doing the round so you can see it in the log.
Jos
Re: How to cut output of svalues in LUA
Posted: Friday 31 March 2017 18:15
by lassiko
jvdz wrote:Don't think you get the shown error on this version of the script as there is no line 39.
There was a commented lines before actual script. Error comes from a line 6:
Code: Select all
return math.floor(num * mult + 0.5) / mult
Log for Ulkolampo:
Re: How to cut output of svalues in LUA
Posted: Friday 31 March 2017 18:39
by jvdz
I see 3 values and you are trying to retrieve 4 values for 4 different variables with this statement:
sTemp, sRV, sComfort, sBaro = otherdevices_svalues[Ulkolampo]:match("([^;]+);([^;]+);([^;]+);([^;]+)")
What do these 3 values represent?
Jos
Re: How to cut output of svalues in LUA
Posted: Friday 31 March 2017 18:49
by Toulon7559
Unintentionally (by accident) probably I have caused some confusion:

the remark 'tune it to your own configuration' is rather lighthearted .......
In my lua-script the set of 4 svalues comes from a Meteo-sensor, not from a DHT22.
Domoticz for a DHT22 will only report 3 svalues.
The script-line to be changed is
Code: Select all
sTemp, sRV, sComfort, sBaro = otherdevices_svalues[Binnen_Temp_RV_Baro]:match("([^;]+);([^;]+);([^;]+);([^;]+)")
Appropriate for a DHT-set of svalues is
Code: Select all
sTemp, sRV, sComfort = otherdevices_svalues[Binnen_Temp_RV_Baro]:match("([^;]+);([^;]+);([^;]+)")
Re: How to cut output of svalues in LUA
Posted: Friday 31 March 2017 18:58
by lassiko
jvdz wrote:I see 3 values and you are trying to retrieve 4 values for 4 different variables with this statement:
sTemp, sRV, sComfort, sBaro = otherdevices_svalues[Ulkolampo]:match("([^;]+);([^;]+);([^;]+);([^;]+)")
That was it. Thank you very much for your help!
Re: How to cut output of svalues in LUA
Posted: Friday 31 March 2017 19:01
by lassiko
Toulon7559 wrote:Unintentionally (by accident) probably I have caused some confusion:

the remark 'tune it to your own configuration' is rather lighthearted .......
In my lua-script the set of 4 svalues comes from a Meteo-sensor, not from a DHT22.
Domoticz for a DHT22 will only report 3 svalues.
The script-line to be changed is
Code: Select all
sTemp, sRV, sComfort, sBaro = otherdevices_svalues[Binnen_Temp_RV_Baro]:match("([^;]+);([^;]+);([^;]+);([^;]+)")
Appropriate for a DHT-set of svalues is
Code: Select all
sTemp, sRV, sComfort = otherdevices_svalues[Binnen_Temp_RV_Baro]:match("([^;]+);([^;]+);([^;]+)")
Indeed! Thank you for this. All working now! Thanks!