why this works and this not??  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
Gianni
Posts: 230
Joined: Saturday 21 July 2018 19:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Home@Belgium
Contact:

why this works and this not??

Post by Gianni »

made a script on my motion detector.

Code: Select all

return {
    	active = true,
	on = {
		devices = {
			'Motion_Boven'
		}
	},
	execute = function(domoticz, sensorboven)
	    local lichtoverloop = domoticz.devices('Licht_Overloop')
			    if (sensorboven.state == 'On'  and domoticz.time.matchesRule('between sunset and sunrise') ) then
	                lichtoverloop.switchOn().forMin(1)
	end
end
}
My sensor get a off trigger after 30 sec
with this code the light stay on and sometimes it's go off after 1 min so it's random

Code: Select all

return {
    	active = true,
	on = {
		devices = {
			'Motion_Boven'
		}
	},
	execute = function(domoticz, sensorboven)
	    local lichtoverloop = domoticz.devices('Licht_Overloop')
			    if (sensorboven.state == 'On'  and domoticz.time.matchesRule('between sunset and sunrise') ) then
	                lichtoverloop.switchOn()
		else
			lichtoverloop.switchOff()
	end
end
}
with this code of the senor get the off trigger the switch go out so thats good.
But why stay the switch on even if i say forMin(1)?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: why this works and this not??

Post by waaren »

Gianni wrote: Tuesday 06 August 2019 1:32 Why stay the switch on even if i say forMin(1)?
From the dzVents wiki
Important note when using forXXX(): Let's say you have a light that is triggered by a motion detector. Currently the light is Off and you do this: light.switchOn().forMin(5). What happens inside Domoticz is this: at t0 Domoticz issues the switchOn() command and schedules a command to restore the current state at t5 which is Off. So at t5 it will switch the light off.

If, however, before the scheduled switchOff() happens at t5, new motion is detected and you send this command again at t2 then something unpredictable may seem to happen: the light is never turned off! This is what happens:

At t2 Domoticz receives the switchOn().forMin(5) command again. It sees a scheduled command at t5 and deletes that command (it is within the new interval). Then Domoticz performs the (unnecessary, it's already on) switchOn() command. Then it checks the current state of the light which is On!! and schedules a command to return to that state at t2+5=t7. So, at t7 the light is switched on again. And there you have it: the light is not switched off and never will be because future commands will always be checked against the current on-state.

That's just how it works and you will have to deal with it in your script.

my preference for dealing with this is to use the cancelQueuedCommand() method.

Code: Select all

return 
{
    active = true,
    
    on = { devices = { 'Motion_Boven' }},
    
    execute = function(domoticz, sensorboven)
        local lichtoverloop = domoticz.devices('Licht_Overloop')
        if sensorboven.active and domoticz.time.matchesRule('between sunset and sunrise') then
            lichtoverloop.cancelQueuedCommands()
            lichtoverloop.switchOn().checkFirst()
            lichtoverloop.switchOff().afterSec(60)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Gianni
Posts: 230
Joined: Saturday 21 July 2018 19:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Home@Belgium
Contact:

Re: why this works and this not??

Post by Gianni »

hey Waaren

I read this before thats why i set the re trigger of my sensor at 30 sec.
I tested yesterday and i'm sure i dont get a second trigger in the timelap.
But it's stay on.
The funny thing is that before everything worked with the same blockly.

if sensor == on and time between sun and sun
set light on for 1 min

i will try to understand your code so i can use it in the future

why did you cancel de queued commands from lichtoverloop.
It's the sensor wwho got the queued command or did i see this wrong.
The light is just an end device?

Code: Select all

 lichtoverloop.cancelQueuedCommands()
grts
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: why this works and this not??  [Solved]

Post by waaren »

Gianni wrote: Tuesday 06 August 2019 18:04 ... i set the re trigger of my sensor at 30 sec.
What do you mean with re-trigger ? Is my assumption correct that it will re-send an off status after 30 seconds and a new on status if motion is detected after this 30 seconds ?
why did you cancel de queued commands from lichtoverloop.
It's the sensor wwho got the queued command or did i see this wrong.
The light is just an end device?

Code: Select all

 lichtoverloop.cancelQueuedCommands()
No dVents does not send anything to the sensor. The scheduled command is the switchOff().afterSec(60) and that switchOff() is canceled when a new motion == On triggered the script. So every new motion == On will ensure the light will stay On for another 60 seconds. I use this sequence of commands for all my motion sensors and it does the job.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
beamzer
Posts: 19
Joined: Sunday 17 August 2014 20:18
Target OS: Raspberry Pi / ODroid
Domoticz version: stable
Contact:

Re: why this works and this not??

Post by beamzer »

Another way to deal with it would be to include a check on the light status before switching it On:

Code: Select all

if sensorboven.active and domoticz.time.matchesRule('between sunset and sunrise') and (lichtoverloop.state == 'Off') then
	lichtoverloop.switchOn().forMin(1)
end
Domoticz stable on a Raspberry Pi 3
RFXtrx433E - RFLink - Smartmeter (through RasPi-Zero-W) - Hue - BMP180 - DHT22 - Wemos D1 Mini's with PIR
Gianni
Posts: 230
Joined: Saturday 21 July 2018 19:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Home@Belgium
Contact:

Re: why this works and this not??

Post by Gianni »

thx is try the 2 and it's working like i want now :-)
But what i dont understand about the wiki part is that i not did a second trigger but still stay on.
So i triggered the sensor and leave the room.
then i checked domticicz if it's go off.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: why this works and this not??

Post by waaren »

beamzer wrote:Another way to deal with it would be to include a check on the light status before switching it On:
This might not work as you would expect if the light is scheduled to go On / Off with afterXXX because the forXXX will check current state and return to that state.


Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
beamzer
Posts: 19
Joined: Sunday 17 August 2014 20:18
Target OS: Raspberry Pi / ODroid
Domoticz version: stable
Contact:

Re: why this works and this not??

Post by beamzer »

waaren wrote: Sunday 11 August 2019 12:27 ...
This might not work as you would expect if the light is scheduled to go On / Off with afterXXX because the forXXX will check current state and return to that state.
Ok, but it should work with the supplied example using forMin(x). If the light is already On, it won't switch it On again. If it's Off the forMin timer has expired and it can safely be triggered again. Agree?

The advantage of the way you mentioned is that it handles repeated triggers while the light is on more gracefully, because the light stays on for the configured time after the last trigger. In my example with an On time for 60 seconds, if there is a second trigger after 55 seconds, the light will still switch Off 5s after that second trigger.
Domoticz stable on a Raspberry Pi 3
RFXtrx433E - RFLink - Smartmeter (through RasPi-Zero-W) - Hue - BMP180 - DHT22 - Wemos D1 Mini's with PIR
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest