Help with a condition

I have this automation working and it works just fine.

- alias: "Person1 Leaving Location Alert" 
  trigger: 
  - platform: state 
    entity_id: device_tracker.person1_all 
    to: 'not_home' 
  condition: 
  - condition: numeric_state 
    entity_id: sensor.ha_runtime_in_minutes 
    above: 1 
  - condition: template 
    value_template: '{{ as_timestamp(now()) - as_timestamp(states.automation.person1_leaving_location_alert.attributes.last_triggered) | int > 120 }}' 
  action: 

Nice and simple, send me an alert when person 1 leaves any zone. The one time I don’t want the alert is if they leave home and I’m already home. I guess if I really wanted to get fancy, I would not need an alert of Person 1 left a zone I was already in, or had just left myself. I’m just having trouble with the logic. Actually the more I think about it, not getting an alert if I was in the zone person 1 left or if I had also just left that zone within the past min or so, would be great.

We are rarely in the same zones (other than home), but sometimes we are, and I get alerts as we travel in and out of those zones together.

Any tips/recommendations are appreciated

Templating is what you’d need, using trigger.from_state (see here).

- condition: template
  value_template: "{{ states('device_tracker.YOU') == trigger.from_state.state }}"

You won’t be able to do the “if I also left recently” using just templates, for that you’d need to store the previous zone name somewhere (like an input_text) and compare them.

input_text:
  your_last_zone:
    name: Zone I last left
- alias: "Leaving Location update" 
  trigger: 
  - platform: state 
    entity_id: device_tracker.YOU
    to: 'not_home' 
action:
  - service: input_text.set_value
    data_template
      entity_id: input_text.your_last_zone
      value: '{{ trigger.from_state.state }}'
- condition: or
  conditions:
  - condition: template
    value_template: "{{ states('device_tracker.YOU') == trigger.from_state.state }}"
  - condition: template
    value_template: "{{ ( states('input_text.your_last_zone') == trigger.from_state.state ) and ( ( as_timestamp(now) - as_timestamp(states.input_text.your_last_zone.last_updated) ) < 300 ) }}"

That’ll check to see if you’re in the same zone they left, or you left the same zone they did in the last 5 minutes (300 seconds)

1 Like

This is fantastic. One quick question in case I’m reading it backwards. I dont want the action to trigger if person 1 left the zone I am in , or if we both left the same zone around the same time. So would I use

!=

instead of

==

I already had an input text of my previous zone in a different automation, so I’m able to use that. :slight_smile:

So like this

- alias: "person_to_track Leaving Location Alert"
  trigger:
  - platform: state
    entity_id: device_tracker.person_to_track
    to: 'not_home'
  condition:
  - condition: numeric_state
    entity_id: sensor.ha_runtime_in_minutes
    above: 1
  - condition: or
    conditions:
    - condition: template
      value_template: "{{ states('device_tracker.me_all') != trigger.from_state.state }}"
    - condition: template
      value_template: "{{ ( states('input_text.commute_start_zone') != trigger.from_state.state ) and ( ( as_timestamp(now) - as_timestamp(states.input_text.commute_start_zone.last_updated) ) < 300 ) }}"
  action: