How to cut output of svalues in LUA

Moderator: leecollings

Post Reply
lassiko
Posts: 15
Joined: Tuesday 04 October 2016 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

How to cut output of svalues in LUA

Post 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!
Toulon7559
Posts: 849
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: mixed
Location: Hengelo(Ov)/NL
Contact:

Re: How to cut output of svalues in LUA

Post 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

Last edited by Toulon7559 on Friday 31 March 2017 18:56, edited 2 times in total.
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
lassiko
Posts: 15
Joined: Tuesday 04 October 2016 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to cut output of svalues in LUA

Post 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
User avatar
jvdz
Posts: 2269
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: How to cut output of svalues in LUA

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
lassiko
Posts: 15
Joined: Tuesday 04 October 2016 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to cut output of svalues in LUA

Post 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:

Code: Select all

LUA: 1.2;80.0;0
User avatar
jvdz
Posts: 2269
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: How to cut output of svalues in LUA

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Toulon7559
Posts: 849
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: mixed
Location: Hengelo(Ov)/NL
Contact:

Re: How to cut output of svalues in LUA

Post 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("([^;]+);([^;]+);([^;]+)")
Last edited by Toulon7559 on Friday 31 March 2017 19:00, edited 1 time in total.
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
lassiko
Posts: 15
Joined: Tuesday 04 October 2016 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to cut output of svalues in LUA

Post 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!
lassiko
Posts: 15
Joined: Tuesday 04 October 2016 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to cut output of svalues in LUA

Post 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!
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest