WebOS: Turning on and conditionally adding delays

Hello!

I made a simple script for my WebOS TV:
Turn the TV ON, change the source and set the channel.

Initially it was simple:

  1. Turn TV ON
  2. Delay for 30 seconds (while TV boots up)
  3. Change Source
  4. Delay for 10 seconds (while app loads)
  5. Change channel.

But when the TV is already ON or in the correct source, it makes no sense to wait for 30~40 seconds for a channel change. So I wanted to reduce the delay and I tried this:

  alias: LG - Channel two
  sequence:
  - data: {}
    entity_id: media_player.lg_sala
    service: media_player.turn_on
  - delay: '{% if is_state(''media_player.lg_sala'', ''on'') -%}   ''00:00:01'' {%-
      else -%}   ''00:00:30'' {%- endif %}'
  - data:
      source: Smart IPTV
    entity_id: media_player.lg_sala
    service: media_player.select_source
  - delay: '{% if is_state_attr(''media_player.lg_sala'', ''source'', ''Smart IPTV'')
      -%}   ''00:00:01'' {%- else -%}   ''00:00:10'' {%- endif %}'
  - data:
      button: '2'
    entity_id: media_player.lg_sala
    service: webostv.button
  mode: single

But the problem is that with this logic the TV will report as ON imediatelly after my command to turn it ON and the delay template won’t ever return 30 seconds. I’m looking for a script that should be like this:

  1. If TV is ON
    -Wait 1 second
    Else
    -Turn ON
    -Wait 30 seconds
  2. If TV is on correct source
    -Wait 1 second
    Else
    -Change source
    -Wait 10 seconds
  3. Change channel

My main motivation for this is having only a single command exposed to Google Assistant that can be used to either turn the TV on or just change channels.

Can I do this via script, should I try multiple scripts or? Is there a way to set a delay time, do an action and then delay?

This is a perfect candidate for the new choose action in 0.113! :smiley:

  alias: LG - Channel two
  sequence:
  # If TV is off, turn it on and delay 30 sec
  - choose:
    - conditions:
      - condition: state
        entity_id: media_player.lg_sala
        state: 'off'
      sequence:
      - data:
          source: Smart IPTV
        entity_id: media_player.lg_sala
        service: media_player.select_source
      - delay: 30
  # If source is not "Smart IPTV", select it and delay 10 sec
  - choose:
    - conditions:
      - condition: template
        value_template: >
          {{ not is_state_attr('media_player.lg_sala', 'source', 'Smart IPTV') }}
      sequence:
      - data:
          source: Smart IPTV
        entity_id: media_player.lg_sala
        service: media_player.select_source
      - delay: 10
  # Change channel
  - data:
      button: '2'
    entity_id: media_player.lg_sala
    service: webostv.button
  mode: single
1 Like

Well, I’m amazed, thank you very much!

I was just checking the UI options and totally forgot about this new implementation.

It’s dead simple now:

  alias: LG - Channel two
  sequence:
  # If TV is off, do the full run
  - choose:
    - conditions:
      - condition: state
        entity_id: media_player.lg_sala
        state: 'off'
      sequence:
      - data: {}
        entity_id: media_player.lg_sala
        service: media_player.turn_on
      - delay: 30
      - data:
          source: Smart IPTV
        entity_id: media_player.lg_sala
        service: media_player.select_source
      - delay: 10
  # If TV is on and source is not "Smart IPTV", select it and delay 10 sec
  - choose:
    - conditions:
      - condition: template
        value_template: >
          {{ not is_state_attr('media_player.lg_sala', 'source', 'Smart IPTV') }}
      - condition: state
        entity_id: media_player.lg_sala
        state: 'on'
      sequence:
      - data:
          source: Smart IPTV
        entity_id: media_player.lg_sala
        service: media_player.select_source
      - delay: 10
  # Always change channel
  - data:
      button: '2'
    entity_id: media_player.lg_sala
    service: webostv.button
  mode: single
1 Like