LUA-script + virtual sensor + temperature + humidity

Moderator: leecollings

Post Reply
Gusten58
Posts: 2
Joined: Tuesday 19 January 2016 21:29
Target OS: Raspberry Pi / ODroid
Domoticz version: v2.3868
Location: Sweden
Contact:

LUA-script + virtual sensor + temperature + humidity

Post by Gusten58 »

Can someone please help me understand how I should make a working LUA-script so the temperature and humidity will show up in a virtual sensor?

I’m importing temperature and humidity from a Tellstick Net sensor and store them in a file on my Raspberry Pi, then I want a LUA-script to place these values in to a virtual sensor, but I cannot figure out how to make this happen. I’m not so familiar with LUA-programming, but I have modifyed another LUA-script that I use for a wheather station where I only "scraping" the temperature as an referens.

I have installed a virtual sensor for temperature and humidity (Idx=45) and the file with temperature and humidity data contains the values “21;45” (+21 degrees Celsius and 45 % humidity).

Then I use a LUA-script that reads the file with the semicolon separated temperature and humidity data and place these values into variables that is sent to the virtual sensor “idxt” and “idxh”. I use another “real” temperature sensor (“Utetemp”) as an trigger for the script :

Code: Select all

--
--The following need updated for your environment get the 'Idx' or 'Name' off the Device tab. By default only the Temp is 'uncommented or enabled' in this script.
local sensorwu = 'Utetemp' --name of the sensor that gets created when you add the WU device (and that contains multiple values like temperature, humidity, barometer etc)
local idxt = 45  --idx of the virtual temperature sensor you need to change this to your own Device IDx
local idxh = 45  --idx of the virtual humidity sensor you need to change this to your own Device IDx
--

commandArray = {}

if devicechanged[sensorwu] then

f = assert (io.open ("/home/pi/tellstick/data/temp-temp+hum.txt", "r"))  -- open the file with the temperature and humidity informatiom
sTempHum = f:read ("*a")  -- read all data in file
print (sTempHum)          -- print out temperature and humidity (separated by a semicolon)
f:close ()                -- close file

        sWeatherTemp, sWeatherHumidity = sTempHum:match("([^;]+);([^;]+)")
        sWeatherTemp = tonumber(sWeatherTemp)
        sWeatherHumidity = tonumber(sWeatherHumidity)

        print(sWeatherTemp)      -- print temperature
        print(sWeatherHumidity)  -- print humidity

        commandArray[1] = {['UpdateDevice'] = idxt .. '|0|' .. sWeatherTemp}
        commandArray[2] = {['UpdateDevice'] = idxh .. '|' .. tostring(sWeatherHumidity)}

end

return commandArray
The log is showing this:
logg.jpg
logg.jpg (23.38 KiB) Viewed 20449 times
The virtual sensor is showing nothing:
no_temp_or_hum.jpg
no_temp_or_hum.jpg (27.15 KiB) Viewed 20449 times
The actual virtual sensor values after running LUA-script (from “show current states”):
idx45_current_state.jpg
idx45_current_state.jpg (15.46 KiB) Viewed 20449 times
What could be wrong in LUA-script?
Last edited by Gusten58 on Friday 22 January 2016 0:44, edited 1 time in total.
Tellstick Net + 443Mhz sensors and switches. Raspberry Pi model B + RaZberry + Domoticz v2.3868 + 868,42Mhz Z-Wave sensors and switches. Android Apps: Telldus Live + Remotestick + Domoticz + Dromotica + Andromoticz.
jmleglise
Posts: 192
Joined: Monday 12 January 2015 23:27
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: FRANCE
Contact:

Re: LUA-script + virtual sensor + temperature + humidity

Post by jmleglise »

Did you use the Temp+Humidity virtual sensor ? So it's only 1 sensor and you must send Temp & humidity in the same svalue like explained here : https://www.domoticz.com/wiki/Domoticz_ ... 2Fhumidity


What you have in log : 21;45 is exactly what you need to send to the sensor. No need to get strings to convert them to numbers to convert them back to strings ;)

This will be done with only 1 command. Something like that :

Code: Select all

local idx = 45
commandArray = {}
if devicechanged[sensorwu] then

f = assert (io.open ("/home/pi/tellstick/data/temp-temp+hum.txt", "r"))  -- open the file with the temperature and humidity informatiom
sTempHum = f:read ("*a")  -- read all data in file
print (sTempHum)          -- print out temperature and humidity (separated by a semicolon)
f:close ()                -- close file

        commandArray[1] = {['UpdateDevice'] = idx .. '|0|' .. sTempHum..';0'}

end

return commandArray
My script : https://github.com/jmleglise
RFXTRX433E: Blind Somfy RTS, Portal Somfy Evolvia, chacon IO, Oregon, PIR sensor PT2262
My Last project : Location de vacances a Ouistreham vue mer
KMTronic USB relay
Chinese Z-WAVE: Neo CoolCam
jmleglise
Posts: 192
Joined: Monday 12 January 2015 23:27
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: FRANCE
Contact:

Re: LUA-script + virtual sensor + temperature + humidity

Post by jmleglise »

oh ! by the way, you will have more chance to get answer , in the this SubForum : http://www.domoticz.com/forum/viewforum.php?f=23
My script : https://github.com/jmleglise
RFXTRX433E: Blind Somfy RTS, Portal Somfy Evolvia, chacon IO, Oregon, PIR sensor PT2262
My Last project : Location de vacances a Ouistreham vue mer
KMTronic USB relay
Chinese Z-WAVE: Neo CoolCam
bologni
Posts: 1
Joined: Wednesday 20 January 2016 17:49
Target OS: Windows
Domoticz version:
Contact:

Re: LUA-script + virtual sensor + temperature + humidity

Post by bologni »

Bonjour jmleglise, excusez mon français.
Je vous écris d'Italie et je voyais que vous "IP vidéo sonnette ATZ-DBV01P« Je voudrais connaître votre opinion sur ce Videophone. Il fonctionne bien avec Internet Phone?
Je ne pouvais trouver aucune vidéo de démonstration de cela, ce serait trop demander à une vidéo de démonstration à court de la vôtre? Je serais très reconnaissant.
Gusten58
Posts: 2
Joined: Tuesday 19 January 2016 21:29
Target OS: Raspberry Pi / ODroid
Domoticz version: v2.3868
Location: Sweden
Contact:

Re: LUA-script + virtual sensor + temperature + humidity

Post by Gusten58 »

Many thank's jmleglise :D Your script code example works great. Now I can combine the temp and humidity values in the same virtual sensor instead of having separate virtual sensors for each one I've had before.

Yes, I used a Temp + Humidity virtual sensor when I tried to get the LUA code to work and was a bit desperate to make it work :lol:

Also thank you for your proposal to me to continue ask additional questions in the subforum "Board Index -> Functionalities and Protocols -> Scripts".
Tellstick Net + 443Mhz sensors and switches. Raspberry Pi model B + RaZberry + Domoticz v2.3868 + 868,42Mhz Z-Wave sensors and switches. Android Apps: Telldus Live + Remotestick + Domoticz + Dromotica + Andromoticz.
jmleglise
Posts: 192
Joined: Monday 12 January 2015 23:27
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: FRANCE
Contact:

Re: LUA-script + virtual sensor + temperature + humidity

Post by jmleglise »

@bologni : look, You have a whole thread especially for you :D
http://www.domoticz.com/forum/viewtopic.php?f=4&t=10118
My script : https://github.com/jmleglise
RFXTRX433E: Blind Somfy RTS, Portal Somfy Evolvia, chacon IO, Oregon, PIR sensor PT2262
My Last project : Location de vacances a Ouistreham vue mer
KMTronic USB relay
Chinese Z-WAVE: Neo CoolCam
chatainsim
Posts: 101
Joined: Wednesday 25 March 2015 10:46
Target OS: Linux
Domoticz version: v4.11307
Location: France
Contact:

Re: LUA-script + virtual sensor + temperature + humidity

Post by chatainsim »

Hello,
I alsoe have the same issue with the virtual device.
He doesn't display temp or humidity.
I'm using a NodeMCU to call the json api of Domoticz.
Update is working fine and graph also but there is no value display in the temp tab.
display.PNG
display.PNG (12.54 KiB) Viewed 19785 times
Here is the graph:
graph.PNG
graph.PNG (113.67 KiB) Viewed 19785 times
And the device row:
device.PNG
device.PNG (12.6 KiB) Viewed 19785 times
Thank you.
chatainsim
Posts: 101
Joined: Wednesday 25 March 2015 10:46
Target OS: Linux
Domoticz version: v4.11307
Location: France
Contact:

Re: LUA-script + virtual sensor + temperature + humidity

Post by chatainsim »

Ok, I figure out why.
In my API call, I haven't set the HUM_SAT parameter.
So that's why.

I used this:
http://192.168.1.254:3434/json.htm?type ... alue=23;35

Instead of:
http://192.168.1.254:3434/json.htm?type ... ue=23;35;0

Do you know how to get the HUM_SAT value ? Is it a calcul or something ?

Thanks
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: LUA-script + virtual sensor + temperature + humidity

Post by elmortero »

Hi all,

I have a simular issue.
The humidity gets scrapped correctly from the actual sensor (and diplayed in the log)
But when it comes to writing the value to a virtual Humidity sensor it fails.
The logs show that the sensor gets updated, but with a 0 instead of the earlier read value.

any suggestions anyone?

Code: Select all

commandArray = {}

bodem = 'soil1'
vsenshum = 'humsoil1'
vsenshum_id = '338'

   smoistsensorTemp, smoistsensorHumidity = otherdevices_svalues[bodem]:match("([^;]+);([^;]+);([^;]+)")
   smoistsensorTemp = tonumber(smoistsensorTemp);
   smoistsensorHumidity = tonumber(smoistsensorHumidity);
 print("xt300: Humidity is " .. smoistsensorHumidity .. " ");

    commandArray['UpdateDevice'] = vsenshum_id .. "|0|" ..smoistsensorHumidity

return commandArray
Attachments
soil.JPG
soil.JPG (38.28 KiB) Viewed 19247 times
marmachine
Posts: 133
Joined: Saturday 26 March 2016 10:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: LUA-script + virtual sensor + temperature + humidity

Post by marmachine »

Any luck yet?
I was actually looking into a similar issue...

- My virtual device, named 'Luchtvochtigheid' displays a value of 75%
- In devices, i see the exact variables: IDX 85 Virtual Weather Devices 140A4 1 Luchtvochtigheid Humidity LaCrosse TX3 Humidity 75 %
- When i am trying to read the value from LUA: print("humidity: "..otherdevices_svalues["Luchtvochtigheid"])
- This prints: 2016-09-09 15:24:02.957 LUA: humidity: 3

Am i missing something here or what?
Version: 3.4834 | Build Hash: 8398125 | Compile Date: 2016-03-02 08:43:51
marmachine
Posts: 133
Joined: Saturday 26 March 2016 10:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: LUA-script + virtual sensor + temperature + humidity

Post by marmachine »

Lisa wrote:viewtopic.php?f=12&t=13280
Thanks for your reply!
Maybe i am missing your point, but i already have a value in my virtual device.
It displays correct in Domoticz, however when i try to do something with it's current value in a LUA event script, it comes up with an incorrect value.
I am using otherdevices_svalues["Luchtvochtigheid"] to get it, this works for all other devices, just this specific one doesn't!

Actually, looking at this again i think it returns 2 instead of 72, earlier i had 3 instead of 73.
Json data works just fine:

Code: Select all

{
   "ActTime" : 1473451006,
   "ServerTime" : "2016-09-09 21:56:46",
   "Sunrise" : "07:04",
   "Sunset" : "20:05",
   "result" : [
      {
         "AddjMulti" : 1.0,
         "AddjMulti2" : 1.0,
         "AddjValue" : 0.0,
         "AddjValue2" : 0.0,
         "BatteryLevel" : 255,
         "CustomImage" : 0,
         "Data" : "Humidity 72 %",
         "Description" : "",
         "Favorite" : 0,
         "HardwareID" : 20,
         "HardwareName" : "Virtual Weather Devices",
         "HardwareType" : "Dummy (Does nothing, use for virtual switches only)",
         "HardwareTypeVal" : 15,
         "HaveTimeout" : false,
         "Humidity" : 72,
         "HumidityStatus" : "Wet",
         "ID" : "140A4",
         "LastUpdate" : "2016-09-09 21:51:04",
         "Name" : "Luchtvochtigheid",
         "Notifications" : "false",
         "PlanID" : "0",
         "PlanIDs" : [ 0 ],
         "Protected" : false,
         "ShowNotifications" : true,
         "SignalLevel" : "-",
         "SubType" : "LaCrosse TX3",
         "Timers" : "false",
         "Type" : "Humidity",
         "TypeImg" : "temperature",
         "Unit" : 1,
         "Used" : 1,
         "XOffset" : "0",
         "YOffset" : "0",
         "idx" : "85"
      }
   ],
   "status" : "OK",
   "title" : "Devices"
}
So i am wondering if we are looking at a bug here!?
marmachine
Posts: 133
Joined: Saturday 26 March 2016 10:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: LUA-script + virtual sensor + temperature + humidity

Post by marmachine »

Lisa wrote:It's a virtual device, how do you set it?
Here's how i set it, using a LUA (event) script;

Code: Select all

if ((idxHumidity) and (humidity)) then
        
        -- Average values per season, depend on location. (Found my values here: http://www.klimaatatlas.nl/klimaatatlas.php)
        -- Winter: 88-90%
        -- Spring: 78-80%
        -- Summer: 78-80%
        -- Aughtum: 86-88%
        
        -- Mapping for Humidity_status:
        -- 0    = Normal 
        -- 1    <> 46-70%   = Comfortable 
        -- 2    < 46        = Dry
        -- 3    > 70%       = Wet

        hum_num = humidity:gsub('%W','') -- get numeric value from string
        hum_num = tonumber(hum_num)      -- make it a numeric value

        if hum_num == nil                           then humidity_status = nil 
        else
            if ((hum_num >= 46) and (hum_num <= 70)) then humidity_status = "1" -- comfortable
            elseif (hum_num < 46)                   then humidity_status = "2" -- dry
            elseif (hum_num > 70)                   then humidity_status = "3" -- wet
            else humidity_status = "0" end
        end
        
        commandArray[indexArray] = {['UpdateDevice'] = tostring(idxHumidity)..'|'..humidity..'|'..humidity_status}
        indexArray=indexArray+1
        if( DEBUG == 1) then print("| Humidity device updated with value: "..tostring(humidity).." and status: "..tostring(humidity_status)) end
end
So, looking at my log and the above scripting again, i am probably getting the status value instead of the percentage.
However, i still don't see how i can get the percentage value... i'd expect to receive a string like "72;2"
Is there any documentation about this? It's a bugger since all other sensors report as expected.

(i'm working on a script to update value 'trend' (up/no change/down) for temp and humidity sensors)
marmachine
Posts: 133
Joined: Saturday 26 March 2016 10:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: LUA-script + virtual sensor + temperature + humidity

Post by marmachine »

Hi Lisa,

Thanks again for your response.

This 'strange' way of updating devices is something that i've found on the forum here.
Intended to prevent command overwriting.

Anyhow, the following command:

Code: Select all

commandArray[indexArray] = {['UpdateDevice'] = tostring(idxHumidity)..'|'..humidity..'|'..humidity_status}
is basically the same as:

Code: Select all

commandArray['UpdateDevice'] = tostring(idxHumidity)..'|'..humidity..'|'..humidity_status
So i don't see any relation between my issue and this command.

When i do this (within the same script) with the variables in the commandArray above:

Code: Select all

print("| Humidity device updated with value: "..tostring(humidity).." and status: "..tostring(humidity_status))
My log shows:
2016-09-13 12:52:03.745 LUA: | Humidity device updated with value: 75% and status: 3

<< At this point, the virtual device in Domoticz shows the correct value: 'Luchtvochtigheid 75%, condition: wet' >>

Now since i require the current value, i do this (in another event script) to get the values from the (virtual) device which has been set:

Code: Select all

print('> otherdevices_svalues["Luchtvochtigheid"] = '..tostring(otherdevices_svalues["Luchtvochtigheid"]))
Now my log shows the result:
2016-09-13 12:55:03.720 LUA: > otherdevices_svalues["Luchtvochtigheid"] = 3

So, where has my humidity percentage gone?
marmachine
Posts: 133
Joined: Saturday 26 March 2016 10:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: LUA-script + virtual sensor + temperature + humidity

Post by marmachine »

Well, as i wrote earlier, my 'setter' log shows the values i set (between 'quotes'):
2016-09-13 12:52:03.745 LUA: | Humidity device updated with value: '75%' and status: '3'

The result of the commandArray line in my script is that the virtual device is updated every time the script runs, so over time the values change up and down. The device logging also displays a nice diagram of the values over time, so what i am trying to say is that i don't see why my script would be wrong.

Adding your 'print' line however, results in: 2016-09-14 11:15:04.013 LUA: Debug: 85|73%|3
marmachine
Posts: 133
Joined: Saturday 26 March 2016 10:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: LUA-script + virtual sensor + temperature + humidity

Post by marmachine »

Lisa wrote:And the svalues in the 'show current states'?
Don't look good either, the percentage seems to be missing there as well:
85 Luchtvochtigheid 2016-09-15 09:35:04 1
marmachine
Posts: 133
Joined: Saturday 26 March 2016 10:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: LUA-script + virtual sensor + temperature + humidity

Post by marmachine »

Lisa wrote:and when you hardcode it?
You mean this;

In my Wunderground script -

Code: Select all

print ('Debug: '..tostring(idxHumidity)..'|'..humidity..'|'..humidity_status)
commandArray['Luchtvochtigheid'] = tostring(idxHumidity)..'|'..humidity..'|'..humidity_status
if(DEBUG == 1) then print("| Humidity device set with command: commandArray['Luchtvochtigheid'] = "..tostring(idxHumidity).."|"..humidity.."|"..humidity_status) end
log:
2016-09-16 23:22:04.024 LUA: Debug: 85|76|3
2016-09-16 23:22:04.024 LUA: | Humidity device set with command: commandArray['Luchtvochtigheid'] = 85|76|3

In my Trend script -

Code: Select all

print('> otherdevices_svalues["Luchtvochtigheid"] = '..tostring(otherdevices_svalues["Luchtvochtigheid"]))
log:
2016-09-16 23:25:03.413 LUA: > otherdevices_svalues["Luchtvochtigheid"] = 3
marmachine
Posts: 133
Joined: Saturday 26 March 2016 10:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: LUA-script + virtual sensor + temperature + humidity

Post by marmachine »

Well... i'm back at the subject i guess... for my previous issue i've found a workaround, but now i don't and would like to sort this out:

My values:
HUM = 70 (%)
STATUS = 3 (wet)

I set my (virtual) humidity device through a json command, like:
http://192.168.2.100:8080/json.htm?type ... lue=STATUS

Then, i want to use the value HUM, by reading from the (virtual) humidity device in a LUA (event) script.
I do that with the following code for my temperature device:

Code: Select all

otherdevices_svalues[temp_device];
This will return the current temperature value

However, for the humidity device, there is also STATUS, this tells us something about the value, like DRY, NORMAL, WET...
When i try to get the value, i mean the humidity (HUM) value, not the STATUS, i am lost when i use the above method.

I call the svalue, i get the STATUS value
I call the nvalue, i get an error: attempt to index global 'otherdevices_nvalues' (a nil value)

So, how do i get the HUM value?

Thanks.
marmachine
Posts: 133
Joined: Saturday 26 March 2016 10:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: LUA-script + virtual sensor + temperature + humidity

Post by marmachine »

Found my answer folks!

My values:
HUMIDITY = 70 (%)
STATUS = 3 (wet)

Set values through json
I set my (virtual) humidity device from an external (Arduino) source through a json command, like:
http:// 192.168.2.100:8080/json.htm?type=command&param=udevice&idx=IDX&nvalue=HUMIDITY&svalue=STATUS

Read values in a LUA (event) script
Then, i want to use the value HUMIDITY, by reading from the (virtual) humidity device in a LUA (event) script.
I do that with the following code for my temperature device:

Code: Select all

TEMPERATURE = otherdevices_svalues[temp_device];
This will return the current temperature value

However, for a humidity device, there is also STATUS, this tells us something about the value, like DRY, NORMAL, WET...
When i try to get the value, i mean the humidity (HUMIDITY) value, not the STATUS which you get when you use the above method.

So, to get the HUMIDITY (nvalue) and STATUS (svalue) through a LUA script, this is how to:

Code: Select all

HUMIDITY = otherdevices_humidity["humidity_device"];
STATUS = otherdevices_svalues["humidity_device"];
Big thanks to aleph0!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest