Combine automations + sequences

Hi All,

I have 3 automations for my AppleTV (playing, paused, idle) but want combine those and have 3 sequences to make everything more logic. Only thing I use for playing a “for: 10 seconds” in trigger. Can someone help me how to combine include this 10 seconds part.

Here original 3 automations:

- alias: "Control - AppleTV status is idle"
  trigger:
  
    platform: state
    entity_id: media_player.woonkamer_2
    to: "idle"

  condition: 
    - condition: state
      entity_id: sun.sun
      state: "below_horizon"

    - condition: state
      entity_id: input_boolean.scene_appletv
      state: "on"

  action:
    - service: light.turn_on
      data:
        entity_id:
          - light.livingroom_wall
          - light.livingroom_window
          - light.balcony
        brightness_pct: 100

    - service: light.turn_on
      data:
        entity_id: light.nanoleaf
        brightness_pct: 50

    - service: light.turn_on
      data_template:
        entity_id: light.dressoir_all
        brightness_pct: "{{ 0 if is_state('binary_sensor.node_10', 'on') else 100 }}"

    - service: light.turn_off
      data:
        entity_id:
          - light.kitchen_all
          - light.sink_all
          - light.toilet_all

- alias: "Control - AppleTV status is playing"
  trigger:
  
    platform: state
    entity_id: media_player.woonkamer_2
    to: "playing"
    for:
      seconds: 10

  condition: 
    - condition: state
      entity_id: sun.sun
      state: "below_horizon"

    - condition: state
      entity_id: input_boolean.scene_appletv
      state: "on"

  action:
    - service: light.turn_on
      data:
        entity_id: light.livingroom_wall
        brightness_pct: 60

    - service: light.turn_on
      data:
        entity_id: 
          - light.livingroom_window
          - light.nanoleaf
          - light.balcony
        brightness_pct: 30

    - service: light.turn_on
      data_template:
        entity_id: light.dressoir_all
        brightness_pct: "{{ 0 if is_state('binary_sensor.node_10', 'on') else 25 }}"

    - service: light.turn_off
      data:
        entity_id:
          - light.kitchen_all
          - light.sink_all
          - light.toilet_all

- alias: "Control - AppleTV status is paused"
  trigger:
  
    platform: state
    entity_id: media_player.woonkamer_2
    to: "paused"

  condition: 
    - condition: state
      entity_id: sun.sun
      state: "below_horizon"
      
    - condition: state
      entity_id: input_boolean.scene_appletv
      state: "on"
 
  action:
    - service: light.turn_on
      data:
        entity_id:
          - light.livingroom_wall
          - light.livingroom_window
          - light.kitchen_all
          - light.sink_all
          - light.toilet_all
          - light.balcony
        brightness_pct: 100

    - service: light.turn_on
      data:
        entity_id: light.nanoleaf
        brightness_pct: 50

    - service: light.turn_on
      data_template:
        entity_id: light.dressoir_all
        brightness_pct: "{{ 0 if is_state('binary_sensor.node_10', 'on') else 100 }}"

Here the automation I want use and below the script sequence I created:

- alias: "Control - AppleTV Status"
  trigger:
  
    platform: state
    entity_id: media_player.woonkamer_2

  condition: 
    - condition: state
      entity_id: sun.sun
      state: "below_horizon"  

    - condition: state
      entity_id: input_boolean.scene_appletv
      state: "on" 

  action:
    service_template: >-
      {% if (states.media_player.woonkamer_2.state) == 'playing' %} script.appletv_control_playing
      {% elif (states.media_player.woonkamer_2.state) == 'paused' %} script.appletv_control_paused
      {% else %} script.appletv_control_idle
      {% endif %}

below sequences:


appletv_control_paused:
  alias: "AppleTV Control Paused Scene"
  sequence:
  
    - service: light.turn_on
      data:
        entity_id:
          - light.livingroom_wall
          - light.livingroom_window
          - light.kitchen_all
          - light.sink_all
          - light.toilet_all
          - light.balcony
        brightness_pct: 100

    - service: light.turn_on
      data:
        entity_id: light.nanoleaf
        brightness_pct: 50

    - service: light.turn_on
      data_template:
        entity_id: light.dressoir_all
        brightness_pct: "{{ 0 if is_state('binary_sensor.node_10', 'on') else 100 }}"

appletv_control_playing:
  alias: "AppleTV Control Playing Scene"
  sequence:
 
    - service: light.turn_on
      data:
        entity_id: light.livingroom_wall
        brightness_pct: 60

    - service: light.turn_on
      data:
        entity_id:
          - light.nanoleaf
          - light.livingroom_window
          - light.balcony
        brightness_pct: 30

    - service: light.turn_on
      data_template:
        entity_id: light.dressoir_all
        brightness_pct: "{{ 0 if is_state('binary_sensor.node_10', 'on') else 25 }}"

    - service: light.turn_on
      data_template:    
        entity_id:
          - light.kitchen_all
          - light.sink_all
        brightness_pct: "{{ 100 if is_state('binary_sensor.node_16', 'on') else 0 }}"

    - service: light.turn_on
      data_template:    
        entity_id: light.toilet_all
        brightness_pct: "{{ 100 if is_state('binary_sensor.node_20', 'on') else 0 }}"          
          

appletv_control_idle:
  alias: "AppleTV Control Idle Scene"
  sequence:

    - service: light.turn_on
      data:
        entity_id:
          - light.livingroom_wall
          - light.livingroom_window
          - light.balcony
        brightness_pct: 100

    - service: light.turn_on
      data:
        entity_id: light.nanoleaf
        brightness_pct: 50

    - service: light.turn_on
      data_template:
        entity_id: light.dressoir_all
        brightness_pct: "{{ 0 if is_state('binary_sensor.node_10', 'on') else 100 }}"
        
    - service: light.turn_on
      data_template:    
        entity_id:
          - light.kitchen_all
          - light.sink_all
        brightness_pct: "{{ 100 if is_state('binary_sensor.node_16', 'on') else 0 }}"

    - service: light.turn_on
      data_template:    
        entity_id: light.toilet_all
        brightness_pct: "{{ 100 if is_state('binary_sensor.node_20', 'on') else 0 }}"

new way is working, only without the “for 10 seconds” because I don’t know how to add that one for only the playing part. I thought to add "delay: “00:00:10"” but after 10 seconds the sequence is firing because it won’t check state anymore.

You could try adding this at the beginning of the appletv_control_playing script:

    - wait_template: "{{ not is_state('media_player.woonkamer_2', 'playing') }}"
      timeout: '00:00:10'
      continue_on_timeout: true
    - condition: state
      entity_id: media_player.woonkamer_2
      state: playing

Note, though, that your solution will not work very well if media_player.woonkamer_2 changes state (even just attribute changes) while the playing script is waiting the 10 seconds. I’d suggest adding the following condition to the automation:

    - condition: template
      value_template: >
        {{ trigger.from_state is none or
           trigger.to_state is not none and
           trigger.to_state.state != trigger.from_state.state }}

This will prevent the action from running if the state string doesn’t change.

Also, I would change the action to:

  action:
    - service: script.turn_off
      entity_id:
        - script.appletv_control_playing
        - script.appletv_control_paused
        - script.appletv_control_idle
    - service_template: >-
        {% if (states.media_player.woonkamer_2.state) == 'playing' %} script.appletv_control_playing
        {% elif (states.media_player.woonkamer_2.state) == 'paused' %} script.appletv_control_paused
        {% else %} script.appletv_control_idle
        {% endif %}

That will make sure the script isn’t already running before trying to start it.

3 Likes

I add everything. The playing part is now not fired anymore. So ill will check the code. Something with the wait part

EDIT:

changed timeout: ‘00:10:00’ to timeout: ‘00:00:10’ otherwise it will wait for 10 minutes not 10 seconds :smiley:

Oops! Sorry about that typo. I’ll go back and fix my previous reply for future reference.

So does this work as you want now?

1 Like

Yes this did the trick!!! Thanks

1 Like