Multiple actions depending on

automation
Tags: #<Tag:0x00007fd076d12380>

#1

Is it possible to set up multiple “actions” for an automation depending on the condition?

For example;

- alias: 'Notify Presence'
  trigger:
    platform: state
    entity_id:
      - device_tracker.first_device
      - device_tracker.second_device
      - device_tracker.third_device
      - device_tracker.fourth_device
    from: not_home
    to: home
  condition:
    - condition: time
      after: '08:00:00'
      before: '20:00:00'
  action:
    # How do I only notify this person if they're home too?
    - service: notify.person_a
      data_template:
        title: "Someone's home!"
        message: "{{ trigger.to_state.name }} is now home"
    # How do I only notify this person if they're home too?
    - service: notify.person_b
      data_template:
        title: "Someone's home!"
        message: "{{ trigger.to_state.name }} is now home"

I see that there is support for conditional actions, but it stops the whole action chain when a condition fails instead of just skipping the next action, which means that if person A wasn’t home then it wouldn’t check person B’s presence or send them a message either.


#2

No but you could just create a script for person_a and person_b add a condition at the top of the script then pass the trigger.to_state.name into the script.

automation action becomes:

- service: script.turn_on
  entity_id: script.person_a
  data_template:
    variables:
       state_name: {{ trigger.to_state.name }}
- service: script.turn_on
  entity_id: script.person_b
  data_template:
    variables:
       state_name: {{ trigger.to_state.name }}

Then your script would be

- condition: # home condition here
- service: notify.person_a
  data_template:
  title: "Someone's home!"
  message: "{{ state_name }} is now home"

Then a second script as above for person b.


#3

It’s a shame there’s no support for more complex branching in automations. I’ve implemented it as you suggested and it works great, thanks!


#4

I would suggest taking a look at Node Red. This is how most (me included) seem to be doing more complex automations


#5

Yeah I am using Node-RED for some automations at the moment (mainly scheduling), I guess it’d make sense to migrate these across too. Thanks!