Device tracker - first home, last home

I use my device tracker with the Manual Alarm Control Panel to turn the alarm on/off.

Is it possible the make a notification that notifies what device turned the alarm off (first device with state “home”) and what device was the last to leave (last device with state “home”)?

Does anyone use this or have any tips?

dk 8123-2017-10-19-17-34-06

Yes it’s possible. Can be done in an automation. You would need to use the manual alarm control panel state as your trigger, then condition the actions with the device tracker state of the device. The problem you run into trying to do this in a single automation is when you use conditions, the actions stop at the first condition not met. This is where you probably need to use a script or maybe a template to handle all conditions and actions based on conditions in one automation. The pseudo code for a basic notification would look like the follow. It can be duplicated for every device if you don’t care about combining into one automation.

trigger:
- platform: state
  entity_id: alarm_control_panel.ha_alarm
  state: 'disarmed'
condition:
- condition: state
  entity_id: device_tracker.mobil
  state: 'home'
action:
- service: notify.mobil
  data:
    message: "Mobil Disarmed Alarm"

Wouldn’t this run into issues using the opposite trigger though? I assume the condition for armed would be that everyone has gone to not_home. So you’d need to create something a bit more complex at least for this I would think.

Definitely possible though, might need to create an input_select that updates a template sensor to “just_left” for a short amount of time so you can use that condition as your notifier instead. That way if someone else is not_home they wont get selected if they didn’t just leave

I guess I assumed there was an automation that disarmed the alarm when a device state became home. If a device state was home and the state of the alarm was still armed then the notification action would never fire.

right I’m talking about when the alarm arms. He wants a automation that will announce who was the last to leave, in which case everyone would be not_home. So you can’t use that as a condition for the notifier as it would just grab everyone. And you can’t use home as no one is home.

Yes, I was in the process of typing that up.

As for last device to leave, you would trigger based on device tracker state set to not home and condition nothing else is home, then the action would send the notification. pseudo code:

trigger:
- platform: state
  entity_id: device_tracker.mobil1
  state: 'not_home'
condition:
  condition: and
  conditions:
  - condition: state
    entity_id: device_tracker.mobil2
    state: 'not_home'
  - condition: state
    entity_id: device_tracker.skoda
    state: 'not_home'
  - condition: state
    entity_id: device_tracker.laptop
    state: 'not_home'
action:
- service: notify.mobil1
  data:
    message: "Mobil1 Armed Alarm"

copy and paste for every device. could possibly be consolidated into one automation by adding all the device tracker states in trigger and having all of them in the condition too. the problem is in the action and conditioning the action to say who last left. That might be able to be done with a template or if-then-else statements.

Yeah that’ll do it!

In fact, here’s a thread recently commented on that explains the if-then-else usage in a template. this would allow the automations to be consolidated into one automation and fire a different action (notification) based on which device it was.

EDIT: specifically this post in that thread