Page 1 of 1

How do i check for days in an IF statement?

Posted: Saturday 30 December 2017 13:08
by rizzah
Hi,

I got this script which which works fine, cept that i only want it to check certain if statement on monday till friday.

Right now my if statement looks like this:

Code: Select all

if (domoticz.devices('Sensor_SK_PenM').state == 'On') and (domoticz.devices('Lamp_SK_PenM').state == 'Off') and (domoticz.devices('Lux_SK_PenM').lux <= 20) then
But i would like to add something like "and days between mon-fri" but i dont know what the right syntax is for this.

Any help would be appriciated!

Re: How do i check for days in an IF statement?

Posted: Saturday 30 December 2017 13:23
by SweetPants
use dzVentz, very easy with:

on = {
timer = { mon, thu, wed, thu, fri }
}

check the Wiki https://www.domoticz.com/wiki/DzVents:_ ... _scripting

Re: How do i check for days in an IF statement?

Posted: Saturday 30 December 2017 13:27
by rizzah
I saw it could be done with a timer, however, this is my code now:

Code: Select all

            if (domoticz.devices('Sensor_SK_PenM').state == 'On') and (domoticz.devices('Lamp_SK_PenM').state == 'Off') and (domoticz.devices('Lux_SK_PenM').lux <= 20) then
                domoticz.log(domoticz.time.rawTime)
                if (domoticz.time.hour >= 8) and (domoticz.time.hour <= 21) then
                    domoticz.scenes('Lamp_SK_PenM_Overdag').switchOn()
                    domoticz.data.counter = 0
                    domoticz.log('<font color="purple"> [DEVICE] Motion sensor Kamer PenM is aan, Lamp is aangezet (routine 8-20)')
                elseif (domoticz.time.hour >= 22) and (domoticz.time.hour <= 22) and (domoticz.time.min <= 30) then
                    domoticz.scenes('Lamp_SK_PenM_Avond').switchOn()
                    domoticz.log('<font color="purple"> [DEVICE] Motion sensor Kamer PenM is aan, Lamp is aangezet  (routine 22-23)')                    
                end
I want to split this code up so the "on" switch from 8 till 21 will happen only on mon-fri and i want to create a different one for example 10-21 on sat-sun.

Re: How do i check for days in an IF statement?

Posted: Saturday 30 December 2017 13:33
by SweetPants
You can have multiple 'on timer' events and select them using triggerinfo.trigger

https://www.domoticz.com/wiki/DzVents:_ ... 29_..._end

Re: How do i check for days in an IF statement?

Posted: Wednesday 03 January 2018 8:57
by dannybloe
You can check for a day by using the matchesRule function on a Time object:

Code: Select all

if (
	domoticz.devices('Sensor_SK_PenM').state == 'On' and 
	domoticz.devices('Lamp_SK_PenM').state == 'Off' and 
	domoticz.devices('Lux_SK_PenM').lux <= 20 and
	domoticz.time.matchesRule('on mon,fri,sat,sun')) then -- doesn't work with 'between'
	---
end
You can use the same time rules as you can with the timer trigger.

Re: How do i check for days in an IF statement?

Posted: Wednesday 03 January 2018 9:23
by rizzah
Awesome, thanks!

Re: How do i check for days in an IF statement?

Posted: Sunday 14 January 2018 10:20
by rizzah
@dannybloe

i have this code now:

Code: Select all

return {
    active = true, 
    on = {
        devices = {
            'Sensor_SK_PenM'
        },
        timer = {
            'every minute on mon,tue,wed,thu,fri'
        },
    },
    data = {
        counter = { initial = 0 }
    },
 
    execute = function(domoticz, device)
 
            if (domoticz.devices('Sensor_SK_PenM').state == 'On') and (domoticz.devices('Lamp_SK_PenM').state == 'Off') and (domoticz.devices('Lux_SK_PenM').lux <= 20) then
                domoticz.log(domoticz.time.rawTime)
                if (domoticz.time.hour >= 8) and (domoticz.time.hour <= 21) then
                    domoticz.scenes('Lamp_SK_PenM_Overdag').switchOn()
                    domoticz.data.counter = 0
                    domoticz.log('<font color="purple"> [DEVICE] Motion sensor Kamer PenM is aan, Lamp is aangezet (routine 8-21)')
                elseif (domoticz.time.hour >= 22) and (domoticz.time.hour <= 22) and (domoticz.time.min <= 30) then
                    domoticz.scenes('Lamp_SK_PenM_Avond').switchOn()
                    domoticz.log('<font color="purple"> [DEVICE] Motion sensor Kamer PenM is aan, Lamp is aangezet  (routine 22-23)')                    
                end
But i notice the lights also go on saturday and sunday. Is that correct? since i use only weekdays in the timer?

Regards,
Peter

Re: How do i check for days in an IF statement?

Posted: Sunday 14 January 2018 11:04
by dannybloe
Well, you can check the trigger info (third parameter to the execute function) and see if the reason was timer or a device. See documentation. Thing is that in your script when Sensor_SK_PenM changes, your script is triggered as well. So if your if-statement renders true then you have things happening on every day.

So, there are several things you can do. You can check the triggerInfo parameter as mentioned above or you can put a day-check in your execute function or add a rule to the device trigger:

Code: Select all

return {
    active = true, 
    on = {
        devices = {
            'Sensor_SK_PenM' = { 'on mon, tue, wed, thu, fri' }
        },
        timer = {
            'every minute on mon,tue,wed,thu,fri'
        },
    },
...
Now changes in Sensor_SK_PenM will only trigger the execute function on weekdays.

Unless of course there's a bug in the timer rule code but first check if anything mentioned above is going on.

Re: How do i check for days in an IF statement?

Posted: Sunday 14 January 2018 11:11
by rizzah
I will try the above method with the devices = first.
Could it be it has to be : 'Sensor_SK_PenM' == { 'on mon, tue, wed, thu, fri' } instead of a single = ? I got an error when i tried to use a single = sign.

Thank you for your responce!

Re: How do i check for days in an IF statement?

Posted: Sunday 14 January 2018 11:14
by dannybloe
No, it is not a comparision but a declaration. 'devices' inside the 'on'-section is a Lua table where each member is either a singleton (device name or id) or a key/value item (where in this case key is 'Sensor_SK_PenM' and value is again a Lua table (array) with timer rules. There can be more rules in that table.

Re: How do i check for days in an IF statement?

Posted: Sunday 14 January 2018 11:52
by rizzah
Would you help me with the error im getting when using a single sign?

Code: Select all

return {
    active = true, 
    on = {
        devices = {
            'Sensor_SK_PenM' = { 'on mon, tue, wed, thu, fri' }
        },
        timer = {
            'every minute on mon,tue,wed,thu,fri'
        },
    },
    data = {
        counter = { initial = 0 }
    },
 
    execute = function(domoticz, device)
Im getting the following error on line 9: '}' expected near '='

Regards,

Re: How do i check for days in an IF statement?

Posted: Sunday 14 January 2018 11:56
by dannybloe
Ow crap, I always forget those nasty [ ]:

Code: Select all

devices = {
           ['Sensor_SK_PenM'] = { 'on mon, tue, wed, thu, fri' }
        },
Lua is weird.

Re: How do i check for days in an IF statement?

Posted: Sunday 14 January 2018 12:01
by rizzah
Im trying to learn python atm to get a better feeling with things. Lua did not have much good teaching on udemy :-)

Thanks alot for your help!

Re: How do i check for days in an IF statement?

Posted: Sunday 14 January 2018 12:06
by dannybloe
Ah, it's easy to learn and very similar to javascript. You just have to learn a couple of syntactic difference and read about Lua tables. Not so hard. There's no dzVents data model in python though.