Auto arm homealarm if devices are connected to specific AP at specific time or if connectes after that time

Hi,

I’m using Unifi device tracker and from there I can see which AP’s mac address our devices are connected to, this is shown in HA as “Attribute”
For example:
Entity: device_tracker.phone1
Attribute: ap_mac: xx:xx:xx:xx:xx

What I want to automate is when mine and my wifes phones are connected to our bedroom AP, then our alarm should “Arm Home”, but with the time condition “after 9pm”
But in the same time, if we already are connected to the bedromm AP before 9pm and the clock hits 9pm it should “Arm Home” at 9pm.

I guess I need two triggers, one that is time, and one that is template reading the Attributes.
Also I assume I need two conditions, template for the attributes AND time.

The trigger and condition with time is easy, but could somone help me with the template for the mac address?

To sum it up:

Trigger:
Phone 1 and 2: Connected to AP with MAC: BE:DR:OO:M0:01
OR
Time after: 21:00

Condition:
Phone 1 and 2: Connected to AP with MAC: BE:DR:OO:M0:01
AND
Time after: 21:00

Action:
Arm Home and maybe turn off all lights?

Thinking about it, maybe we should have some extra safety, maybe no motion for x minutes in our hallway before triggering the action? Bayesian Binary Sensor maybe is a better approach?
Anyone have some ready automation doing something similiar?

I’d recommend using appdaemon - it’s surprisingly difficult to get multi-stage conditions like this to work reliably. appdaemon makes it MUCH (like infinitely) easier to debug these kinds of automations. Here’s my automation for turning on some lights when either my wife or I are away for a certain amount of time, it’s after the sun is down, and to turn them back off when we arrive home or after a time. Might give you some ideas.

1 Like

Overall, I think you mapped it out pretty nicely already - and I don’t think it is actually that complicated.

First step would be to create a template sensor for either phone that turns true if the respective phone is logged onto the bedroom AP.

- platform: template
  sensors:
    his_phone_on_bedroom_ap:
      value_template: >
        {% if is_state("device_tracker.his_phone", "home") %}
          {% if is_state_attr("device_tracker.his_phone", "ap_mac", "BE:DR:OO:M0:01") %} 
            true
          {% else %}
            false
          {% endif %}
        {% else %}
          Offline
        {% endif %}

Then use them as triggers, as well as the time condition - keep in mind that triggers are always treated as OR, so simply listing them would be enough:

- alias: 'Turn Off All Lights And Arm Home Alarm'
  trigger:
    - platform: state
      entity_id: sensor.his_phone_on_bedroom_ap
      to: 'true'
    - platform: state
      entity_id: sensor.her_phone_on_bedroom_ap
      to: 'true'
    - platform: time
      at: '21:00:00'
  condition: 
    - condition: state
      entity_id: sensor.his_phone_on_bedroom_ap
      state: 'true'
    - condition: state
      entity_id: sensor.her_phone_on_bedroom_ap
      state: 'true'
    - platform: time
      after: '21:00:00'
  action:
    - service: light.turn_off
      group: group.all_lights
    - service: alarm.arm_home
      entity_id: alarm.not_sure_what_it_is_called

Conditions are always treated as AND so the simple list should be fine there as well.

Not sure about the action you want to carry out - I don’t manage my alarm through HA.

n.b.:
This is based partly on some code I’m using - not sure if it works out of the box.

Let me know how it goes!

1 Like

Hi both and sorry for my long absence, life came in the way so I have not had any time to play with my install.
@TD22057 Regarding appdaemon, well, I just started with HA and ESPHome so I think i need to get a little more comfortable with this before I introduce another path :slight_smile:

@chairstacker I will definitely try this out but will probably have to add some more condition to avoid false triggering.
Sometimes both phones are in the bedroom and charging before we actually go to bed.
Do you have any good ideas for more conditions?
I have some motion sensors, my TV in the living room and more connected to HA so maybe a Bayesian Binary Sensor can help anyway. I just have hard time figure out some good logic.