Automation with multiple conditions and actions

HI
Is it possible to have multiple conditions with separate actions for these conditions ?
What I am trying to achieve is after the trigger ensure binary_sensor.workday_sensor is off, If it is on exit the automation.
If it is off then set input_boolean.hotwater_timer to off then check input_boolean.hotwater_override is off if it is turn off switch.hot_water_cylinder_switch.
Hope the above makes sense :slight_smile:

   - alias: 'Hotwater off with Timer PM Weekends'
      hide_entity: False
      trigger:
        platform: template
        value_template: '{{ states.sensor.time.state == states.sensor.hotwater_weekends_pm_stop_time_long.state }}'
      condition:
        - condition: state
          entity_id: binary_sensor.workday_sensor
          state: 'off'
      action:
        - service: input_boolean.turn_off
          data:
            entity_id: input_boolean.hotwater_timer
        - condition: state
          entity_id: input_boolean.hotwater_override
          state: 'off'
      action:
        - service: switch.turn_off
          data:
            entity_id: switch.hot_water_cylinder_switch

Many Thanks
Jman

Yes, I believe you just need to call out your second condition and it should work fine.

   - alias: 'Hotwater off with Timer PM Weekends'
      hide_entity: False
      trigger:
        platform: template
        value_template: '{{ states.sensor.time.state == states.sensor.hotwater_weekends_pm_stop_time_long.state }}'
      condition:
        - condition: state
          entity_id: binary_sensor.workday_sensor
          state: 'off'
      action:
        - service: input_boolean.turn_off
          data:
            entity_id: input_boolean.hotwater_timer
#Add the following line
      condition:
        - condition: state
          entity_id: input_boolean.hotwater_override
          state: 'off'
      action:
        - service: switch.turn_off
          data:
            entity_id: switch.hot_water_cylinder_switch

Basically, the only requirements for an automation are a trigger and action. But you can have as many condition/action sequences as you want. The only catch is that as soon as a condition evaluates to false the automation ends. In your example that’s what you want so it’s fine, but as soon as you want to do anything more complicated such as if/else logic, you’d want to look at Node-Red or AppDaemon.

1 Like

Two action: statements in the same automation?

I tested the following automation (with version 0.91) and, although it passed Config Check, it failed to toggle the second light (light.family).

- alias: 'Test multi-action'
  trigger:
    entity_id: light.mantle
    platform: state
  action:
    - service: light.toggle
      entity_id: light.kitchen
  action:
    - service: light.toggle
      entity_id: light.family

This automation, with a single action:, successfully toggled both lights.

- alias: 'Test single-action'
  trigger:
    entity_id: light.mantle
    platform: state
  action:
    - service: light.toggle
      entity_id: light.kitchen
    - service: light.toggle
      entity_id: light.family

The correct syntax is

action:
  - service: input_boolean.turn_off
    entity_id: input_boolean.hotwater_timer
  - condition: state
    entity_id: input_boolean.hotwater_override
    state: 'off'
  - service: switch.turn_off
    entity_id: switch.hot_water_cylinder_switch
2 Likes

Thanks for the reply’s. I have it sorted now.

Regards
John

Hi, would you mind sharing your working code for this? I’m also looking to get something similar working. Thanks!

Hi
Sure the code snip below is what i ended up using

- alias: 'Hotwater off with Timer PM Weekdays'
  hide_entity: False
  trigger:
    platform: template
    value_template: '{{ states.sensor.time.state == states.sensor.hotwater_weekdays_pm_stop_time_long.state }}'
  action:
    - condition: state
      entity_id: binary_sensor.workday_sensor
      state: 'on'
    - service_template: >
         {% if is_state('input_boolean.hotwater_override', 'on') %}
           script.hotwater_timer_override_on
         {% else %}
           script.hotwater_timer_override_off
         {%endif%}

Regards
John

1 Like

If you’d like to streamline the service_template portion of your automation, you can do this:

    - service_template: script.hotwater_timer_override_{{states('input_boolean.hotwater_override')}}
2 Likes

Does priority matters? Currently, I have 2 automation with the same trigger. One announces x person is home. The second turns on the hallway light. While the first automation do not have a condition, second one does. That is, turn on the light if the light is in the ‘off’ state.

If I combine these 2 automation into one, will this work? For the end result, I want the announcement to happen regardless if the condition is false.
Update: The below code do not perform both actions. It performs only the last one.

- alias: welcome home alexa greeting
  trigger:
     - platform: state
       entity_id: 
        - input_boolean.duc_home
        - input_boolean.eri_home
       to: 'on'
  action:
    - service: notify.alexa_media
      data_template:
        data:
          type: tts
        target:
         - media_player.ai_1
        message: '{{ trigger.to_state.attributes.friendly_name }}, will be home in about 10 minutes.'
  condition:
    - condition: state
      entity_id: light.h_1
      state: 'off'
  action:
     - service: light.turn_on
       entity_id: light.h_1
       data:
         brightness: 180

You can’t use multiple keys like that. Just have one action, under which you have the service you always want to run, then the condition, then the service you only want to run if the condition was true.

I haven’t got a chance to test it. Will this work?

- alias: welcome home alexa greeting
  trigger:
     - platform: state
       entity_id: 
        - input_boolean.duc_home
        - input_boolean.eri_home
       to: 'on'
  action:
    - service: notify.alexa_media
      data_template:
        data:
          type: tts
        target:
         - media_player.ai_1
        message: '{{ trigger.to_state.attributes.friendly_name }}, will be home in about 10 minutes.'
  condition:
    - condition: state
      entity_id: light.h_1
      state: 'off'
     - service: light.turn_on
       entity_id: light.h_1
       data:
         brightness: 180
	

No, because the condition section attempts to call a service and that’s simply invalid.

Can you please delete one of your double posts, I suggest the top one as Taras and I have replied to the bottom one

Ok. This code is working. It will run the first service regardless if the condition is true or not.

- alias: welcome home alexa greeting
  trigger:
     - platform: state
       entity_id: 
        - input_boolean.duc_home
        - input_boolean.eri_home
       to: 'on'
  action:
    - service: notify.alexa_media
      data_template:
        data:
          type: tts
        target:
         - media_player.ai_1
        message: '{{ trigger.to_state.attributes.friendly_name }}, will be home in about 10 minutes.'
    - condition: state
      entity_id: light.h_1
      state: 'off'
    - service: light.turn_on
      entity_id: light.h_1
      data:
        brightness: 180
2 Likes