lua script if Humidity >...% then

Moderator: leecollings

Post Reply
robingerritsen
Posts: 10
Joined: Thursday 10 December 2015 21:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

lua script if Humidity >...% then

Post by robingerritsen »

Hello,

I'm trying to create a script, that the fan turn on when the humidity above 60% is.
I know that this script is completely wrong but I had something like this in mind:

Code: Select all

commandArray = {}
sWeatherTemp, sWeatherHumidity, sWeatherUV, sWeatherPressure, sWeatherUV2 = otherdevices_svalues[Badkamer]:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")

sWeatherHumidity = tonumber(sWeatherHumidity);



if (devicechanged['Badkamer'] > 60) then

commandArray['Fan']='On'
   
end
return commandArray



I know that there are many examples, but this would not succeed me
I hope someone can help me
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: lua script if Humidity >...% then

Post by jvdz »

Are you sure the device Badkamer contains all that weather data? What is the exact value of the device listed under Setup/Devices?
I guess it should be something like this and name the script script_time_xxxxxx.lua to do the check one time each minute:

Code: Select all

commandArray = {}
sWeatherTemp, sWeatherHumidity, sWeatherUV, sWeatherPressure, sWeatherUV2 = otherdevices_svalues[Badkamer]:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")
sWeatherHumidity = tonumber(sWeatherHumidity);
if (sWeatherHumidity > 60 and otherdevices['Fan'] == 'Off') then
	print("luchtvochtigheid in de Badkamer is groter dan 60%, zet ventilator aan. " .. sWeatherHumidity)
	commandArray['Fan']='On'
elsif (sWeatherHumidity < 55 and otherdevices['Fan'] == 'On') then
	print("luchtvochtigheid in de Badkamer is lager dan 55%, zet ventilator uit. " .. sWeatherHumidity)
	commandArray['Fan']='Off'
end
return commandArray
EDIT: Updated the script to avoid setting the FAN each minute.
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
robingerritsen
Posts: 10
Joined: Thursday 10 December 2015 21:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: lua script if Humidity >...% then

Post by robingerritsen »

jvdz wrote:Are you sure the device Badkamer contains all that weather data? What is the exact value of the device listed under Setup/Devices?
I guess it should be something like this and name the script script_time_xxxxxx.lua to do the check one time each minute:

Code: Select all

commandArray = {}
sWeatherTemp, sWeatherHumidity, sWeatherUV, sWeatherPressure, sWeatherUV2 = otherdevices_svalues[Badkamer]:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")
sWeatherHumidity = tonumber(sWeatherHumidity);
if (sWeatherHumidity > 60 and otherdevices['Fan'] == 'Off') then
	print("luchtvochtigheid in de Badkamer is groter dan 60%, zet ventilator aan. " .. sWeatherHumidity)
	commandArray['Fan']='On'
elsif (sWeatherHumidity < 55 and otherdevices['Fan'] == 'On') then
	print("luchtvochtigheid in de Badkamer is lager dan 55%, zet ventilator uit. " .. sWeatherHumidity)
	commandArray['Fan']='Off'
end
return commandArray
EDIT: Updated the script to avoid setting the FAN each minute.
Jos
Thanks for your reply
I added the script but I get the following error
2016-01-03 13:47:00.094 Error: EventSystem: /home/pi/domoticz/scripts/lua/script_time_Badkamer.lua:7: unexpected symbol near 'then'

I've here a screenshot of the Device page
Screenshot device
Screenshot device
badkamer.png (2.01 KiB) Viewed 4897 times
thankyou
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: lua script if Humidity >...% then

Post by jvdz »

I see I had a typo in there for elseif.
Had a closer look at the LUA event options for that type of device and think something like this should work as a "script_device_xxxx.lua" event:

Code: Select all

commandArray = {}
if (devicechanged['Badkamer_Humidity'] > 60 and otherdevices['Fan'] == 'Off') then
	print("luchtvochtigheid in de Badkamer is groter dan 60%, zet ventilator aan. " .. devicechanged['Badkamer_Humidity'])
	commandArray['Fan']='On'
elseif (devicechanged['Badkamer_Humidity'] < 55 and otherdevices['Fan'] == 'On') then
	print("luchtvochtigheid in de Badkamer is lager dan 55%, zet ventilator uit. " .. devicechanged['Badkamer_Humidity'])
	commandArray['Fan']='Off'
end
return commandArray
Wiki refference https://www.domoticz.com/wiki/Events somewhere in the middle of that page.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
bizziebis
Posts: 182
Joined: Saturday 19 October 2013 14:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8805
Location: The Netherlands
Contact:

Re: lua script if Humidity >...% then

Post by bizziebis »

I use this Lua script: https://www.domoticz.com/wiki/Humidity_control

Easy to adjust and working very well!
robingerritsen
Posts: 10
Joined: Thursday 10 December 2015 21:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: lua script if Humidity >...% then

Post by robingerritsen »

jvdz wrote:I see I had a typo in there for elseif.
Had a closer look at the LUA event options for that type of device and think something like this should work as a "script_device_xxxx.lua" event:

Code: Select all

commandArray = {}
if (devicechanged['Badkamer_Humidity'] > 60 and otherdevices['Fan'] == 'Off') then
	print("luchtvochtigheid in de Badkamer is groter dan 60%, zet ventilator aan. " .. devicechanged['Badkamer_Humidity'])
	commandArray['Fan']='On'
elseif (devicechanged['Badkamer_Humidity'] < 55 and otherdevices['Fan'] == 'On') then
	print("luchtvochtigheid in de Badkamer is lager dan 55%, zet ventilator uit. " .. devicechanged['Badkamer_Humidity'])
	commandArray['Fan']='Off'
end
return commandArray
Wiki refference https://www.domoticz.com/wiki/Events somewhere in the middle of that page.

Jos
Hi Jos,

Thanks!
I still get an error message, but I think we're almost there

the error message is:
2016-01-03 14:11:00.130 Error: EventSystem: /home/pi/domoticz/scripts/lua/script_device_Badkamer.lua:2: attempt to index field '?' (a nil value)
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: lua script if Humidity >...% then

Post by jvdz »

Is the devicename for your Fan correct ('Fan') ? It is case sensitive and need to be the exact name as listed in the Devices overview.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
robingerritsen
Posts: 10
Joined: Thursday 10 December 2015 21:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: lua script if Humidity >...% then

Post by robingerritsen »

jvdz wrote:Is the devicename for your Fan correct ('Fan') ? It is case sensitive and need to be the exact name as listed in the Devices overview.

Jos
yes, it is exactly 'Fan' also with a capital.
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: lua script if Humidity >...% then

Post by jvdz »

Have done some proper testing this time with devices I have and this logic worked for me.
It will print a log file entry each time an update is received which you might want to comment out when things are working:

Code: Select all

commandArray = {}
if devicechanged['Badkamer_Humidity'] ~= nil then
	print("luchtvochtigheid in de Badkamer update: " .. devicechanged['Badkamer_Humidity'] .. " Fan:" .. otherdevices['Fan'])
	if (devicechanged['Badkamer_Humidity'] > 60 and otherdevices['Fan'] == 'Off') then
		print("luchtvochtigheid in de Badkamer is groter dan 60%, zet ventilator aan. ")
		commandArray['Fan']='On'
	elseif (devicechanged['Badkamer_Humidity'] < 55 and otherdevices['Fan'] == 'On') then
		print("luchtvochtigheid in de Badkamer is lager dan 55%, zet ventilator uit. ")
		commandArray['Fan']='Off'
	end
end
return commandArray
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
robingerritsen
Posts: 10
Joined: Thursday 10 December 2015 21:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: lua script if Humidity >...% then

Post by robingerritsen »

sorry there was something wrong with the upload of the script. I have now uploaded the correct script and get the following error message:

2016-01-03 14:36:36.166 Error: EventSystem: /home/pi/domoticz/scripts/lua/script_device_Badkamer.lua:2: attempt to compare number with nil
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: lua script if Humidity >...% then

Post by jvdz »

This is for the last script I posted 3 minutes before your last post?

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
robingerritsen
Posts: 10
Joined: Thursday 10 December 2015 21:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: lua script if Humidity >...% then

Post by robingerritsen »

jvdz wrote:This is for the last script I posted 3 minutes before your last post?

Jos
Jos you are a hero!! It works!
thank you very much for the time and effort!!
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: lua script if Humidity >...% then

Post by jvdz »

robingerritsen wrote: Jos you are a hero!! It works!
thank you very much for the time and effort!!
No worries, it is fun and a learning experience for me too discovering all possibilities with LUA. :)
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: lua script if Humidity >...% then

Post by dannybloe »

robingerritsen wrote:Hello,

I'm trying to create a script, that the fan turn on when the humidity above 60% is.
I know that this script is completely wrong but I had something like this in mind:

Code: Select all

commandArray = {}
sWeatherTemp, sWeatherHumidity, sWeatherUV, sWeatherPressure, sWeatherUV2 = otherdevices_svalues[Badkamer]:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")

sWeatherHumidity = tonumber(sWeatherHumidity);



if (devicechanged['Badkamer'] > 60) then

commandArray['Fan']='On'
   
end
return commandArray



I know that there are many examples, but this would not succeed me
I hope someone can help me
I think that such a script won't do you any good on the long run. The thing is that this 60% threshold varies from day to day. A while ago I had a hygrostat that was supposed to switch on the fan when a certain amount of humidity was registered in the bathroom but I ended up adjusting this threshold almost every day. On a rainy day it was different than on a cold winter's day. I ditched the hygrostat and created the script https://www.domoticz.com/wiki/Humidity_control. That registers a relative rise in humidity level and tries to dehumidify to the level just before the rise. Works like a charm. But you need a humidity sensor of course.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
robingerritsen
Posts: 10
Joined: Thursday 10 December 2015 21:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: lua script if Humidity >...% then

Post by robingerritsen »

dannybloe wrote:I think that such a script won't do you any good on the long run. The thing is that this 60% threshold varies from day to day. A while ago I had a hygrostat that was supposed to switch on the fan when a certain amount of humidity was registered in the bathroom but I ended up adjusting this threshold almost every day. On a rainy day it was different than on a cold winter's day. I ditched the hygrostat and created the script https://www.domoticz.com/wiki/Humidity_control. That registers a relative rise in humidity level and tries to dehumidify to the level just before the rise. Works like a charm. But you need a humidity sensor of course.
hello,

I have edit my script. If the humidity is above 65%, the fan wil turn on for 10 minutes. The script does this once per hour

Code: Select all

t1 = os.time()
s = otherdevices_lastupdate['Fan']

 
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)


commandArray = {}

t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = (os.difftime (t1, t2))

if devicechanged['Badkamer_Humidity'] ~= nil then
   print("luchtvochtigheid in de Badkamer update: " .. devicechanged['Badkamer_Humidity'] .. " Fan:" .. otherdevices['Fan'])
   if (devicechanged['Badkamer_Humidity'] >= 65) then
   if (otherdevices['Fan'] == 'Off' and difference > 3600 and difference < 3660) then
      print("luchtvochtigheid in de Badkamer is groter dan 65%, zet ventilator aan. ")
      commandArray['Fan']='On FOR 10'
   end
   end
end
return commandArray
But thanks for your replay, I'm going to see how the Humidity control script works and maybe I'm going to use that script
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: lua script if Humidity >...% then

Post by jvdz »

Maybe I am confused now but the result of your test is that the FAN will only be activated when the Humidity is higher than 65% before 1 hour is passed since the last time the fan was activated? When the humidity is only > 65% after the 3660 seconds is passed, the fan will not be activated anymore.

Code: Select all

   if (otherdevices['Fan'] == 'Off' and difference > 3600 and difference < 3660) then
Shouldn't that be that you want to wait at least 1 hour since last activation? like:

Code: Select all

   if (otherdevices['Fan'] == 'Off' and difference > 3600) then
Disadvantage will be when taking a shower, it can take up to 60 minutes before the fan is activated. :)

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
robingerritsen
Posts: 10
Joined: Thursday 10 December 2015 21:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: lua script if Humidity >...% then

Post by robingerritsen »

jvdz wrote:Maybe I am confused now but the result of your test is that the FAN will only be activated when the Humidity is higher than 65% before 1 hour is passed since the last time the fan was activated? When the humidity is only > 65% after the 3660 seconds is passed, the fan will not be activated anymore.

Code: Select all

   if (otherdevices['Fan'] == 'Off' and difference > 3600 and difference < 3660) then
Shouldn't that be that you want to wait at least 1 hour since last activation? like:

Code: Select all

   if (otherdevices['Fan'] == 'Off' and difference > 3600) then
Disadvantage will be when taking a shower, it can take up to 60 minutes before the fan is activated. :)

Jos
Thanks Jos!, I have changed my script
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests