Templating question: Delay within template syntax

Hi, I have the following switch defined:

turn_on: 
        - service: >
            {% if is_state('switch.sonoff_8', 'off') %}
              switch.turn_on
              [DELAY HERE!]
            {% endif %}
          entity_id: switch.sonoff_8

        - service: switch.turn_on
          entity_id: switch.sonoff_7

        - service: device_tracker.see
          data:
            dev_id: hisense_tv
            location_name: home

I need a delay at the specified location which only gets triggered if the if-condition was true. how do I do this? Thanks!

You can’t do this.
You could use the choose action. But what are you showing here, an automation or a configuration for an entity?

It’s a configuration for a template switch entity.

I assume that you can’t use the choose avtion here,but you’d need to try.

No, I think he just needs to define a switch.
And then when he uses the switch he can build the required delay
If it always has the same ‘action’ then write it into a script and call the script instead of the switch

What is the use case for this ?
There may be a better way to achieve the same result

1 Like

The use case is the following:
I have two Tasmota switches.
One is the “TV Master”, it switches on/off the power for everything related to the TV (Receiver, Subwoofer, BluRay Player). For the TV there is an extra Tasmota switch called “TV” , because the TV is configured to switch on immediately when it has Power.

Now, if TV Master is off and I want to turn on the TV, I would have to turn on TV Master first, then wait a few seconds till the TV Tasmota Switch connected to my WiFi, and then also switch it on.

Fair enough, I actually have a similar use case.
But it’s just a delayed power on for kodi when HA sees that my TV is on
And a similar action on switch off except that the switch to turn the actual power of its fake, it interrupts the switch off and shuts down kodi first, waits 20 secs then cuts the power.

I can’t imagine you doing these independently so just have a switch on automation with your delay in it and a switch off automation reversing the action.
Both automations could be driven by your template switch
They’d be really simple and easy to maintain so I wouldn’t bother trying to get them into one.
Remember the KISS principle.
Good luck

just make a script that has that condition

script:
  switch_sonoff_8:
    sequence:
      - condition: state
        entity_id: switch.sonoff_8
        state: 'on'
      - delay: 00:00:05
      - service: switch.turn_on
        entity_id: switch.sonoff_8

Then change your other to

     turn_on: 
        - service: script.switch_sonoff_8
          entity_id: switch.sonoff_8

        - service: switch.turn_on
          entity_id: switch.sonoff_7

        - service: device_tracker.see
          data:
            dev_id: hisense_tv
            location_name: home
1 Like

Thanks for the idea, I solved it now like this:

scripts.yaml:

# TV Master
switch_tv_master:
  sequence:
    - condition: state
      entity_id: switch.sonoff_7  # TV plug
      state: 'unavailable'
    - service: switch.turn_on
      entity_id: switch.sonoff_8  # Master plug
    - wait_template: "{{ not is_state('switch.sonoff_7', 'unavailable') }}"

The wait template ensures the smallest delay possible.

switches_hisense.yaml

- platform: template
  switches:
    hisense_tv:
      turn_on: 
        - service: script.switch_tv_master
        - service: switch.turn_on
          entity_id: switch.sonoff_7
        - service: device_tracker.see
          data:
            dev_id: hisense_tv
            location_name: home