Presence detection and welcome message - improvement

Based on my previous post:

Now this part is solved, but I would like to improve a bit when the welcome message is performed, and when not.

The idea is to use a geofence system (I will use Location via IFTTT on my iPhone) and a couple of new items. When I will go away I’ll use the switch “Andrea_Presence_IFTTT” triggered by IFTTT, in order to be sure I’m 100% away, and not around the house for whatever reason. When that switch is changed to OFF, a new global switch “Andrea_Presence” is changed also to OFF. This is my rule:

// Presence (Away) IFTTT out

rule "Set Away"
when
    Member of gPIFTTT changed to OFF
then
    val setAway = gPIFTTT.members.filter[ a | a.state == OFF && a.name == triggeringItem.name].map[ name.replace("_Presence_IFTTT", "") ]

    switch setAway {
        case setAway.get(0) == "Andrea": sendCommand(Andrea_Presence, OFF)
        case setAway.get(0) == "Cate": sendCommand(Cate_Presence, OFF)
    }
end

I’m using a switch method because I will have more than 2 people involved.

The second part is more tricky and complicated for me. I would like to use my Unifi Binding as the wifi coverage is exactly mapping my house. The idea is: when someone comes to home, the wifi join will trigger the rule (switch “Andrea_Phone_Home_Unifi_online”) and IF the global switch “Andrea_Presence” is OFF (and not ON -> to avoid to receive a welcome message if I’m already at home but with my phone off) also a welcome message will be performed.

Let me resume the overall rule:

// Presence (Home) Unifi in

rule "Welcome home my dear"
when
    Member of gPhones changed to ON
then
    if(cameHomeTimer !== null) {
        return; // do nothing if there is already a Timer
    }
    //logInfo("test", "Rule triggered by " + triggeringItem.name + " at " + now.toString)
    cameHomeTimer = createTimer(now.plusSeconds(5), [ |

        val cameHome = gPhones.members.filter[ p | p.state == ON && (p.name == triggeringItem.name || (p.lastUpdate !== null && p.lastUpdate.isAfter(now.minusSeconds(6)))) ].map[ name.replace("_Phone_Home_Unifi_online", "") ]

        //gPhones.members.forEach[ p | logInfo("test", "Values for " + p.name + "\n" + "State = " + p.state.toString + "\n" + "LastUpdate = " + p.lastUpdate.toString)]

        var message = "Benvenuti a casa, "

        switch(cameHome.size) {
            case 0: {
                logError("came home", "Unexpected number of phones: " + cameHome.size)
                return;
            }
            case cameHome.size == 1 && cameHome.get(0) == "Andrea": message = "Benvenuto a casa, Andrea"
            case cameHome.size == 1 && cameHome.get(0) == "Cate": message = "Benvenuta a casa, Caterina"
            case cameHome.size == 1 && cameHome.get(0) == "Sofia": message = "Benvenuta a casa, Sofia"
            case cameHome.size == 1 && cameHome.get(0) == "Edo": message = "Benvenuto a casa, Edoardo"
            default: {
                message = message + cameHome.members.reduce[ String list, String name | list + ", " + name ]
                val ind = message.lastIndexOf(",")
                message = new StringBuilder(str).replace(ind, ind+1," e").toString();
            }
        }
        //logInfo("check message", "message is: " + message)

        Echo_Living_Room_TTS_Volume.sendCommand('90')
        Echo_Living_Room_TTS.sendCommand('<speak><break time="2s"/>' + message + '</speak>')
        cameHomeTimer = null
    ])
end

The problem is at the end. I need to find a way to say (like val cameHome):

  • if cameHome is eg. Andrea, and the item “‘cameHome’_Presence” is OFF, play the message and set at the end the item “‘cameHome’_Presence” to ON
  • the same with other people
  • if there are more than 1 coming in, do the same with all names in the list

Any suggestion?
Thanks

Andrea

1 Like

You really already have all the parts you need to do this already. Use a Group and find the associated _Presence Item for each member of cameHome. Then check which ones are ON and build up the message as desired similar to the current switch statement.

2 Likes