Page 1 of 1
Lua for 'turn on for x minutes'
Posted: Monday 17 October 2016 19:30
by jrich523
Trying to figure out the appropriate way to write the lua to handle turning a light on for 15 minutes.
Thanks,
Justin
Re: Lua for 'turn on for x minutes'
Posted: Monday 17 October 2016 19:38
by jvdz
The syntax is described in the wiki:
https://www.domoticz.com/wiki/Events
e.g.:
Code: Select all
commandArray['Porch Light']='On FOR 15'
Jos
Re: Lua for 'turn on for x minutes'
Posted: Monday 17 October 2016 19:46
by jrich523
Thanks, I was looking at the LUA wiki, not a whole ton there.
Here is the whole script, for chuckles
commandArray = {}
sFrontLightName = '$sFrontLight' -- dummy device used for automation
FrongLightName = 'Outside Front Light' -- physical front light switch
if(devicechanged[sFrontLightName] == 'On') then
if(timeofday['Nighttime'] == true) then
commandArray[FrongLightName] = 'On FOR 15'
end
-- if the camera triggers it on without it being dark, just turn off dummy switch
commandArray[sFrontLightName] = 'Off'
end
return commandArray
Re: Lua for 'turn on for x minutes'
Posted: Monday 17 October 2016 20:02
by manjh
Keep in mind that if you submit the "On FOR" command a second time while the light is already on, it will not switch off again. Not sure if this is a bug or a "works as designed"...
Re: RE: Re: Lua for 'turn on for x minutes'
Posted: Monday 17 October 2016 21:42
by tequila
manjh wrote:Keep in mind that if you submit the "On FOR" command a second time while the light is already on, it will not switch off again. Not sure if this is a bug or a "works as designed"...
Add a condition that the device needs to be off (or not on) prior turning it on again.
Re: Lua for 'turn on for x minutes'
Posted: Monday 17 October 2016 23:21
by jrich523
Well then, how does this logic look?
Also in the docs it seems to tell me to reference otherdevices to check the current state of a device however the intellisense doesnt have that variable but it does have a device (was it changed, is this right?)
Code: Select all
commandArray = {}
sFrontLightName = '$sFrontLight' -- dummy device used for automation
FrongLightName = 'Outside Front Light' -- physical front light switch
if(devicechanged[sFrontLightName] == 'On' and timeofday['Nighttime'] == true and device[FrontLightName] == 'Off') then
commandArray[FrontLightName] = 'On FOR 15'
commandArray[sFrontLightName] = 'Off'
end
return commandArray
Re: Lua for 'turn on for x minutes'
Posted: Tuesday 18 October 2016 6:14
by tequila
I would guess that this should work
Re: RE: Re: Lua for 'turn on for x minutes'
Posted: Wednesday 19 October 2016 23:48
by manjh
tequila wrote:manjh wrote:Keep in mind that if you submit the "On FOR" command a second time while the light is already on, it will not switch off again. Not sure if this is a bug or a "works as designed"...
Add a condition that the device needs to be off (or not on) prior turning it on again.
That would leave the timer on for the previously set time. The idea of my application is that the timer is restarted for every time the condition is set.
Example: a PIR sensor is tripped and the LUA script switches the light on for 5 minutes.
Then, 3 minutes later, the sensor is tripped again and the light should stay on for 5 minutes from that moment.
If the "on" command is conditional to the current status, as you suggesst, the light would remain on for a further 2 minutes, not 5.
Re: Lua for 'turn on for x minutes'
Posted: Thursday 20 October 2016 9:33
by tequila
that is of course true.
I have no experience with the 'on for' command so I cannot confirm it would not switch off if triggered for the 2nd time (sounds like a bug to me).
but if this is the case, what you could do is having for example a user variable updated on PIR action (update variable when PIR registers a movement) and a second script (time based, running every minute) turning your light off 5 minutes after last variable update.
or the other way round, a time script turning the light off X minutes after your PIR turned off and the light is still on.
there are several ways to do this based on what you are able to read from your PIR sensor and not using the on for command
Re: Lua for 'turn on for x minutes'
Posted: Wednesday 28 December 2016 3:00
by jrich523
so, i recently got back to this, since i never got it working right the first time..
the first thing i found was that my varirables arent working, and i havent figured out how to resolve that, just a work around... dont use them
if i do
lightName = 'whateverItIs'
print(otherdevices[lightName])
it says it cant find the index (nil). From examples I've looked at, this would appear to be fine?
but if i just do
print(otherdevices['whateverItIs']
so, after changing it to names rather than variables, im kinda working ok, but im trying to make sure it only turns on between sunset and 11pm, but i cant find a good way to check the time. Im sure its something stupid and easy, but, really what im wondering is... where can i find good docs for this?
I found some decent random lui stuff, but i thought I should ask first since there are a good deal of variables that seem to be poorly document or not documented at all...
thanks
Re: Lua for 'turn on for x minutes'
Posted: Thursday 07 November 2019 21:06
by Dirk
manjh wrote: ↑Monday 17 October 2016 20:02
Keep in mind that if you submit the "On FOR" command a second time while the light is already on, it will not switch off again. Not sure if this is a bug or a "works as designed"...
Thank you so much for this remark! At first I had a hard time getting 'On for' to work, then it suddenly worked for one script but not for another. An hour of trouble-shooting later I saq your remark and realised: in the first script I first check if the light is off, then I turn it on for x minutes. Only then will it actually turn off. Otherwise it will stay on forever.
Re: Lua for 'turn on for x minutes'
Posted: Thursday 14 November 2019 19:57
by JohnnySK
Maybe ask gizmocuz for add GET/JSON command.
Simmilar as toggle switch command, called by GET..
I'm dreaming about &command=switchOnFor&t=15
And this I will can add to any button or action in other systems.
Re: RE: Re: Lua for 'turn on for x minutes'
Posted: Monday 23 November 2020 22:25
by reneklomp
manjh wrote: ↑Wednesday 19 October 2016 23:48
tequila wrote:manjh wrote:Keep in mind that if you submit the "On FOR" command a second time while the light is already on, it will not switch off again. Not sure if this is a bug or a "works as designed"...
Add a condition that the device needs to be off (or not on) prior turning it on again.
That would leave the timer on for the previously set time. The idea of my application is that the timer is restarted for every time the condition is set.
Example: a PIR sensor is tripped and the LUA script switches the light on for 5 minutes.
Then, 3 minutes later, the sensor is tripped again and the light should stay on for 5 minutes from that moment.
If the "on" command is conditional to the current status, as you suggesst, the light would remain on for a further 2 minutes, not 5.
This is exactly the challenge I am facing. Anyone has found a solution for this??
Re: Lua for 'turn on for x minutes'
Posted: Monday 23 November 2020 23:29
by Lokonli
For this example (motion sensor triggering a light) just set the switch off delay for the lamp to 5 minutes.
Sent from my SM-A320FL using Tapatalk
Re: Lua for 'turn on for x minutes'
Posted: Monday 23 November 2020 23:40
by reneklomp
Thanks, However, that's not really solving the issue.
I already found again what is the answer to this...
I have solved this now with the lines:
if (devicechanged[sensor] == 'On') then
commandArray[#commandArray + 1] = { [light] = 'On' }
commandArray[#commandArray + 1] = { [light] = 'Off AFTER 300' }
end
So everytime the sensor is triggered, the 300 seconds the lamp should be On is reset.
Seems to work fine.
Thanks, Rene.
Re: Lua for 'turn on for x minutes'
Posted: Monday 04 January 2021 10:09
by manjh
I'm not sure if this will work, since it triggers an internal mechanism in Domoticz to switch the light after 300 seconds.
A long time ago I decided to leave the "on for" approach. I now use this mechanism for several scripts.
1. define a user variable
2. when the trigger is tripped (for instance PIR sensor), set the user variable to the desired number of minutes, and switch on the light. Create a device-driven LUA script to do this.
3. create a time-driven LUA script that does the following:
a. get the user variable into a local var
b. if the value is greater than 0, decrease it 1 and store it
c. if the new value is zero, switch off the light
This will also work if you have multiple sensors. Also, the length of the "on" time can vary with the type of trigger.
Re: Lua for 'turn on for x minutes'
Posted: Sunday 17 January 2021 13:19
by BartSr
Hans, just to be sure I understand your approach well. The time resolution is a multiple of one (1) minute. Is that correct?
-Bart
Re: Lua for 'turn on for x minutes'
Posted: Tuesday 26 January 2021 19:57
by manjh
BartSr wrote: ↑Sunday 17 January 2021 13:19
Hans, just to be sure I understand your approach well. The time resolution is a multiple of one (1) minute. Is that correct?
-Bart
Correct. I use a time driven LUA script to decrease the value every minute, and switch off when zero is reached. This script is called every minute, so the resolution is one minute.
Moreover, if you set the value to 2 minutes, the switch-off moment can be between 60 and 120 minutes after switch on...
But if the switching period is 10 minutes (like in my application), this inaccuracy is quite acceptable.