Timer with user variables

Moderator: leecollings

Post Reply
alttab
Posts: 25
Joined: Friday 07 November 2014 19:56
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Timer with user variables

Post 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!
D'rMorris
Posts: 138
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: Timer with user variables

Post 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'"
alttab
Posts: 25
Joined: Friday 07 November 2014 19:56
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Timer with user variables

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

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)
alttab
Posts: 25
Joined: Friday 07 November 2014 19:56
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Timer with user variables

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

Who is online

Users browsing this forum: No registered users and 1 guest