Lua for 'turn on for x minutes'
Moderator: leecollings
-
- Posts: 63
- Joined: Monday 18 January 2016 21:43
- Target OS: Windows
- Domoticz version: 3.5196
- Location: Seattle, WA
- Contact:
Lua for 'turn on for x minutes'
Trying to figure out the appropriate way to write the lua to handle turning a light on for 15 minutes.
Thanks,
Justin
Thanks,
Justin
[ Domoticz 3.5196 | Win10 | AeoTec Z-Stick Gen5 ]
- jvdz
- Posts: 2189
- Joined: Tuesday 30 December 2014 19:25
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 4.107
- Location: Netherlands
- Contact:
Re: Lua for 'turn on for x minutes'
The syntax is described in the wiki: https://www.domoticz.com/wiki/Events
e.g.:
Jos
e.g.:
Code: Select all
commandArray['Porch Light']='On FOR 15'
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
-
- Posts: 63
- Joined: Monday 18 January 2016 21:43
- Target OS: Windows
- Domoticz version: 3.5196
- Location: Seattle, WA
- Contact:
Re: Lua for 'turn on for x minutes'
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
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
[ Domoticz 3.5196 | Win10 | AeoTec Z-Stick Gen5 ]
-
- Posts: 708
- Joined: Saturday 27 February 2016 12:49
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2020.2
- Location: NL
- Contact:
Re: Lua for 'turn on for x minutes'
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"...
Hans
Re: RE: Re: Lua for 'turn on for x minutes'
Add a condition that the device needs to be off (or not on) prior turning it on again.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"...
-
- Posts: 63
- Joined: Monday 18 January 2016 21:43
- Target OS: Windows
- Domoticz version: 3.5196
- Location: Seattle, WA
- Contact:
Re: Lua for 'turn on for x minutes'
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?)
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
[ Domoticz 3.5196 | Win10 | AeoTec Z-Stick Gen5 ]
Re: Lua for 'turn on for x minutes'
I would guess that this should work
-
- Posts: 708
- Joined: Saturday 27 February 2016 12:49
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2020.2
- Location: NL
- Contact:
Re: RE: Re: Lua for 'turn on for x minutes'
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.tequila wrote:Add a condition that the device needs to be off (or not on) prior turning it on again.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"...
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.
Hans
Re: Lua for 'turn on for x minutes'
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
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
-
- Posts: 63
- Joined: Monday 18 January 2016 21:43
- Target OS: Windows
- Domoticz version: 3.5196
- Location: Seattle, WA
- Contact:
Re: Lua for 'turn on for x minutes'
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
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
[ Domoticz 3.5196 | Win10 | AeoTec Z-Stick Gen5 ]
-
- Posts: 35
- Joined: Wednesday 04 November 2015 20:59
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Lua for 'turn on for x minutes'
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.
- JohnnySK
- Posts: 25
- Joined: Thursday 28 May 2015 22:14
- Target OS: Raspberry Pi / ODroid
- Domoticz version: beta
- Location: Slovakia
- Contact:
Re: Lua for 'turn on for x minutes'
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.
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.
-
- Posts: 30
- Joined: Monday 20 February 2017 23:07
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: RE: Re: Lua for 'turn on for x minutes'
This is exactly the challenge I am facing. Anyone has found a solution for this??manjh wrote: ↑Wednesday 19 October 2016 23:48That 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.tequila wrote:Add a condition that the device needs to be off (or not on) prior turning it on again.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"...
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.
-
- Posts: 2260
- Joined: Monday 29 August 2016 22:40
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Lua for 'turn on for x minutes'
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
Sent from my SM-A320FL using Tapatalk
-
- Posts: 30
- Joined: Monday 20 February 2017 23:07
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Lua for 'turn on for x minutes'
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.
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.
-
- Posts: 708
- Joined: Saturday 27 February 2016 12:49
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2020.2
- Location: NL
- Contact:
Re: Lua for 'turn on for x minutes'
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.
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.
Hans
-
- Posts: 347
- Joined: Sunday 03 July 2016 16:16
- Target OS: Raspberry Pi / ODroid
- Domoticz version: V2024.3
- Location: Netherlands
- Contact:
Re: Lua for 'turn on for x minutes'
Hans, just to be sure I understand your approach well. The time resolution is a multiple of one (1) minute. Is that correct?
-Bart
-Bart
Raspberry pi 3b
Arduino
KAKU
RfxCom
Zwave
OTGW
Chinese sensors temp (Dallas),movement
Tasmota
Esp8266 espeasy
MQTT
Arduino
KAKU
RfxCom
Zwave
OTGW
Chinese sensors temp (Dallas),movement
Tasmota
Esp8266 espeasy
MQTT
-
- Posts: 708
- Joined: Saturday 27 February 2016 12:49
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2020.2
- Location: NL
- Contact:
Re: Lua for 'turn on for x minutes'
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.
Hans
Who is online
Users browsing this forum: No registered users and 1 guest