Script Action with Condition (Is This A Thing?)

Is it possible to have a script action that only occurs if a condition is true? I.e. only fade out pioneer receiver if it’s on?

boom_start_playlist:
  sequence:
    - service: script.turn_on
      entity_id: script.pioneer_fade_to_zero
      conditions:
        condition: state
          entity_id: media_player.pioneer_receiver
          state: "on"

I get:

Error loading /config/configuration.yaml: mapping values are not allowed here
in “/config/scripts.yaml”, line 207, column 20

If the State Condition evaluates to true then the next action (script.turn_on) is executed. If it evaluates to false then nothing else happens and the script ends.

boom_start_playlist:
  sequence:
    - condition: state
      entity_id: media_player.pioneer_receiver
      state: "on"
    - service: script.turn_on
      entity_id: script.pioneer_fade_to_zero


EDIT

If you need more flexibility (execute some service calls but not others) then I suggest you use choose.

@123, in your example all execution will stop if the condition is not met, correct? So what if there are subsequent steps that should run? The idea here is to fade the amp out if it’s on, then switch the playlist, and then fade it back up. But if it’s off, I want to turn it on and then fade it in. Here’s my full attempt…

boom_start_playlist:
  sequence:
    - service: script.turn_on
      entity_id: script.pioneer_fade_to_zero
      condition: state
        entity_id: media_player.pioneer_receiver
        state: "on"
    - alias: "Wait until pioneer volume is zero"
      wait_template: "{{ is_state_attr('media_player.pioneer_receiver','volume_level', 0) }}"
      condition: state
        entity_id: media_player.pioneer_receiver
        state: "on"
    - service: switch.turn_on
      data:
        entity_id:
        - switch.harmony_hub_boom
      condition: state
        entity_id: media_player.boom_2
        state: "off"
    - delay: '00:00:03'
    - service: squeezebox.call_method
      data:
        entity_id: media_player.boom_2
        command: playlist
        parameters:
          - stop
    - service: media_player.volume_set
      data:
        entity_id: media_player.pioneer_receiver
        volume_level: '0.30'
    - service: squeezebox.call_method
      data:
        entity_id: media_player.boom_2
        command: playlist
        parameters:
          - play
          - >-
            https://a.playlist.url/example
          - Playlist
          - 5

So, in English…

Fade the amp if it’s on. Wait until it’s at 0 if it’s on. Turn it on if it’s off. Play the playlist.

As such, I want to attach conditions to the action items instead of halting all execution if a condition is not met.

Your original question didn’t mention anything about “subsequent steps”. That’s why I provided an example with a simple, inline State Condition. However, I did add that if you need more flexibility (meaning more than a simple inline condition) then you should use choose.

For example, this will execute script.pioneer_fade_to_zero only if the receiver is on. If it is off it immediately proceeds to the wait_template.

boom_start_playlist:
  sequence:
    - choose:
        - conditions: "{{ is_state('media_player.pioneer_receiver', 'on') }}"
          sequence:
            - service: script.pioneer_fade_to_zero
    - alias: "Wait until pioneer volume is zero"
      wait_template: "{{ is_state_attr('media_player.pioneer_receiver','volume_level', 0) }}"
    ... etc ...

Final working script:

boom_start_playlist:
  sequence:
    - choose:
        - conditions: "{{ is_state('media_player.pioneer_receiver', 'on') }}"
          sequence:
            - service: script.pioneer_fade_to_zero
    - choose:
        - conditions: "{{ is_state('media_player.pioneer_receiver', 'on') }}"
          sequence:
            - alias: "Wait until pioneer volume is zero"
              wait_template: "{{ is_state_attr('media_player.pioneer_receiver','volume_level', 0) }}"
    - choose:
        - conditions: "{{ is_state('media_player.boom_2', 'playing') }}"
          sequence:
            - service: squeezebox.call_method
              data:
                entity_id: media_player.boom_2
                command: stop
    - choose:
        - conditions: "{{ is_state('media_player.boom_2', 'off') }}"
          sequence:
            - service: switch.turn_on
              data:
                entity_id: switch.harmony_hub_boom
            - service: squeezebox.call_method
              data:
                entity_id: media_player.boom_2
                command: stop
    - service: media_player.volume_set
      data:
        entity_id: media_player.pioneer_receiver
        volume_level: '0.30'
    - service: squeezebox.call_method
      data:
        entity_id: media_player.boom_2
        command: playlist
        parameters:
          - play
          - >-
            https://a.playlist.url/example
          - Playlist
          - 5

And here is the fade out script…

pioneer_fade_to_zero:
  sequence:
    - alias: "Check the Pioneer is on, else we can bail."
      condition: state
      entity_id: media_player.pioneer_receiver
      state: "on"
    - alias: "Set Variables"
      variables:
        step: 0.01
    - alias: "Turn the amp down AS LONG AS the conditions are true"
      repeat:
        while:
          - condition: template
            value_template: "{{ states.media_player.pioneer_receiver.attributes.volume_level | round(2) > 0 }}"
        sequence:
          - service: media_player.volume_set
            data_template:
              entity_id: media_player.pioneer_receiver
              volume_level: >
                {% set n = states.media_player.pioneer_receiver.attributes.volume_level %}
                {{ n - step }}
    - alias: "Set it to exactly zero in case of a math issue."
      service : media_player.volume_set
      data:
        entity_id: media_player.pioneer_receiver
        volume_level: 0

If the system is already playing a stream, it will fade down and then change streams and fade up. If it is off, it will turn on, and then start a stream and fade up. Squeezebox has a fade in parameter but not a fade out, hence the need to manually fade the receiver. Nice effect overall! Thanks, @123 !

1 Like

Hi, I hava a similar problem and can’t find my failure.
I want to use a condition - input_number.xxx != 0 - in a script.

alias: script_irrigation_zone_1_start
sequence:
  - service: rainbird.start_irrigation
    data:
      duration: '{{ states(''input_number.helper_irrigation_zone_1_duration'') | int }}'
      entity_id: switch.irrigation_zone_1
    condition: numeric-state
      entity_id: input_number.helper_irrigation_zone_1_duration
      above: 0
mode: restart
icon: mdi:sprinkler

It throws an error massage “Unexpectes value for condition”
Regards,
Eckart

The Numeric State Condition in your example contains syntax errors. Posted below is the corrected version. However, I don’t understand why you put the condition at the end of the script. There are no actions after it so it doesn’t matter what is the condition’s result because it’s the end of the script anyway.

alias: script_irrigation_zone_1_start
sequence:
  - service: rainbird.start_irrigation
    data:
      duration: '{{ states(''input_number.helper_irrigation_zone_1_duration'') | int }}'
      entity_id: switch.irrigation_zone_1
  - condition: numeric_state
    entity_id: input_number.helper_irrigation_zone_1_duration
    above: 0
mode: restart
icon: mdi:sprinkler

Thank you. Conditions at the beginning of the script and your code: It works! Regards, Eckart