Media Players optimizing script

Hi,

The below code works but is there a way that this code can be optimized?

The script triggers if tv or tv_1 is in state (playing, paused, idle or unavailable). Only one of the tv will be playing at a given time.

Once the script is triggered,

  1. It checks if tv or tv_1 is playing then it turns OFF the lights and sets input_boolean.movie_mode to “off” (its required for TTS in another script) and call the script to turn OFF the lights.

  2. It checks if tv or tv_1 is paused, idle or unavailable then it turns ON the lights and sets input_boolean.movie_mode to “on” (its required for TTS in another script) and call the script to turn ON the lights.

alias: Movie
description: ""
trigger:
  - platform: state
    entity_id:
      - media_player.tv
      - media_player.tv_1
    to:
      - playing
      - paused
      - idle
      - unavailable
condition: []
action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: or
                conditions:
                  - condition: state
                    entity_id: media_player.tv
                    state: playing
                  - condition: state
                    entity_id: media_player.tv_1
                    state: playing
              - condition: state
                entity_id: input_boolean.movie_mode
                state: "off"
        sequence:
          - service: script.turn_lights_on_off
            data:
              flag: true
      - conditions:
          - condition: and
            conditions:
              - condition: or
                conditions:
                  - condition: state
                    entity_id: media_player.tv
                    state:
                      - paused
                      - idle
                      - unavailable
                  - condition: state
                    entity_id: media_player.tv_1
                    state:
                      - paused
                      - idle
                      - unavailable
              - condition: state
                entity_id: input_boolean.movie_mode
                state: "on"
                enabled: true
        sequence:
          - service: script.turn_lights_on_off
            data:
              flag: false
    default: []
mode: single
trigger:
  - platform: state
    entity_id:
      - media_player.tv
      - media_player.tv_1
    to:
      - playing
      - paused
      - idle
      - unavailable

a. Can this part be converted to a template which returns the entity_id of the tv that is playing ?

            conditions:
              - condition: or
                conditions:
                  - condition: state
                    entity_id: media_player.tv
                    state: playing
                  - condition: state
                    entity_id: media_player.tv_1
                    state: playing

b. Can this part be converted to template which return the entity_id of the tv that is playing ?

C. Can the code be optimized?

It will anyway. Whichever entity triggers the automation will be available in the variable

{{ trigger.entity_id }}.

Yes using a template condition:

            conditions:
              - condition: template
                value_template: "{{ is_state(trigger.entity_id, 'playing') }}"

Here’s one way, separate your triggers and give them ids:

trigger:
  - id: playing
    platform: state
    entity_id:
      - media_player.tv
      - media_player.tv_1
    to:
      - playing
  - id: other_state
    platform: state
    entity_id:
      - media_player.tv
      - media_player.tv_1
    to:
      - paused
      - idle
      - unavailable

Then all of this:

      - conditions:
          - condition: and
            conditions:
              - condition: or
                conditions:
                  - condition: state
                    entity_id: media_player.tv
                    state:
                      - paused
                      - idle
                      - unavailable
                  - condition: state
                    entity_id: media_player.tv_1
                    state:
                      - paused
                      - idle
                      - unavailable

Can be changed to:

      - conditions:
          - condition: trigger
            id: other_state     

And all of this:

      - conditions:
          - condition: and
            conditions:
              - condition: or
                conditions:
                  - condition: state
                    entity_id: media_player.tv
                    state: playing
                  - condition: state
                    entity_id: media_player.tv_1
                    state: playing
              - condition: state
                entity_id: input_boolean.movie_mode
                state: "off"

Can be changed to:

      - conditions:
          - condition: trigger
            id: playing 
2 Likes

Thank you​:+1::+1: