Page 1 of 1
lua script if Humidity >...% then
Posted: Sunday 03 January 2016 13:07
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
Re: lua script if Humidity >...% then
Posted: Sunday 03 January 2016 13:17
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
Re: lua script if Humidity >...% then
Posted: Sunday 03 January 2016 13:52
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
- badkamer.png (2.01 KiB) Viewed 4899 times
thankyou
Re: lua script if Humidity >...% then
Posted: Sunday 03 January 2016 14:08
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
Re: lua script if Humidity >...% then
Posted: Sunday 03 January 2016 14:13
by bizziebis
I use this Lua script:
https://www.domoticz.com/wiki/Humidity_control
Easy to adjust and working very well!
Re: lua script if Humidity >...% then
Posted: Sunday 03 January 2016 14:15
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)
Re: lua script if Humidity >...% then
Posted: Sunday 03 January 2016 14:20
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
Re: lua script if Humidity >...% then
Posted: Sunday 03 January 2016 14:24
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.
Re: lua script if Humidity >...% then
Posted: Sunday 03 January 2016 14:36
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
Re: lua script if Humidity >...% then
Posted: Sunday 03 January 2016 14:39
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
Re: lua script if Humidity >...% then
Posted: Sunday 03 January 2016 14:40
by jvdz
This is for the last script I posted 3 minutes before your last post?
Jos
Re: lua script if Humidity >...% then
Posted: Sunday 03 January 2016 14:52
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!!
Re: lua script if Humidity >...% then
Posted: Sunday 03 January 2016 14:57
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.
Re: lua script if Humidity >...% then
Posted: Sunday 03 January 2016 20:20
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.
Re: lua script if Humidity >...% then
Posted: Sunday 03 January 2016 20:48
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
Re: lua script if Humidity >...% then
Posted: Sunday 03 January 2016 21:14
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
Re: lua script if Humidity >...% then
Posted: Sunday 03 January 2016 21:24
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