Waiting if the TV was off

My TV needs about 10 seconds before it can change sources (from TV to Netflix for example), but the change is instantaneous if the TV is already on.
So I want to have a condition that check if the TV is off, and if this is the case to turn on and wait 10 seconds before changing source. But I want no delay if the TV is on.

This is my actual configuration for watching Netflix for example. The delay of 1 second is there because the Yamaha need a little time after turned on to change audio source. As this is just one second there is no problem for the delay from one to another source. With this script I need to ask the same order two times for the source to change. One time the TV turns on. After at least seconds I ask again and then the source is changes.

I´ve been trying for hours different things with no success…
Help please.

  netflix:
    alias: Netflix
    sequence:
    - entity_id: media_player.lgtv
      service: media_player.turn_on
    - entity_id: switch.yamaha_power
      service: switch.turn_on
    - delay: 00:00:01
    - entity_id: media_player.lgtv
      service: media_player.select_source
      data:
        source: Netflix
    - entity_id: switch.yamaha_scene_1
      service: switch.turn_on
    - entity_id: script.actividades
      service: script.turn_on

Probably the easiest way to handle this is to make the delay conditional on whether or not the TV was on. But, of course, by the time you get to that step the TV has already been turned on. But you can test how long the TV has been on and use that instead. So maybe changing the delay step to something like this might work for you:

  - delay: >
      {% set lgtv_on = (now() - states.media_player.lgtv.last_changed).total_seconds()|int %}
      {% if lgtv_on < 9 %}
        {{ 10 - lgtv_on }}
      {% else %}
        1
      {% endif %}

Caveat: I don’t use media_player’s, so I’m not 100% sure this will work. But if the state changes when the TV is turned on, it probably should.

What happens if you call the service to change the source too early? If it does eventually change and you just want to wait until that is done, you could use a wait template. This would also work if your TV takes ten seconds to show that it’s on after powering on.

Coincidentally the example in the docs is for a media player!