Multiple actions on trigger each with it's own condition

Hi all,

Noob here.

I have this:

automation:
  - alias: Sync Heating Status from Zipato
    trigger:
      platform: state
      entity_id: binary_sensor.HeatStatus
    action:
      - condition: state
        entity_id: binary_sensor.HeatStatus
        state: 'on'
      - service: switch.turn_on
        entity_id: switch.zipato_sensor
      #off  
      - condition: state
        entity_id: binary_sensor.HeatStatus
        state: 'off'      
      - service: switch.turn_ff
        entity_id: switch.zipato_sensor

An attempt at 2 actions with it’s own condition each. Independently, these definitely work. But when combined like above, only the first action works.

Is my syntax wrong? Is the above possible or do I have to break it out into two triggers - one for “on” and one for “off”?

Thanks.

If your first condition is not fulfilled the first action is not triggered and the second condition is not evaluated because the sequence has not made it beyond the first condition.

I see two options:

  1. Wrap your conditons/actions into an if-then-else
  2. Use a template template to evaluate what the trigger was and adjust the action accordingly.

Both will work only after you corrected turn_ff to turn_off, of course.

Here is an example of how to evaluate triggers and adjust the action accordingly:

- alias: XMas Lights Mirror
#  initial_state: "off"
  trigger:
    - platform: state
      entity_id: switch.sonoff_s31_01, switch.sonoff_s31_02
      from: 'on'
      to: 'off'
    - platform: state
      entity_id: switch.sonoff_s31_01, switch.sonoff_s31_02
      from: 'off'
      to: 'on'
  action:
    service_template: >
      {% if trigger.to_state.state == "on" %}
      switch.turn_on
      {% elif trigger.to_state.state == "off" %}
      switch.turn_off
      {% endif %}
    data_template: 
      entity_id: >
        {% if trigger.from_state.entity_id == "switch.sonoff_s31_01" %}
        switch.sonoff_s31_02
        {% elif trigger.from_state.entity_id == "switch.sonoff_s31_02" %}
        switch.sonoff_s31_01
        {% endif %}

Because you only using one switch as trigger and action you can lose a lot of code, obviously, e.g. the whole entity_id thing.

2 Likes

Thank you. This will do.

I’m getting:

Error loading /home/homeassistant/.homeassistant/configuration.yaml: while scanning for the next token
found character ‘%’ that cannot start any token
in “/home/homeassistant/.homeassistant/configuration.yaml”, line 205, column 8

For this:

automation:
  - alias: Sync Heating Status from Zipato
    trigger:
      platform: state
      entity_id: binary_sensor.HeatStatus
    action:
      service_template: >
      {% if trigger.to_state.state == "on" %}
      switch.turn_on
      {% elif trigger.to_state.state == "off" %}
      switch.turn_off
      {% endif %}
      data: 
       entity_id: switch.zipato_sensor

Line 205 is the “if trigger.to_state.state == on” line.

Try and indent the text under service_template by 2 spaces and the entity_id by 1:

automation:
  - alias: Sync Heating Status from Zipato
    trigger:
      platform: state
      entity_id: binary_sensor.HeatStatus
    action:
      service_template: >
        {% if trigger.to_state.state == "on" %}
        switch.turn_on
        {% elif trigger.to_state.state == "off" %}
        switch.turn_off
        {% endif %}
      data: 
        entity_id: switch.zipato_sensor
1 Like

that was it. thank you.

side question while I have you: can one use a data template for the initial value of sensors?

Can you please elaborate, what do you mean with ‘initial value’?
Got an example?