Presence rule

Hi All

I thought this would be straight forward but seems im missing something.

I’m trying to get Alexa to say something when my Mobile goes online (as in i’ve arrived at home) and i’ve walked past the motion sensors (not point saying welcome, unless one of the two motion sensors trip) which indicates im close to the Alexa dot

Any thoughts?


rule "Play Welcome Message when Kris enters the House"
when
        Item Kris_Mobile changed to ON
then
        if (BackEveMotionAlarm == ON || FibaroEye1Motion == ON) {
         logInfo("Mobile Presense", "Kris has entered the Home")
          Echo_Living_Room_TTS.sendCommand('Welcome Home Kris')
}
end

What is the control factor for your Kris_Mobile item?

For example if you are using the iCloud binding for this it takes time for the data to update. Might also be helpful to do multiple logInfo lines to know if any part of the then statement is triggering.

With the if statement using or then Alexa should respond when you mobile go online. If your not near the echo you may not hear the message. You can change the or (||) to and (&&) so that both conditions must be met before the message is sent. I use Alexa to notify me if there is movement in my garage, when the door is open, and it works good. I also included motion detection suspension for going in the garage without Alexa alerting every 15 seconds.:wink: Here’s an example of the rule I use, maybe it can help give you some ideas.

var Timer stopMotionTimer = null
var Timer startMotionTimer = null
var Timer startLightTimer = null
val int timeoutMinutes = 60
rule "Detect Motion when Garage is Open"
when
    Item Esp_Easy_Motion changed from OFF 
then
    if(Proxy_Motion.state == ON && stopMotionTimer === null && ESP_Easy_Door.state == OFF){  // Door open = OFF
        Echo_Plus_TTS.sendCommand('Alert, garage movement!')
            stopMotionTimer = createTimer(now.plusSeconds(15)) [|
                stopMotionTimer = null
            ]
    }
end

rule "Auto ON Garage Motion Detection after One Hour when Garage is Open"
when
    Item Proxy_Motion changed from ON to OFF
then
    if(startMotionTimer === null && Proxy_Motion.state == OFF){
        Echo_Plus_TTS.sendCommand('The garage motion suspended for one hour')
            startMotionTimer = createTimer(now.plusMinutes(60)) [|
                Proxy_Motion.sendCommand(ON)
                Echo_Plus_TTS.sendCommand('Garage motion is now on')
                startMotionTimer = null
            ]  
    }
end
rule "Garage Light Detection"
when
    Item Esp_Garage_Lightlevel received update
then
    val Val_Light = Esp_Garage_Lightlevel.state as Number 
    if(Val_Light >= 650 && ESP_Easy_Door.state == ON && startLightTimer === null){
        startLightTimer = createTimer(now.plusSeconds(190)) [|
            Echo_Plus_TTS.sendCommand('The garage door is closed, and light has been left on')
            startLightTimer = null
        ]
    }
    if(Val_Light <= 500){
        Proxy_Lightlevel.postUpdate(ON)
    }
end
1 Like

I think the fundamental problem is you can’t really control the order of the events. There can be times where the BackEveMotionAlarm goes to ON or FibaroEye1Motion goes to ON after Kris_Mobile changes to ON. In that case the Alexa will never say the greeting.

So I think you need to trigger the Rule on any of these three events taking place and then send the message to talk if one of the motion sensors is ON and Kris_Mobile is ON and they both turned on within a certain amount of time ago.

Also, you need to check the state of the Items in your if statement.

The below requires persistence on Kris_Mobile configured with everyChange, not everyMinute.

rule "Play Welcome Message when Kris enters the House"
when
    Item Kris_Mobile changed to ON or
    Item BackEveMotionAlarm changed to ON or
    Item FibaroEye1Motion changed to ON
then
    if(Kris_Mobile.state != ON) return;
    if(BackEyeMotionAlarm.state != ON || FibaroEye1Motion.state != ON) return;

    val lu = new DateTime(Kris_Mobile.lastUpdate.toString)
    if(lu.isAfter(now.minusSeconds(20)) {
        logInfo("Mobile Presense", "Kris has entered the Home")
        Echo_Living_Room_TTS.sendCommand('Welcome Home Kris')
    }
end
1 Like

That should do it, except &&

if(BackEyeMotionAlarm.state != ON && FibaroEye1Motion.state != ON) return;

I’m doing pretty much the same but in jsr223 :grinning:

1 Like

Thanks Rich, i had an error using that rule

I think it was missing a bracket, line 11. I shall give it a go!

Hi @rlkoshak

ive started to test this rule but I get this error in the log, seems date/time related?


13:28:28.736 [WARN ] [ence.extensions.PersistenceExtensions] - There is no default persistence service configured!
13:28:28.738 [WARN ] [ence.extensions.PersistenceExtensions] - There is no queryable persistence service registered with the id 'null'
13:28:28.739 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'Play Welcome Message when Kris enters the House': cannot invoke method public java.lang.String org.joda.time.base.AbstractInstant.toString() on null

rule:


rule "Play Welcome Message when Kris enters the House"
when
    Item Kris_Mobile changed to ON or
    Item BackEveMotionAlarm changed to ON or
    Item FibaroEye1Motion changed to ON
then
    if(Kris_Mobile.state != ON) return;
    if(BackEveMotionAlarm.state != ON && FibaroEye1Motion.state != ON) return;

    val lu = new DateTime(Kris_Mobile.lastUpdate.toString)
    if(lu.isAfter(now.minusSeconds(20))) {
        logInfo("Mobile Presense", "Kris has entered the Home")
        Echo_Living_Room_TTS.sendCommand('Welcome Home Kris')
    }
end

Kris_Mobile is not being saved to persistence. You must set up persistence and configure it to save Kris_Mobile on every change in order for the Rule to work. The error is telling us that Kris_Mobile.lastUpdate is returning null and null doesn’t have a toString method.

Hi Rich

But mapdb has this configuration:

kris@openhab2:/etc/openhab2/persistence$ cat mapdb.persist

Strategies {
    everyHour   : "0 0 * * * ?"
    everyDay    : "0 0 0 * * ?"

    default = everyChange
}

Items {
    * : strategy = everyChange, EveryDay, restoreOnStartup
}

Not on my server to verify but I think everyMinute needs to be added to the item strategy.

The EveryDay might be causing problems. You defined it as everyDay. Do you see errors when OH loads this file?

everyMinute is only needed for rrd4j and would completely break what we need for the above Rule to work.

I knew it was needed on one of the services. :thinking: Just couldn’t remember the exact one.
Im sure missing my laptop about now.:pensive:

Hi Rich ive had no issues with persistence on any item or loading the file. Ive updated it to everyChange anyway