Actions in an intent script with a delay?

I’d like to add a delay between two actions in an intent script, but cannot get it to work.

Two actions below (turn on TV, switch to HDMI2) work in this pattern:

TVControl:
  action:
    - service_template: >
        {% if is_state('media_player.front_room_tv', 'off') %}
          media_player.turn_on
        {% endif %}
      data_template:
        entity_id: >
          {% if is_state('media_player.front_room_tv', 'off') %}
            media_player.front_room_tv
          {% endif %}
    - service_template: >
        media_player.play_media
      data_template:
        entity_id: >
          media_player.front_room_tv
        media_content_type: >
            send_key
        media_content_id: >
            ST_HDMI2

I want a 5 second delay before executing the second action, so I assume adding a delay: 5 would work, like this:

TVControl:
  action:
    - service_template: >
        {% if is_state('media_player.front_room_tv', 'off') %}
          media_player.turn_on
        {% endif %}
      data_template:
        entity_id: >
          {% if is_state('media_player.front_room_tv', 'off') %}
            media_player.front_room_tv
          {% endif %}
    - delay: 5
      service_template: >
        media_player.play_media
      data_template:
        entity_id: >
          media_player.front_room_tv
        media_content_type: >
            send_key
        media_content_id: >
            ST_HDMI2

But the second script fails to load in home assistant due to the delay line. Could someone point out the way to simple delay an action in this scenario?

I know, as an alternative, I could trigger a timer and have an automation finish the delayed action, but that is more messy, so I’d like to know if there is a way to do the simple delay above instead.

delay is either “hh:mm:ss”, or

delay:
  seconds: 5

Also, change service_template and data_template to just ‘service’ and ‘data’ now. They’ve been depricated as of 0.115, so might as well change em now.


Actually, this whole thing needs reworked. You are going to throw errors if the tv is already on and it wont do anything since you’ll have an empty service call. So it wont even try to change the HDMI2 command.

TVControl:
  action:
    # If the TV is off, turn it on then wait 5 seconds before doing the next thing
    - choose:
        condition:
          condition: state
          entity_id: media_plaer.front_room_tv
          state: 'off'
        sequence:
          - service: media_player.turn_on
            entity_id: media_player.front_room_tv
          # Wait 5 seconds for the TV to turn on.
          - delay: 
              seconds: 5
    - service: media_player.play_media
      data:
        entity_id: media_player.front_room_tv
        media_content_type: "send_key"
        media_content_id: "ST_HDMI2"

Now if the TV is already on, it will just change to HDMI 2 immediately. Else, it will turn it on, wait for 5 seconds, THEN turn it to hdmi2

1 Like