Script, turn TV 'on' only if it's 'off'

Hi,

I’m new with this and I’m having trouble understanding conditionals within scripts.

I need a script with this steps:

  1. IF the receiver is ‘off’ THEN turn it on ELSE do nothing / move on
  2. Turn ON the PC
  3. Select the PC source in the receiver
  4. IF the tv is ‘off’ THEN turn it on ELSE do nothing

I have the script working but without the conditionals:

scripts.yaml:

tv_sugo:
  alias: TV SUGO
  sequence:
    - alias: Yamaha on
      service: media_player.turn_on
      data:
        entity_id: media_player.yamaha_receiver
    - alias: SUGO on
      service: switch.turn_on
      data:
        entity_id: switch.sugo
    - alias: Source SUGO
      service: media_player.select_source
      data:
        entity_id: media_player.yamaha_receiver
        source: PC2
    - alias: TV on
      service: switch.turn_on
      data:
        entity_id: switch.tv_lg

I need the conditional on the TV because it works as a toggle, so it will turn it OFF if it’s already ON. I tried using service_template but I don’t need ANY service if the TV is ON. I tried surrounding the entire alias object with an {% if %} but it seems that’s not a valid syntax. I tried conditions but it will prevent the script to continue (I have other conditions in the middle, this is just a simplified example).

So, how can I conditionally “add” or “remove” steps from the sequence?

Sorry if this had already been answered.

Thanks in advanced,
azeós.

    - alias: TV on
      - condition: state
        entity_id: switch.tv_lg
        state: 'off'
      - service: switch.turn_on
        data:
          entity_id: switch.tv_lg

You must do this as the last step though as your script will stop at the condition if your TV is on. The way you have it is good.

EDIT: I haven’t actually used aliases inside scripts before so I hope this is correct. if not, drop the aliases as I know this works, I have very similar scripts:

  alias: TV SUGO
  sequence:
    - service: media_player.turn_on
      data:
        entity_id: media_player.yamaha_receiver
    - service: switch.turn_on
      data:
        entity_id: switch.sugo
    - service: media_player.select_source
      data:
        entity_id: media_player.yamaha_receiver
        source: PC2
    - condition: state
      entity_id: switch.tv_lg
      state: 'off'
    - service: switch.turn_on
      data:
        entity_id: switch.tv_lg

Thanks for the quick reply.

Sorry if I wasn’t clear, the problem is that I have other conditions in the middle. So, as you suggested, I can use conditions but only for the last step.

Let’s assume that I need to check if the receiver is on (the first step) AND other stuff in the middle.

Then what will be the approach?

Thanks again.

You could call a separate script for each device that needs conditions.

 alias: TV SUGO
  sequence:
    - service: script.turn_on
       entity_id: script.tv_on
    - service: script.turn_on
       entity_id: script.next_device_etc
tv_on
  sequence:
    - condition: state
       entity_id: switch.tv_lg
       state: 'off'
     - service: switch.turn_on
       data:
         entity_id: switch.tv_lg

The main script will run the whole way through as it has no conditions.

You can use service_templates for this, take this example I use:

tv_on:
  sequence:
      # Check if TV is powered down, "unknown" and turn on power board #1
    - service_template: >-
        {%- if (is_state('media_player.sharp_tv_remote_192_168_10_125', 'off')) or (is_state('media_player.sharp_tv_remote_192_168_10_125', 'on')) -%}
          script.do_nothing
        {%- else -%}
          switch.broadlink_send_packet_192_168_10_199
        {%- endif %}
      data:
        packet:
          - "sgDCAA8aGw0bDRsNGw0PGhsNDgABeQ4MDgwPDA4MDwwOhhwNGw0OGhsNGw0cDBsNGw0PGhsNGw0bDRsNDhsbDQ4aDxoOGw4aDhsOGg4bDhoOGxsMDxoOGhwNDhoPGhsNGw0bDRsNDhocDQ4AAXkODA4NDgwODQ4MD4YaDRwMDxobDRsNGw0bDRsNDxobDRsNHAwbDQ8aGw0OGg8aDhoPGg4aDxoOGg8aDhsbDA8aDhocDQ4aDxobDRsNGw0bDQ4aHA0OAAXcAAAAAAAA"
    - delay: '00:00:01'
      # Check if TV is powered down, "unknown" and turn on power board #2
    - service_template: >-
        {%- if (is_state('media_player.sharp_tv_remote_192_168_10_125', 'off')) or (is_state('media_player.sharp_tv_remote_192_168_10_125', 'on')) -%}
          script.do_nothing
        {%- else -%}
          switch.broadlink_send_packet_192_168_10_199
        {%- endif %}
      data:
        packet:
          - "sgCYABwNGw0NGxsNGw0bDRsNDhsbDQ4aDhsOGg4bDhoOGw4aDhsOGg4bGw0OGhsNDhsOGhsNGw0OGxsNDhobDQ4AAXkPDA4MDwwODA4NDoYbDRsNDhocDBwNGw0bDRsNDhobDRsOGwwcDA4bGw0OGg8aDhoPGg4aDxoOGg4bDhoOGxsNDhobDg4aDhsbDBwNDhobDQ4bGg4OAAXc"
      #Wait for TV to become off before continuing
    - delay: '00:00:02'
      # Turn on Amplifier
    - service: switch.broadlink_send_packet_192_168_10_152
      data:
        packet:
          - "JgBoAQABKJEUNRQRFDUUERQRFBAUERQ1FDYUEBQ2FDUUERQ1FDUVNRQRFDUUERQQFBEUERQQFRAUNRUQFDUVNRQ1FDUVNRQ1FAAFJgABKJAVNRQRFDUUERQQFRAUERQ1FDUVEBU1FDUUERQ1FDUVNRQQFTUUERQQFBEUERQQFRAUNRUQFDUVNRQ1FDYUNRQ1FQAFJQABKJAVNRQQFTUUERQQFBEUERQ1FDUVEBQ1FTUUEBU1FDUUNRUQFDUVEBQRFBEUEBUQFBEUNRQRFDUUNRU1FDUUNRU1FAAFJQABKJEUNRQRFDUVEBQRFBAVEBQ1FTUUEBU1FDUUERQ1FDYUNRQRFDUUERQQFRAUERQQFRAUNRUQFTUUNRQ1FTUUNRQ1FQAFJQABJ5EUNhQQFTUUEBUQFBEUERQ1FDUUERQ1FDUVEBM3EzYTNhMSEzYUERMSExEUERMSExITNhMSEzYTNxI2FDYTNhM3EwANBQ=="
      # Wait until TV is 'off' and then turn on. Abandon if TV doesnt appear, or is already on.
    - wait_template: "{{ (states.media_player.sharp_tv_remote_192_168_10_125.state == 'off') }}"
      timeout: '00:00:60'
      continue_on_timeout: 'false'
      # Turn media player on
    - service: media_player.turn_on
      entity_id: media_player.sharp_tv_remote_192_168_10_125

You need a script.do_nothing, which contains nothing so that nothing happens.

1 Like

just add a condition before turning the tv on.

tv_sugo:
  alias: TV SUGO
  sequence:
    - alias: Yamaha on
      service: media_player.turn_on
      data:
        entity_id: media_player.yamaha_receiver
    - alias: SUGO on
      service: switch.turn_on
      data:
        entity_id: switch.sugo
    - alias: Source SUGO
      service: media_player.select_source
      data:
        entity_id: media_player.yamaha_receiver
        source: PC2
    - condition: state
      entity_id: switch.tv_lg
      state: 'off'
    - alias: TV on
      service: switch.turn_on
      data:
        entity_id: switch.tv_lg

it’ll only move forward if the condition is met. Otherwise it won’t.

EDIT: for some reason this thread didn’t fully load. Sorry if i’m posting things that others have said.

Excellent!

I tried both solutions (yours and @callifo one) and I liked yours for long scripts, I think it’s cleaner.

Thanks all of you for the responses. Great community.

Hello azeos, Nice that you were helped here in the forum, have also received much help here. If you look at “Node-RED”, maybe this will be useful too …