[SOLVED] Change in rules syntax? (changedSince)

Hi all,

until recently I used the following to check the presence state at my home:

Group   Mobiles
Switch  Presence
Switch  Mobile1 "Mobile1"        <network>    (Mobiles)    { channel="network:device:192_168_10_1:online" }
Switch  Mobile2 "Mobile2"        <network>    (Mobiles)    { channel="network:device:192_168_10_2:online" }
rule "Periodically check presence of mobiles"
when
    Time cron "*/10 * * * * ?"
then
    if (Presence.state == ON)
    {
        if(Mobiles.members.filter(s | s.state == ON).size == 0) {
            logInfo("PresenceCheck", "No phone within reach, checking for flapping")
            if(Mobiles.members.filter(s | s.changedSince(now.minusSeconds(120))).size == 0) {
                logInfo("PresenceCheck", "Nobody is at home, waited for 2 minutes")
                sendCommand(Presence, OFF)
            }
        }
    }
    else
    {
        //For initialisation. If Presence is undefined or off, although it should be on.
        if(Mobiles.members.filter(s | s.state == ON).size > 0) {
            sendCommand(Presence, ON)
        }
    }
end

What’s currently not working anymore is the flapping check…

if(Mobiles.members.filter(s | s.changedSince(now.minusSeconds(120))).size == 0) 

I updated to the latest snapshot yesterday, now if the last device goes offline it immediately triggers the Presence -> OFF and isn’t checking for 120 seconds anymore. This worked with the snapshot from early December. Did anything change concerning this method?

Regards, Christian

i had similar issues, so i redesigned the presence checks (i think that is easier and does work well):

Group:Switch:OR(ON, OFF) group_presence “Gruppe zur Anwesenheitserkennung”

in this group, only switches are allowed - and a single “on” switches the whole group item to “on”.

rule "Zyklische Anwesenheitserkennung"
when
    Time cron "0 */5 * * * ?"
then
    if (presence.state == ON) {
        if (group_presence.state == OFF) {
			if (!group_presence.changedSince(now.minusMinutes(5)) && ct_flur_wohnungstuer.changedSince(now.minusMinutes(15))) sendCommand(presence, OFF)
        }
    }
    else {
        if (group_presence.state == ON) sendCommand(presence, ON)
        else if (presence.state == NULL) sendCommand(presence, OFF)
    }
end

rule "Eventbasierte Anwesenheitserkennung"
when
	Item group_presence changed from OFF to ON
then
    if (presence.state != ON) sendCommand(presence, ON)
end

you can ignore the ct_flur_wohnungstuer, this is only an addition when you have a door contact at the main door.

This may not be the answer you wanted, because you want to know why the behaviour changed - i discovered the flapping issue when i changed to OH2 from OH1 and helped myself like this. Maybe someone else can point you what changed exactly.

Hi Stefan,

thanks for the tip, that,'s much easier than calculating the group members :slight_smile:
Unfortunately this doesn’t change my initial problem: the flapping detection doesn’t work… the

if (!itemname.changedSince(now.minusMinutes(2)))

doesn’t lead to waiting 2 minutes, it instantly continues and switches off the presence switch in my case. This worked perfectly in the past…

Christian

okay, i’m not sure if i understand your problem right:

You check for presence every 10minutes. Your “flapping detection” is 2 Minutes without a change, so your presence should be set to off after minimum 02:00min and maximum 11:59min - Right?
So how can a device change the presence off immediately? Or do you mean the statement

if (!itemname.changedSince(now.minusMinutes(2)))

is always true? if yes - did you confirm this via loginfo?

No, I’m checking every 10 seconds. After turning off the last group member the rule should check if the group’s current state doesn’t change in the next 2 minutes (to avoid flapping mobiles i.e.), and only then proceed with turning off my presence switch.

The longer I look at my rules the more confused I get :confused:

sorry, my mistake - yes, you check every 10seconds.

so if the last switch-member of your group changed to off, the whole group changes to off.
Now you check if the presence is on, the group item is off and hasn’t changed for 2 Minutes:

if (presence.state == ON) {
        if (group_presence.state == OFF) {
		if (!group_presence.changedSince(now.minusMinutes(2))) sendCommand(presence, OFF)
        }
}

so your presence should be set to off at a minimum of 2min and a maximum of 2min10sec.

But it changes to off before that 2mins?

Yes, it changes immediately.
Are you running on the latest snapshot? Then you could try it yourself. I’ll revert to a release from the 20th or so to check if it works there later.

I put this at the beginning of my rule:

println(Networkdevices.changedSince(now.minusSeconds(20)))

This prints “false” in the console every 2 seconds (changed the cron entry to every 2 seconds for debugging). When I switch my mobile off the state definitely changes to OFF, but the “false” continues to show in the log:

17:13:00.103 [INFO ] [ome.event.GroupItemStateChangedEvent] - Networkdevices changed from ON to OFF through Chrissi
17:13:00.103 [INFO ] [marthome.event.ItemStateChangedEvent] - Chrissi changed from ON to OFF
false
17:13:02.000 [INFO ] [smarthome.model.script.PresenceCheck] - No phone within reach, checking for flapping
false
17:13:04.001 [INFO ] [smarthome.model.script.PresenceCheck] - No phone within reach, checking for flapping
false
17:13:06.001 [INFO ] [smarthome.model.script.PresenceCheck] - No phone within reach, checking for flapping
false
17:13:08.000 [INFO ] [smarthome.model.script.PresenceCheck] - No phone within reach, checking for flapping
false

This should change to true, right?

No i’m not at the latest snapshot because of the problems with the serial transportation upgrade, i run snapshot #631 at the moment.

yes it should, but only if you have configured your persistence service every second or something like that, if “Networkdevices” is only “recorded” once a minute, this won’t work…

THAT’S IT!!! I switched from rrd4j to mapdb yesterday, and the mapdb persistence seems to have some problems. It’s default is set to commit changes every 5 seconds, but it doesn’t do that obviously… switched back to rrd4j and it started working immediately!!

Thanks a lot for this hint!

Christian

1 Like