Motion timer overrules dzvents

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

Moderator: leecollings

Post Reply
rizzah
Posts: 75
Joined: Monday 17 October 2016 16:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Groningen
Contact:

Motion timer overrules dzvents

Post by rizzah »

Hi,

I have this code which turns on lights in one of my kids bedroom when motion detected (i use Aeotec Multisensor 6). I configured the multisensor motion detection to 60 seconds. this part seems to work oke. I wanted to add a variable when time is between x and y that the lights should stay on for 5 minutes instead of the 60 seconds on no motion. I used the forMin(5) for that, but that does not seem to work. I should probably do something different but i have no clue what, can anyone help me in the right direction?

This is my code so far:

Code: Select all

return {
    active = true,  
    on = {
        devices = {
            'Sensor_SK_Jop'
        }
    },

    execute = function(domoticz, device)

        if (device.state == 'On') and (domoticz.devices('Lamp_SK_Jop').state == 'Off') and (domoticz.devices('Lux_SK_Jop').lux <= 27) then
            if (domoticz.time.hour >= 9) and (domoticz.time.hour <= 19) then
                domoticz.scenes('Lamp_SK_Jop_Overdag').switchOn()
                domoticz.log('Motion sensor Kamer Jop is aan, Lamp is aangezet op 100% met korte timer')


            elseif (domoticz.time.hour >= 19) and (domoticz.time.hour < 20) then
                domoticz.scenes('Lamp_SK_PenM_Overdag').switchOn().forMin(5)
                domoticz.log('Motion sensor Kamer Jop is aan, Lamp is aangezet op 100% met langere timer')

            else
                domoticz.devices('Lamp_SK_Jop').switchOff()
                domoticz.log('Motion sensor Kamer Jop is aan, Lamp is niet aangezet')
            end
            
        elseif (device.state == 'Off') and (domoticz.devices('Lamp_SK_Jop').state == 'On') then
                domoticz.devices('Lamp_SK_Jop').switchOff()      
                domoticz.log('Motion sensor Kamer Jop is uit, Lamp is uitgezet')
        end
    end
}
Thanks!
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Motion timer overrules dzvents

Post by dannybloe »

The thing is, if you have a switch command in the queue (afterMin(5)) and you issue a new command like switchOff() (in the else part of your outer if) then the schedule is cleared. So I guess what's happening is that you switch on Job's light for 5 minutes but then the motion detector kicks in again, doesn't know anything about the scheduled command and switches the light off. Which is logical I'd say. if you do a switchOff() it should go off at that moment.

So, you have to add some more intelligence in your script. What you can do is use a persistent variable (see the docs) and create a flag that indicates that you want the lights to be on for only 5 minutes. Add a timer rule to your script that when it fires (check the triggerInfo to see if the trigger was a timer (see docs)) it will check for this flag to be set and if the light is on, switch it off at that moment. The timer rule can be set to every minute and you can check the lastUpdate of the light. Also, when the sensor is triggered you have to check for this flag and not turn off the lights (because the timer will switch it off).

Something like that.

Intelligent PIR-light-activation always requires custom scripting as everybody wants it to behave just a tad different. It may require a couple of iterations and script adaptations before it is just right. I have a couple them in the house as well (of course, a little different than yours).
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
rizzah
Posts: 75
Joined: Monday 17 October 2016 16:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Groningen
Contact:

Re: Motion timer overrules dzvents

Post by rizzah »

Thank you for your reply @dannybloe.

It makes some sence, but for someone who doesnt know scripting very well its quite complicated to understand, but i will read into the documentation which you are referring to.

If you would/could share some examples which i can learn from that would be very helpfull.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Motion timer overrules dzvents

Post by dannybloe »

It shouldn't be that hard actually. But that's easily said for someone who programs for 30+ years.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
rizzah
Posts: 75
Joined: Monday 17 October 2016 16:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Groningen
Contact:

Re: Motion timer overrules dzvents

Post by rizzah »

I have spend like a whole weekend on this script, i think its working now, but im pretty sure my logic is not the best or the script can be simplified or be written better then i have now. If anyone would like to help me with this i would really appreciate that.

Code: Select all

return {
    active = true,  
    on = {
        devices = {
            'Sensor_SK_Jop'
        },
        timer = {
            'every minute'
        },
    },
    data = {
        counter = { initial = 0 }
    },

    execute = function(domoticz, device, triggerInfo)

        if (triggerInfo.type == domoticz.EVENT_TYPE_DEVICE) then
            if (domoticz.devices('Sensor_SK_Jop').state == 'On') and (domoticz.devices('Lamp_SK_Jop').state == 'Off') and (domoticz.devices('Lux_SK_Jop').lux <= 27) then
                if (domoticz.time.hour >= 8) and (domoticz.time.hour <= 20) then
                    domoticz.data.counter = 0
                    domoticz.scenes('Lamp_SK_Jop_Overdag').switchOn()
                    domoticz.log('<font color="purple"> [DEVICE] Motion sensor Kamer Jop is aan, Lamp is aangezet')
                else
                    domoticz.devices('Lamp_SK_Jop').switchOff()
                    domoticz.log('<font color="purple"> [DEVICE] Motion sensor Kamer Jop is aan, Lamp is niet aangezet')    
                end
            elseif (domoticz.devices('Sensor_SK_Jop').state == 'Off') and (domoticz.devices('Lamp_SK_Jop').state == 'On') then
                if (domoticz.time.hour >= 11) and (domoticz.time.hour < 19) then
                    domoticz.devices('Lamp_SK_Jop').switchOff()
                    domoticz.log('<font color="purple"> [DEVICE] Lamp is uitgezet')
                end
            end
        end
    
        if (triggerInfo.type == domoticz.EVENT_TYPE_TIMER) then  
            if (domoticz.devices('Sensor_SK_Jop').state == 'Off') and (domoticz.devices('Lamp_SK_Jop').state == 'On') then
                if (domoticz.time.hour >= 9) and (domoticz.time.hour <= 20) then
                    if (domoticz.data.counter == 3) then
                        domoticz.data.counter = 0
                        domoticz.devices('Lamp_SK_Jop').switchOff()
                        domoticz.log('<font color="purple"> [TIMER] Counter staat op 3, deze is nu gereset, Counter is nu weer: ' ..domoticz.data.counter)
                        domoticz.log('<font color="purple"> [TIMER] Lamp Jop is uitgezet')
                    else
                        domoticz.scenes('Lamp_SK_Jop_Overdag').switchOn()
                        domoticz.data.counter = domoticz.data.counter + 1
                        domoticz.log('<font color="purple"> [TIMER] Timer nog niet op 3, timer is nu: ' .. domoticz.data.counter)
                        domoticz.log('<font color="purple"> [TIMER] Motion sensor Kamer Jop is uit, Lamp is aangezet voor 5 minuten')                            
                    end
                end
            end
        end
    end
}
rizzah
Posts: 75
Joined: Monday 17 October 2016 16:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Groningen
Contact:

Re: Motion timer overrules dzvents

Post by rizzah »

Ok, i ran into this issue last night. I am curious if this could be fixed somehow.

In the room i also have a Hue Tap for enabling or disabling the light (mostly as a backup in case the lights need to be turned or off in case there is something going on outside the rules set for the light). But now it messing up the rules i set for it when someone pressed the light button.

The Hue Tap is not a device in domoticz, so you cant really control it. Question is, can i somehow fix this in the code so it knows the light has not been turned on by either the motion detector or the timer in place? How would i go around this?
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Motion timer overrules dzvents

Post by dannybloe »

If Domoticz doesn't know about this then surely your event script don't know about this either. Gonna be hard.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest