Script change with conditions

Tags: #<Tag:0x00007f326a2c7628>

Guys I have the following script and in need to put a condition under the first and fourth data template.
I need these two to be executed only when the status of “media_player.sonos_one” is “playing”.

sonos_say:
  alias: Sonos TTS script
  sequence:
  - data_template:
      entity_id: '{{ sonos_entity }}'
    service: sonos.snapshot
  - data_template:
      entity_id: '{{ sonos_entity }}'
      volume_level: '{{ volume }}'
    service: media_player.volume_set
  - data_template:
      entity_id: '{{ sonos_entity }}'
      message: '{{ message }}'
    service: tts.google_translate_say
  - delay: '{{ delay|default(''00:00:00'') }}'
  - data_template:
      entity_id: '{{ sonos_entity }}'
    service: sonos.restore

Use the choose action.

@tom_l
Can you please show me because I am kind of confused in how to do this.

Wrap the services you want to be conditional in the choose action:

sonos_say:
  alias: Sonos TTS script
  sequence:
  - choose:
    - conditions:
      - condition: state
        entity_id: media_player.sonos_one
        state: playing
        - data_template:
            entity_id: '{{ sonos_entity }}'
          service: sonos.snapshot
  - data_template:
      entity_id: '{{ sonos_entity }}'
      volume_level: '{{ volume }}'
    service: media_player.volume_set
  - data_template:
      entity_id: '{{ sonos_entity }}'
      message: '{{ message }}'
    service: tts.google_translate_say
  - delay: '{{ delay|default(''00:00:00'') }}'
  - choose:
    - conditions:
      - condition: state
        entity_id: media_player.sonos_one
        state: playing
        - data_template:
            entity_id: '{{ sonos_entity }}'
          service: sonos.restore

Now, this does what you asked for (actions 1 and 4 only if playing) but action 4 will never happen because the media player won’t be playing by then. The TTS message will have finished.

One way around this would be to create an input boolean and set it if the Sonos snapshot occurs, then use that as the test if you should restore the Sonos.

sonos_say:
  alias: Sonos TTS script
  sequence:
  - choose:
    - conditions:
      - condition: state
        entity_id: media_player.sonos_one
        state: playing
        - data_template:
            entity_id: '{{ sonos_entity }}'
          service: sonos.snapshot
        - service: input_boolean.turn_on
          entity_id: input_boolean.sonos_was_saved
  - data_template:
      entity_id: '{{ sonos_entity }}'
      volume_level: '{{ volume }}'
    service: media_player.volume_set
  - data_template:
      entity_id: '{{ sonos_entity }}'
      message: '{{ message }}'
    service: tts.google_translate_say
  - delay: '{{ delay|default(''00:00:00'') }}'
  - choose:
    - conditions:
      - condition: state
        entity_id: input_boolean.sonos_was_saved
        state: 'on'
        - data_template:
            entity_id: '{{ sonos_entity }}'
          service: sonos.restore
        - service: input_boolean.turn_off
          entity_id: input_boolean.sonos_was_saved # reset ready for next time

Nice catch with the boolean
I am getting two errors
Error loading /home/homeassistant/.homeassistant/configuration.yaml: while parsing a block mapping
in “/home/homeassistant/.homeassistant/scripts.yaml”, line 67, column 9
expected , but found ‘-’
in “/home/homeassistant/.homeassistant/scripts.yaml”, line 70, column 9

It should be the intentation? But I cannot understand which.

Oops, I forgot to put in sequence: in the choose actions.

sonos_say:
  alias: Sonos TTS script
  sequence:
  - choose:
    - conditions:
      - condition: state
        entity_id: media_player.sonos_one
        state: playing
      - sequence:
        - data_template:
            entity_id: '{{ sonos_entity }}'
          service: sonos.snapshot
        - service: input_boolean.turn_on
          entity_id: input_boolean.sonos_was_saved
  - data_template:
      entity_id: '{{ sonos_entity }}'
      volume_level: '{{ volume }}'
    service: media_player.volume_set
  - data_template:
      entity_id: '{{ sonos_entity }}'
      message: '{{ message }}'
    service: tts.google_translate_say
  - delay: '{{ delay|default(''00:00:00'') }}'
  - choose:
    - conditions:
      - condition: state
        entity_id: input_boolean.sonos_was_saved
        state: 'on'
      - sequence:
        - data_template:
            entity_id: '{{ sonos_entity }}'
          service: sonos.restore
        - service: input_boolean.turn_off
          entity_id: input_boolean.sonos_was_saved # reset ready for next time

I am still getting an error
Error loading /home/homeassistant/.homeassistant/configuration.yaml: mapping values are not allowed here
in “/home/homeassistant/.homeassistant/scripts.yaml”, line 63, column 11

Any clue on that?

Which line is line 63?

I have found the mistake.
I was missing the line sonos_say:

Now I am getting a bunch of things
Invalid config for [script]: Entity ID {{ sonos_entity }} is an invalid entity id for dictionary value @ data[‘script’][‘sonos_say’][‘sequence’][0][‘choose’][0][‘conditions’][0][‘entity_id’]. Got ‘{{ sonos_entity }}’
required key not provided @ data[‘script’][‘sonos_say’][‘sequence’][0][‘choose’][0][‘sequence’]. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 99).

Just to let you know that I have created the input boolean under the helpers as toggle.

I assumed you were passing that to the script as a variable. If not:

sonos_say:
  alias: Sonos TTS script
  sequence:
  - choose:
    - conditions:
      - condition: state
        entity_id: media_player.sonos_one
        state: playing
      - sequence:
        - data_template:
            entity_id: media_player.sonos_one
          service: sonos.snapshot
        - service: input_boolean.turn_on
          entity_id: input_boolean.sonos_was_saved
  - data_template:
      entity_id: media_player.sonos_one
      volume_level: '{{ volume }}'
    service: media_player.volume_set
  - data_template:
      entity_id: media_player.sonos_one
      message: '{{ message }}'
    service: tts.google_translate_say
  - delay: '{{ delay|default(''00:00:00'') }}'
  - choose:
    - conditions:
      - condition: state
        entity_id: input_boolean.sonos_was_saved
        state: 'on'
      - sequence:
        - data_template:
            entity_id: media_player.sonos_one
          service: sonos.restore
        - service: input_boolean.turn_off
          entity_id: input_boolean.sonos_was_saved # reset ready for next time

So all you have to pass to the script now is the volume level and message.

Yes, but I want this to be passed as a variable because I have multiple Sonos.

Why were you only creating a snapshot of media_player.sonos_one then?

It was a typo.
That is what II have and is not working

sonos_say:
  alias: Sonos TTS script
  sequence:
  - choose:
    - conditions:
      - condition: state
        entity_id: '{{ sonos_entity }}'
        state: 'playing'
      - sequence:
        - data_template:
            entity_id: '{{ sonos_entity }}'
          service: sonos.snapshot
        - service: input_boolean.turn_on
          entity_id: input_boolean.sonos_snapshot
  - data_template:
      entity_id: '{{ sonos_entity }}'
      volume_level: '{{ volume }}'
    service: media_player.volume_set
  - data_template:
      entity_id: '{{ sonos_entity }}'
      message: '{{ message }}'
    service: tts.google_translate_say
  - delay: '{{ delay|default(''00:00:00'') }}'
  - choose:
    - conditions:
      - condition: state
        entity_id: input_boolean.sonos_snapshot
        state: 'on'
      - sequence:
        - data_template:
            entity_id: '{{ sonos_entity }}'
          service: sonos.restore
        - service: input_boolean.turn_off
          entity_id: input_boolean.sonos_snapshot

I might be wrong, but at a guess you might only be able to pass variables to the main sequence, not the choose sequences. Not sure.

You should be able to use the passed variable everywhere.

@03397
Can you please show one of the automations that calls this script? How did you test the script? Did you just execute it (this will fail as the variables don’t exist then) or did you call it from an automation?

I am not running it, I ust check the configuration in the validation like I was doing before the change.

Ok, now I see the issue. There are multiple things wrong, first you need to change your state condition to a template condition in order to use the template. Seconds, the dash before sequence needs to be removed. try like this:

sonos_say:
  alias: Sonos TTS script
  sequence:
  - choose:
    - conditions: "{{ states(sonos_entity) == 'playing' }}"
      sequence:
        - data_template:
            entity_id: '{{ sonos_entity }}'
          service: sonos.snapshot
        - service: input_boolean.turn_on
          entity_id: input_boolean.sonos_snapshot
  - data_template:
      entity_id: '{{ sonos_entity }}'
      volume_level: '{{ volume }}'
    service: media_player.volume_set
  - data_template:
      entity_id: '{{ sonos_entity }}'
      message: '{{ message }}'
    service: tts.google_translate_say
  - delay: '{{ delay|default(''00:00:00'') }}'
  - choose:
    - conditions:
        - condition: state
          entity_id: input_boolean.sonos_snapshot
          state: 'on'
      sequence:
        - data_template:
            entity_id: '{{ sonos_entity }}'
          service: sonos.restore
        - service: input_boolean.turn_off
          entity_id: input_boolean.sonos_snapshot

Also if you are on 0.116 or later you can change all the data_template to data.

Thank you all guys for everything. Finally, compiled ok and it works.

Wow. I made a mess of that. In my defence it was 3am.

FYI, unless you are likely to add extra actions to the end of the script you can simply the last choose action to a condition.

sonos_say:
  alias: Sonos TTS script
  sequence:
  - choose:
    - conditions: "{{ states(sonos_entity) == 'playing' }}"
      sequence:
        - data_template:
            entity_id: '{{ sonos_entity }}'
          service: sonos.snapshot
        - service: input_boolean.turn_on
          entity_id: input_boolean.sonos_snapshot
  - data_template:
      entity_id: '{{ sonos_entity }}'
      volume_level: '{{ volume }}'
    service: media_player.volume_set
  - data_template:
      entity_id: '{{ sonos_entity }}'
      message: '{{ message }}'
    service: tts.google_translate_say
  - delay: '{{ delay|default(''00:00:00'') }}'
  - condition: state
    entity_id: input_boolean.sonos_snapshot
    state: 'on'
  - data_template:
      entity_id: '{{ sonos_entity }}'
    service: sonos.restore
  - service: input_boolean.turn_off
    entity_id: input_boolean.sonos_snapshot