How do i check for days in an IF statement?

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:

How do i check for days in an IF statement?

Post 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!
SweetPants

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

Post by SweetPants »

use dzVentz, very easy with:

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

check the Wiki https://www.domoticz.com/wiki/DzVents:_ ... _scripting
rizzah
Posts: 75
Joined: Monday 17 October 2016 16:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Groningen
Contact:

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

Post 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.
SweetPants

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

Post by SweetPants »

You can have multiple 'on timer' events and select them using triggerinfo.trigger

https://www.domoticz.com/wiki/DzVents:_ ... 29_..._end
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

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

Post 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.
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: How do i check for days in an IF statement?

Post by rizzah »

Awesome, thanks!
rizzah
Posts: 75
Joined: Monday 17 October 2016 16:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Groningen
Contact:

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

Post 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
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

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

Post 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.
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: How do i check for days in an IF statement?

Post 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!
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

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

Post 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.
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: How do i check for days in an IF statement?

Post 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,
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

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

Post 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.
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: How do i check for days in an IF statement?

Post 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!
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

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

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest