Help request: Add a delay inside a Service_template

Hi there.
Thanks in advance.

I’ve been following the YouTube ‘Domotica Solar’ guide on automating the heaters and boiler at home, but I have a problem.

I need to add a 5-minute delay between the thermostat change to “heating” and the boiler starts, because I need the valve to fully open, to allow water circulation.

This is the action I’m trying to change:

service_template: >
  {% if is_state_attr('climate.calefaccion_gamer','hvac_action','heating') or
  is_state_attr('climate.calefaccion_hab_1','hvac_action','heating') or
  is_state_attr('climate.calefaccion_hab_2','hvac_action','heating') or
  is_state_attr('climate.calefaccion_hab_ppal','hvac_action','heating') %}
    switch.turn_on
  {% else %}
    switch.turn_off
  {% endif %}  
entity_id: switch.caldera

I must add a delay after the template is considered “true” and the switch is turned on.

Any ideas?
Thanks for the support!!!

Post the whole automation… there are a few ways to approach this, but they aren’t all going to be appropriate in every circumstance.

Thanks for the quick answer. In any case, after posting I got inspired… Now its working fine:

alias: Encender caldera a demanda
description: ""
triggers:
  - trigger: state
    entity_id:
      - climate.calefaccion_gamer
      - climate.calefaccion_hab_1
      - climate.calefaccion_hab_2
      - climate.calefaccion_hab_ppal
      - input_boolean.calefaccion_general
    attribute: hvac_action
conditions:
  - condition: state
    entity_id: input_boolean.calefaccion_general
    state: "on"
actions:
  - if:
      - condition: template
        value_template: >-
          {{ is_state_attr('climate.calefaccion_gamer','hvac_action','heating')
          or
            is_state_attr('climate.calefaccion_hab_1','hvac_action','heating') or
            is_state_attr('climate.calefaccion_hab_2','hvac_action','heating') or
            is_state_attr('climate.calefaccion_hab_ppal','hvac_action','heating') }}
    then:
      - delay:
          hours: 0
          minutes: 5
          seconds: 0
          milliseconds: 0
      - action: switch.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: switch.caldera
    else:
      - action: switch.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: switch.caldera
mode: single

If you have any suggestions to improve, they are more than welcome!

Another option would be to set up an template binary sensor with a delay_on like:

template:
  - binary_sensor:
      - name: Demanda Calefacción
        state: |
          {{ 'heating' in ["climate.calefaccion_gamer","climate.calefaccion_hab_1",
          "climate.calefaccion_hab_2","climate.calefaccion_hab_ppal"]
          | map('state_attr', 'hvac_action') | list }}
        delay_on: "00:05:00"

Then your automation would be:

alias: Encender caldera a demanda
description: ""
triggers:
  - trigger: state
    entity_id: binary_sensor.demanda_calefaccion
    to:
      - "on"
      - "off"
conditions:
  - condition: state
    entity_id: input_boolean.calefaccion_general
    state: "on"
actions:
  - action: switch.turn_{{ trigger.to_state.state }}
    metadata: {}
    data: {}
    target:
      entity_id: switch.caldera
mode: single