Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 95919

Service templates in automations - multiple independent if conditions

$
0
0

@murtoz wrote:

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

Posts: 3

Participants: 3

Read full topic


Viewing all articles
Browse latest Browse all 95919

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>