Combining two simple automations

I think what I want to to is use triggered by or something like that

- alias: 'Reset Door Lock for person1'
  trigger:
  - platform: event
    event_type: timer.finished
    event_data: 
      entity_id: timer.person1_just_arrived
  action:
  - service: homeassistant.turn_off
    entity_id:
      - input_boolean.back_door_opened_for_person1
	  
- alias: 'Reset Door Lock for person2'
  trigger:
  - platform: event
    event_type: timer.finished
    event_data: 
      entity_id: timer.person2_just_arrived
  action:
  - service: homeassistant.turn_off
    entity_id:
      - input_boolean.back_door_opened_for_person2

This is the simplest example, but I have several automatons for my wife and I that are similar. Looking for ideas for two different enties to take different actions based on the trigger entity.

Thanks for any ideas

use the trigger.entity_id to find out who triggered the automation, then based on this you can set the correct input_boolean.
Think this should work

- alias: 'Reset Door Lock for person'
  trigger:
  - platform: event
    event_type: timer.finished
    event_data: 
      entity_id:
        - timer.person1_just_arrived
        - timer.person2_just_arrived
  action:
  - service: homeassistant.turn_off
    data_template:
      entity_id: >-
        {% if trigger.entity_id == 'timer.person1_just_arrived'}
        input_boolean.back_door_opened_for_person1
        {% else %}
        input_boolean.back_door_opened_for_person2
        {% endif %}
1 Like

I don’t believe this works. Saw another thread about this a week or so ago.

@ptdalen the end solution provided by @pnbruckner was to create a condition and omit the event_data from the trigger.

The action will take some massaging to get it to work correctly, as you need to select the opposite person. I’ll be heading home soon and I can take a look at it. I believe @lolouk44’s action section should work fine too with a minor change. The event trigger doesn’t contain an entity id, the trigger.event.data contains the entity id. Trigger template stuff is here:

and this is the change:

 action:
 - service: homeassistant.turn_off
   data_template:
     entity_id: >-
       {% if trigger.event.data.entity_id == 'timer.person1_just_arrived'}
       input_boolean.back_door_opened_for_person1
       {% else %}
       input_boolean.back_door_opened_for_person2
       {% endif %}
2 Likes

Thanks, Here’s anotherone I’m combining into one automation.

- alias: 'People Left'
  trigger:
   - platform: state
     entity_id:
       - binary_sensor.person1_presence
       - binary_sensor.person2_presence
     to: 'off'
  condition:
  - condition: numeric_state
    entity_id: sensor.ha_runtime_in_minutes
    above: 1
 action:
 - service: homeassistant.turn_off
   data_template:
     entity_id: >-
       {% if trigger.entity_id == 'binary_sensor.person1_presence'}
       input_boolean.person1_present,input_boolean.back_door_opened_for_person1,input_boolean.open_garage_for_person1
       {% else %}
       input_boolean.person2_present,input_boolean.back_door_opened_for_person2,input_boolean.open_garage_for_person2
       {% endif %}
1 Like

What if I had condtions that were different depending on the entity triggering? Too complex, not worth it?

the trigger and the actions are pretty much the same. I use this to auto unlock the door when I 1st arrive (timer automation above), but only once. The input boolean keeps it from opening multiple times in while the timer runs. I guess I could cancel the timer instead of the boolean, but when I did the timer, I had plans on doing other automations while the timer was on, so maybe not

- alias: 'Unlock Back Door for person1'
  trigger:
  - platform: state
    entity_id: binary_sensor.backyard_line_crossing
    to: 'on'    
  condition:
  - condition: state
    entity_id: timer.person1_just_arrived
    state: 'active'
  - condition: state
    entity_id: input_boolean.back_door_opened_for_person1
    state: 'off'
  action:
  - service: input_boolean.turn_on
    entity_id: input_boolean.back_door_opened_for_person1
  - service: lock.unlock
    entity_id: lock.lock_back_door_lock

- alias: 'Unlock Back Door for person2'
  trigger:
  - platform: state
    entity_id: binary_sensor.backyard_line_crossing
    to: 'on'    
  condition:
  - condition: state
    entity_id: timer.person2_just_arrived
    state: 'active'
  - condition: state
    entity_id: input_boolean.back_door_opened_for_person2
    state: 'off'  
  action:
  - service: input_boolean.turn_on
    entity_id: input_boolean.back_door_opened_for_person2
  - service: lock.unlock
    entity_id: lock.lock_back_door_lock

It’s definitely possible, the only thing is although you reduce the number of automations, you increase their complexity. This doesn’t make much difference for HA, but it will give you grey hairs when you need to troubleshoot what went wrong…
This specific automation doesn’t trigger based on a person so it might actually turn up a lot more complex and lengthier than 2 automations…

It’s doable, but can be painful.

- alias: 'Unlock Back Door for person1'
  trigger:
  - platform: state
    entity_id: binary_sensor.backyard_line_crossing
    to: 'on'
  action:
  - condition: template
    value_template: >
      {% set people = [ 
        states.timer.person1_just_arrived,
        states.timer.person2_just_arrived,
        states.timer.person3_just_arrived ] %}
      # get the people who arrived.
      {% set arrived = people | selectattr('state','eq','active') | list %}
      # did anyone arrive?
      {% if arrived | length >= 1 %}
        # find the first person who arrived home and get 'who' did.
        {% set person = arrived[0].split('.')[-1].split('_')[0] %} 
        # make sure their boolean is off
        {{ is_state('input_boolean.back_door_opened_for_%s'%person, 'off') }}
      {% else %}
        False
      {% endif %} 
  - service: input_boolean.turn_on
    data_template: >
      {% set people = [ 
        states.timer.person1_just_arrived,
        states.timer.person2_just_arrived,
        states.timer.person3_just_arrived ] %}
      # get the people who arrived.
      {% set arrived = people | selectattr('state','eq','active') | list %}
      input_boolean.back_door_opened_for_{{ arrived[0].split('.')[-1].split('_')[0] }}
  - service: lock.unlock
    entity_id: lock.lock_back_door_lock
1 Like

I wouldn’t want to troubleshoot this :wink:

Agreed. Good in my head, bad in real life. haha