Service templates in automations - multiple independent if conditions

Hi,

I wonder if anybody can shed some light on whether the following is possible:
I have a binary sensor (sofa_occupied). When the sensor turns off I want to:

  1. pause the media player if it is playing
  2. turn off a fan if it is spinning
  3. turn off a light if it is on

When the switch turns back on I want to

  1. unpause the media player if it was previously playing
  2. turn the fan back on if it was previously on
  3. turn the light on if it is a certain time of day

I use input booleans to keep track of the media player and fan states at the time the sofa switch turns off.
I can do all of the above individually using service templates.
However, if I combine all of these in a single automation, it seems that the moment any of my if conditions is false, none of the actions that are listed further down in the automation get actioned. I guess this is intended behaviour - but is there a way around it?
I guess I could make individual automations for the 3 different entities, all with the same trigger - but that seems very clunky. Any advice is very welcome!

Here’s my two automations. In the first one, if it isn’t between 6:00 and 16:00, the daylight won’t turn on, but also none of the other actions are done. I’d like those to still run.

- id: '1579884607525'
  alias: sofa_becomes_occupied
  description: ''
  trigger:
  - entity_id: binary_sensor.sofa_occupied
    platform: state
    to: 'on'
  condition: []
  action:
  - service_template: '{% if ''06:00'' < states.sensor.time.state < ''16:00'' %} switch.turn_on
      {% endif %}'
    entity_id: switch.daylight
  - service_template: '{% if is_state(''input_boolean.was_media_playing'' , ''on'')
      %} script.newtv_media_playpause {% endif %}'
  - service_template: '{% if is_state(''input_boolean.was_media_playing'' , ''on'')
      %} input_boolean.turn_off {% endif %}'
    entity_id: input_boolean.was_media_playing
  - service_template: '{% if is_state(''input_boolean.was_fan_spinning'' , ''on'')
      %} switch.turn_on {% endif %}'
    entity_id: switch.downstairs_fan
  - service_template: '{% if is_state(''input_boolean.was_fan_spinning'' , ''on'')
      %} input_boolean.turn_off {% endif %}'
    entity_id: input_boolean.was_fan_spinning
- id: '1579899963808'
  alias: sofa_becomes_empty
  description: ''
  trigger:
  - entity_id: binary_sensor.sofa_occupied
    platform: state
    to: 'off'
  condition: []
  action:
  - service: switch.turn_off
    entity_id: switch.downstairs_fan
  - service: switch.turn_off
    entity_id: switch.daylight
  - service_template: '{% if is_state(''binary_sensor.newtv_media_playing'' , ''on'')
      %} input_boolean.turn_on {% endif %}'
    entity_id: input_boolean.was_media_playing
  - service_template: '{% if is_state(''binary_sensor.newtv_media_playing'' , ''on'')
      %} script.newtv_media_playpause {% endif %}'
  - service_template: '{% if is_state(''switch.downstairs_fan'' , ''on'') %} input_boolean.turn_on
      {% endif %}'
    entity_id: inpout_boolean.was_fan_spinning

when it gets complicated, often it’s better to do it in a proper code rather than in multiple templates/automations.
I’d say look at python scripts.

1 Like

This is possible, I do something similar for my music players, each player has its own on off schedule, a manual on facility and a sleep timer function.
These are all summated in a binary sensor to determine if they ‘should be’ on then I condition this with house occupancy to switch the players on or off.
It basically means you can’t set the entities direct through the ui (you just use the intermediaries) but you could if you wanted to, it just means that the states may then ‘not’ follow your dictates.
The issue for you is - is sofa occupancy a mandate for any of these 3 devices operating ?

Edit : Anything is possible and you usually don’t need a cray supercomputer to add 2 + 2 either.
They landed on the moon using a computer about as sophisticated as a programmable calculator. And computer languages are pretty much interchangeable, it’s just elitist to infer that ‘your’ language isn’t up to it and you ‘need’ Visual C++ to get anything done.

Awesome, thanks. That’s exactly the kind of thing I was looking for.

If you are familiar with python, I can also highly suggest AppDaemon.