Using outcome of entity selected in a specific automation as an action in a different automation

Hi folks, I have the following automation in place for closing respective garage doors when either household member leaves home:

description: Close respective garage door when Amy or Mark leave home
trigger:
  - entity_id: person.mark_fulton
    from: home
    platform: state
    to: not_home
    id: mark_left
  - entity_id: person.amy_fulton
    from: home
    platform: state
    to: not_home
    id: amy_left
condition: []
action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id: mark_left
              - condition: state
                entity_id: cover.garage_door_mark
                state: open
              - condition: template
                value_template: >-
                  {{ as_timestamp(now()) -
                  as_timestamp(states.sensor.uptime.last_changed) | int > 300 }}
        sequence:
          - service: notify.signal_warning
            data:
              title: Mark left home!
              message: Closing Mark's garage door
          - service: cover.close_cover
            target:
              entity_id: cover.garage_door_mark
            data: {}
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id: amy_left
              - condition: state
                entity_id: cover.garage_door_amy
                state: open
              - condition: template
                value_template: >-
                  {{ as_timestamp(now()) -
                  as_timestamp(states.sensor.uptime.last_changed) | int > 300 }}
        sequence:
          - service: notify.signal_warning
            data:
              title: Amy left home!
              message: Closing Amy's garage door
          - service: cover.close_cover
            target:
              entity_id: cover.garage_door_amy
            data: {}
    default: []
mode: single

As you can see household members both have separate cars with respective garage door covers.

I need to solve the case for the opposite arrival based automation where we arrive back at the house, both in the same car, the correct door opens.

Other threads suggest the bluetooth state of the companion app to identify the car, but we have iOS devices and don’t have this sensor. I also do not have car sensors in the garage that can tell which car is present/not present.

The next best solution I thought of was using the outcome of the garage door entity that was closed in the car leaving automation, paired with a person group sensor state transition, something like:

If group.amyandmark sensor transition to home, open the last garage door entity closed by the ‘close garage door’ automation

But checking docs I’m not sure if/how I can accomplish this. Is there a way in a template (or otherwise) I can reference which entity was selected in the last run of a different automation?

I think you should post your entire automation. This is just the action section.

Updated original post to include the other fields

1 Like

Based on your explanation, Home Assistant has no information about two cars, only about two persons.

  • If Mark leaves with Amy’s car, the automation will attempt to close Mark’s garage door. It should attempt to close Amy’s garage door but it has no information about her car’s location, just Mark’s location.

  • If both persons return home in the same car, there’s nothing indicating they’re ‘grouped’ into one of the two cars, therefore the automation will open both of the closed doors.

If each car had its own device tracker, then Home Assistant could make correct decisions about which door to open/close including tricky situations like both Mark and Amy leaving/arriving in Mark’s car or Mark leaving/arriving in Amy’s car.

1 Like

Correct on all three points. As you mention in an ideal situation we’d have device tracking on the cars.
The challenge is getting a reliable device tracker - I can’t use the companion app as we have iOS devices, so no bluetooth sensor.

So, the next best option is to use the sensors we have creatively. We are using homekit to do presense detection which has been solid.

The two scenarios you describe:

  • If both persons return home in the same car, there’s nothing indicating they’re ‘grouped’ into oneof the two cars, therefore the automation will open both of the closed doors.

That’s what’s happening today and I’m looking to improve. However, because the garage door close automation works correctly when both persons leave home in the same car, my thought is to setup a third action condition that triggers off group presense and opens the garage door that was the last one to close. Need to find out if this is possible/how to accomplish this.

  • If Mark leaves with Amy’s car, the automation will attempt to close Mark’s garage door. It should attempt to close Amy’s garage door but it has no information about her car’s location, just Mark’s location.

This is an edge case I’m OK to live with - if I drive my wifes car 99% of the time we are both in it. The other 1% is if I take it for maintenance. For security I can setup a notifier based automation that alerts me when the garage door is open for longer periods of time.

You could install Bluetooth trackers in each vehicle. Use that in combination with the person entities to correctly determine which door to open for all situations.

That sounds plausible provided you create an ‘old-style’ Group entity and set its all option to true (i.e. it will report on/off only when both person entities are home/not_home).

Thanks - I have a group entity setup that way and confirmed via history that that’s how its behaving.
But how do I do this bit?

open the last garage door entity closed by the ‘close garage door’ automation

This is the part I’m stumped on (and my reason for creating this post :slight_smile: )

One way is to use a service call (in your automation) to store the entity_id of the most recently closed door in an Input Text.

Another way is to create a Trigger-based Template Sensor that records the entity_id of the most recently closed door.

1 Like

Fantastic, it looks like the Input Text service was what I needed. Thank you!

Added an input_text helper and then added actions to the close garage door automation to set respective values:

# Amy:
service: input_text.set_value
data:
     entity_id: input_text.garage_door_last_closed          
     value: "amy"

# Mark:
service: input_text.set_value
data:
     entity_id: input_text.garage_door_last_closed          
     value: "mark"

Then added two new choose conditions in the action section of the open garage door automation. This is quite long winded but will hopefully accomplish what I need it to:

  alias: Garage - OPEN - Door - Amy or Mark garage door when Amy or Mark arrive home
  description: Open respective garage door when Amy or Mark arrive home
  trigger:
  - entity_id: person.mark
    from: not_home
    platform: state
    to: home
    id: mark_arrived
  - entity_id: person.amy
    from: not_home
    platform: state
    to: home
    id: amy_arrived
  - entity_id:
    - group.amyandmark
    from: not_home
    platform: state
    to: home
    id: group_arrived
  condition: []
  action:
  - choose:
    - conditions:
      - condition: and
        conditions:
        - condition: trigger
          id: mark_arrived
        - condition: state
          entity_id: cover.garage_door_mark
          state: closed
        - condition: template
          value_template: '{{ as_timestamp(now()) - as_timestamp(states.sensor.uptime.last_changed)
            | int > 300 }}'
      sequence:
      - service: notify.signal_warning
        data:
          title: Mark arrived home!
          message: Welcome home Mark! Opening Mark's garage door
      - service: cover.open_cover
        target:
          entity_id: cover.garage_door_mark
        data: {}
    - conditions:
      - condition: and
        conditions:
        - condition: trigger
          id: amy_arrived
        - condition: state
          entity_id: cover.garage_door_amy
          state: closed
        - condition: template
          value_template: '{{ as_timestamp(now()) - as_timestamp(states.sensor.uptime.last_changed)
            | int > 300 }}'
      sequence:
      - service: notify.signal_warning
        data:
          title: Amy arrived home!
          message: Welcome home Amy! Opening Amy's garage door
      - service: cover.open_cover
        target:
          entity_id: cover.garage_door_amy
        data: {}
    - conditions:
      - condition: and
        conditions:
        - condition: trigger
          id: group_arrived
        - condition: state
          entity_id: cover.garage_door_amy
          state: closed
        - condition: state
          entity_id: input_text.garage_door_last_closed
          state: amy
        - condition: template
          value_template: '{{ as_timestamp(now()) - as_timestamp(states.sensor.uptime.last_changed)
            | int > 300 }}'
      sequence:
      - service: notify.signal_warning
        data:
          title: Amy and Mark arrived home!
          message: Welcome home Amy & Mark! Opening Amy's garage door
      - service: cover.open_cover
        target:
          entity_id: cover.garage_door_amy
        data: {}
    - conditions:
      - condition: and
        conditions:
        - condition: trigger
          id: group_arrived
        - condition: state
          entity_id: cover.garage_door_mark
          state: closed
        - condition: state
          entity_id: input_text.garage_door_last_closed
          state: mark
        - condition: template
          value_template: '{{ as_timestamp(now()) - as_timestamp(states.sensor.uptime.last_changed)
            | int > 300 }}'
      sequence:
      - service: notify.signal_warning
        data:
          title: Amy and Mark arrived home!
          message: Welcome home Amy & Mark! Opening Mark's garage door
      - service: cover.open_cover
        target:
          entity_id: cover.garage_door_mark
        data: {}
    default: []
  mode: single

Here’s what I foresee can happen:

When you are both in the same car and arriving in the home zone, let’s say Amy’s phone is the first to report it has entered the home zone (because there’s no way both phones can report their arrival in the home zone at the exact same moment in time). Therefore it will cause the automation to trigger via its second State Trigger and open Amy’s garage door. That might not be the correct door if both departed in Mark’s car.

Then when Mark’s phone enters the home zone (perhaps seconds or milliseconds later), it’s a tossup between which of the two remaining State Triggers gets triggered first, the one just for Mark’s arrival home or the one monitoring the arrival of the group.

If Amy’s phone is the first to report a home state, Amy’s presence switching to home should also flip the groups sensor group.amyandmark to home at the same time, meaning its a toss up between the group state and Amy arriving at this point… hmm.
Is there some sort of evaluation priority that I could set here and set the trigger priority for the group condition higher?

Don’t you think, that this can only holdup as a workaround? :laughing: This all would be easily solveable by two BT beacons and an ESP32. :slight_smile: I could even imagine using a Shelly UNI, connected to the car battery and work with signal strength of the WIFI. But working with a “person” trigger for a car? :slight_smile:

Don’t misunderstand me, I find this topic interesting, that’s why I’m following from the beginning, but I get the feeling, investing some $20 or $30 bucks might not be a bad idea, don’t you think? :slight_smile:

Not if the group sensor is configured to report on only when both Mark and Amy are home. If it’s not configured like that then it will report on when either Mark or Amy is home which defeats its intended purpose (group.amyandmark).

It’s first come, first served; there’s no priority.

Like I suggested in my first post, equip the two cars with device trackers. Besides simplifying the automation, and properly handling edge cases, it’s more secure to detect the cars when they’re on the driveway and not when your phones have just entered the periphery of your home zone (which I assume is larger than the bounds of your property).

1 Like

Thank you - yes I see, ESP32 and a couple of USB powered BT beacons for the cards would be pretty inexpensive and more secure. But nevertheless glad to have exhausted options using phone device trackers. Thanks again for the great input!

1 Like