Page 1 of 1
Timer with user variables
Posted: Tuesday 05 January 2016 11:25
by alttab
Hi,
I want to get this working:
- In Domoticz i store a time user variable
- In a LUA script this time variable should be used as a timer for a device (kaku switch)
- This kaku switch has to be switched on (for instance) 3 moments:
* time stored in variable
* time stored in variable + 1 minute
* time stored in variable + 2 minutes
* time stored in variable + 15 minutes
(this is to mitigrate the unreliability of 433mhz)
Is this possible? Can i find some examples of this somewhere?
Thanks in advance!
Re: Timer with user variables
Posted: Tuesday 05 January 2016 15:54
by D'rMorris
Yes, with Lua everything is possible

.
You can create a uservariable of type "time" called timer_lamp for example.
Then, you could use the code:
Code: Select all
commandArray = {}
-- Setting the time variables:
time = os.date("%H:%M")
timer_lamp = uservariables["timer_lamp"]
print(' Time______:' .. time .. ' ')
print(' Timer lamp:' .. timer_lamp .. ' ')
if time == timer_lamp and otherdevices['your_lamp_name'] == 'Off'
then
print('<b style="color:Red">>> It is time to turn the lamp on</b>')
commandArray['your_lamp_name']='On'
--else
--whatever
end
return commandArray
I'm still thinking about how to do the offsets. You could create 2 additional variables and create 2 additional if statements, but it's nicer to fix it in the code with an offset. If you do it with 2 additional if statements, remove the "and otherdevices['your_lamp_name'] == 'Off'" part in the code. If the signal did not reach the lamp, Domoticz then the lamp is on in Domoticz, but it didn't reach the lamp. So you can either leave that part out or you change it to "and otherdevices['your_lamp_name'] == 'On'"
Re: Timer with user variables
Posted: Tuesday 05 January 2016 18:40
by alttab
Thanks, appreciated!
Would it be possible to set 3 additional time variables in the Lua script (for instance timer_lamp2= timer_lamp + 1m.)
With 'OR' they can then be used to turn on the device multiple times.
edit; i experimented a bit, and it seems i have to convert it back from string to use it as a time object (to be able to add 60 seconds)
Unfortunally then it turns in unix time (gotta love *nix

)
What i can do is let the kaku switch every minute (lua script execute time) between uservariable and uservariable + 300. Lamp will switch 5 times then.
If i can do this more beautiful please let me know

(will keep on experimenting)
Re: Timer with user variables
Posted: Tuesday 05 January 2016 21:35
by alttab
so, this is what i did.
works great. any advice or things i should do on another way?
Also it would even be better if i could iterate trough the devices with a foreach (based on the names) so i dont have to add all device names. (i have sevaral devices which should be switched at once). Will look into that later.
Any help apriciated!
Code: Select all
commandArray = {}
timer_on = uservariables["on_uvb_zolder"]
timer_off = uservariables["off_uvb_zolder"]
current_time = os.time()
year = os.date("%Y")
month = os.date("%m")
day = os.date("%d")
timer_on_minute = string.sub(timer_on, 4, 5)
timer_on_hour = string.sub(timer_on, 1, 2)
timer_off_minute = string.sub(timer_off, 4, 5)
timer_off_hour = string.sub(timer_off, 1, 2)
timer_on = os.time{year=year, month=month, day=day, hour=timer_on_hour, min=timer_on_minute}
timer_on_end = timer_on + 240
timer_off = os.time{year=year, month=month, day=day, hour=timer_off_hour, min=timer_off_minute}
timer_off_end = timer_off + 240
if (current_time > timer_on and current_time < timer_on_end)
then
print('UVB LAMP AAN')
commandArray['Lamp Woonkamer Achter']='On'
commandArray['Lamp Woonkamer Voor']='On'
elseif
(current_time > timer_off and current_time < timer_off_end)
then
print('UVB LAMP UIT')
commandArray['Lamp Woonkamer Achter']='Off'
commandArray['Lamp Woonkamer Voor']='Off'
end
return commandArray