Lua for 'turn on for x minutes'

Post Reply
jrich523
Posts: 84
Joined: Monday 18 January 2016 22:43
Target OS: Windows
Domoticz version: 3.5196
Location: Seattle, WA
Contact:

Lua for 'turn on for x minutes'

Post by jrich523 » Monday 17 October 2016 19:30

Trying to figure out the appropriate way to write the lua to handle turning a light on for 15 minutes.

Thanks,
Justin
[ Domoticz 3.5196 | Win10 | AeoTec Z-Stick Gen5 ]

User avatar
jvdz
Posts: 1313
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Westland/Netherlands
Contact:

Re: Lua for 'turn on for x minutes'

Post by jvdz » Monday 17 October 2016 19:38

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

jrich523
Posts: 84
Joined: Monday 18 January 2016 22:43
Target OS: Windows
Domoticz version: 3.5196
Location: Seattle, WA
Contact:

Re: Lua for 'turn on for x minutes'

Post by jrich523 » Monday 17 October 2016 19:46

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
[ Domoticz 3.5196 | Win10 | AeoTec Z-Stick Gen5 ]

manjh
Posts: 319
Joined: Saturday 27 February 2016 13:49
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: NL
Contact:

Re: Lua for 'turn on for x minutes'

Post by manjh » 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"... :-(
Hans

tequila
Posts: 92
Joined: Tuesday 02 August 2016 17:08
Target OS: -
Domoticz version:
Contact:

Re: RE: Re: Lua for 'turn on for x minutes'

Post by tequila » Monday 17 October 2016 21:42

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.

jrich523
Posts: 84
Joined: Monday 18 January 2016 22:43
Target OS: Windows
Domoticz version: 3.5196
Location: Seattle, WA
Contact:

Re: Lua for 'turn on for x minutes'

Post by jrich523 » Monday 17 October 2016 23:21

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
[ Domoticz 3.5196 | Win10 | AeoTec Z-Stick Gen5 ]

tequila
Posts: 92
Joined: Tuesday 02 August 2016 17:08
Target OS: -
Domoticz version:
Contact:

Re: Lua for 'turn on for x minutes'

Post by tequila » Tuesday 18 October 2016 6:14

I would guess that this should work

manjh
Posts: 319
Joined: Saturday 27 February 2016 13:49
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: NL
Contact:

Re: RE: Re: Lua for 'turn on for x minutes'

Post by manjh » 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.
Hans

tequila
Posts: 92
Joined: Tuesday 02 August 2016 17:08
Target OS: -
Domoticz version:
Contact:

Re: Lua for 'turn on for x minutes'

Post by tequila » Thursday 20 October 2016 9:33

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

jrich523
Posts: 84
Joined: Monday 18 January 2016 22:43
Target OS: Windows
Domoticz version: 3.5196
Location: Seattle, WA
Contact:

Re: Lua for 'turn on for x minutes'

Post by jrich523 » Wednesday 28 December 2016 4:00

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
[ Domoticz 3.5196 | Win10 | AeoTec Z-Stick Gen5 ]

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests