Using Delay with Templates in an Automation

I have an away mode set up that puts a manual thermostat into an “away” mode (think: house doesn’t get above 30 celsius or below 16 celsius) when I’m gone. However, I am trying to set up a way to trigger it so that the away mode shuts off as much as an hour before I return home so that the house is actually warm.

I’ve created the below automation; I get an error at the Delay portion. Is Delay not able to use if/elif statements, and if so then do you guys have some advice on what I’m trying to do?

- id: returninghome
  alias: Returning Home Mode
  trigger:
    - platform: state
      entity_id: input_select.tam_status, input_select.sbm_status
      to: 'Travelling Home'
  condition:
    - condition: state
      entity_id: input_select.family_status
      state: "Away"
    - condition: template
      value_template: "{{ (states('input_number.thermostat_temp') | int - state_attr('climate.upstairs', 'temperature') |int ) | abs > 3 }}"
  action:
    - service: homeassistant.update_entity
      data_template:
        entity_id: >
          {% if trigger.entity_id == 'input_select.tam_status' %}
            sensor.tam_travel_home
          {% elif trigger.entity_id == 'input_select.sbm_status' %}
            sensor.sbm_travel_home
          {% endif %}
    - delay: >
        {% if trigger.entity_id == 'input_select.tam_status' and (state_attr('sensor.tam_travel_home','duration') | replace(" mins","") | int < 60 | int) %}
          minutes: 0
        {% elif trigger.entity_id == 'input_select.tam_status' and (state_attr('sensor.tam_travel_home','duration') | replace(" mins","") | int > 60 | int) %}
          minutes: "{{ (state_attr('sensor.tam_travel_home','duration') | replace(' mins','' | int - 60 | int }}"
        {% elif trigger.entity_id == 'input_select.sbm_status' and (state_attr('sensor.sbm_travel_home','duration') | replace(" mins","") | int < 60 | int) %}
          minutes: 0
        {% elif trigger.entity_id == 'input_select.sbm_status' and (state_attr('sensor.sbm_travel_home','duration') | replace(" mins","") | int > 60 | int) %}
          minutes: "{{ (state_attr('sensor.sbm_travel_home','duration') | replace(' mins','' | int - 60 | int }}"
        {% endif %}
    - service: input_boolean.turn_off
      entity_id: input_boolean.thermostat_away
- delay:
    minutes: >
      {% if trigger.entity_id == 'input_select.tam_status' and 
        (state_attr('sensor.tam_travel_home','duration')
        | replace(" mins","") | int > 60 | int) %}
          {{ (state_attr('sensor.tam_travel_home','duration') | replace(' mins','' | int - 60 | int }}
      {% elif trigger.entity_id == 'input_select.sbm_status' and
        (state_attr('sensor.sbm_travel_home','duration')
        | replace(" mins","") | int > 60 | int) %}
          {{ (state_attr('sensor.sbm_travel_home','duration') | replace(' mins','' | int - 60 | int }}
      {% else %} 0 {% endif %}

I had actually discovered the error (an errant “(” before state_attr and a missing “)” after “’’”) and I couldn’t post (for some reason the forum went read-only for a second).

With that said, sure, just go ahead @anon43302295 and fix it and finish it so much more elegantly and less redundantly than I did. You know, raw talent and all.

Thanks bud!

1 Like

As an experiment, can you try this and let me know if it works?

- id: returninghome
  alias: Returning Home Mode
  trigger:
    - platform: state
      entity_id: input_select.tam_status, input_select.sbm_status
      to: 'Travelling Home'
  condition:
    - condition: state
      entity_id: input_select.family_status
      state: "Away"
    - condition: template
      value_template: "{{ (states('input_number.thermostat_temp') | int - state_attr('climate.upstairs', 'temperature') |int ) | abs > 3 }}"
  action:
    - service: homeassistant.update_entity
      data_template:
        entity_id: "sensor.{{trigger.object_id[:3]}}_travel_home"
    - delay:
        minutes: >
          {% set t = state_attr(trigger.entity_id, 'duration').replace(" mins","") | int %}
          {% set s = 'sensor.' ~ trigger.object_id[:3] ~ '_travel_home' %}
          {% set result = state_attr(s, 'duration').replace(' mins','') | int - 60 %}
          {{ result if t > 60 else 0 }}
    - service: input_boolean.turn_off
      entity_id: input_boolean.thermostat_away

Good grief! This looks arcane and brilliant.

I’ll do my best to test it. As you can imagine, this automation won’t be a very common occurrence. I’ll have to drop the condition.