Addition of condition in script

Guys, I have the following script.
I want to add this condition
{{ ‘RINCON’ in state_attr(‘media_player.sonos_one’,‘media_content_id’) }}

Only when this is TRUE the script should be executed. How can I add 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
  mode: queued
  max: 10

What not add that condition to whatever calls the script?

In other words, don’t even call the script if it’s asked to do something it doesn’t do.

If that’s impractical, simply add the condition as the first action in the sequence.

sonos_say:
  alias: Sonos TTS script
  sequence:
  - conditions: "{{ 'RINCON' in state_attr('media_player.sonos_one', 'media_content_id') }}"
  - choose:
     ... etc ...

It is impractical since the script is called inside over 20 automations.

Thanks I will try your suggestion.

After adding this as suggested I am getting an error after “Check Configuration”

Invalid config for [script]: [conditions] is an invalid option for [script]. Check: script->script->sonos_say->sequence->0->conditions. (See /home/homeassistant/.homeassistant/configuration.yaml, line 104).

I don’t think home assistant will accept a condition as the first action of a script or automation. I’ve seen this a few times on the forum.

As a test try:

sonos_say:
  alias: Sonos TTS script
  sequence:
  - delay: 
      seconds: 1
  - conditions: "{{ 'RINCON' in state_attr('media_player.sonos_one', 'media_content_id') }}"
  - choose:
     ... etc ...
1 Like

Tricky! I didn’t know it was averse to delay condition as the first action.


EDIT

brain fart …

It’s not, it’s averse to condition as the first action. The delay is the workaround.

1 Like

@123 @tom_l you can have a condition as the first action in a script. I do it all the time. The problem is a typo

  - condition: "{{ 'RINCON' in state_attr('media_player.sonos_one', 'media_content_id') }}"`

or simply

  - "{{ 'RINCON' in state_attr('media_player.sonos_one', 'media_content_id') }}"`

EDIT: It seems like the UI does not like this configuration method, so if you’re using the UI you have to use the old notation

  - condition: template
    value_template: "{{ 'RINCON' in state_attr('media_player.sonos_one', 'media_content_id') }}"

Otherwise, if you’re not… you can use the shorthand versions.

1 Like

Thanks guys.
Working like a charm

Oh. :man_facepalming:

2 Likes

good to know!