Need help with templating in automation - Template timer

Hi all,

this is my first post, so please be gentle with me, especially if I did not post everything right :).

My usecase is as follows:
I want my home assistant to measure the outdoor temperature deviation from a defined “comfort zone”, wind and precipitation and determine a time value when it should warn me to close open windows.

The part with the template sensor is done for that purpose.
For example the sensor (screenshot below) will 60 minutes warning when the temperature is in the comfort zone, but 5 minutes, if we have freezing outdoor temperaturs.
Even if the temperature is in the comfort zone, if rain sets in, time will decrease, exponentially so if wind is above 10 kph.
The resulting value is a float minute value.

Now, all that was left is to hook the template sensor into the automation. That is where I fail. I tried to define automation variables first, then refer to them in the state triggers and delay commands, but somehow I fail again and again.

I tried to implement this by using deltatime string (like … for: "{{triggertime}}", or like in the example below, via numberic values.

Did I miss something obvious? Or is it simply not possible to hook templates to trigger state periods?

Script excerpt below:…

alias: Window Warning
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.aussen_bad
    to: "on"
    for:
      minutes: |
        {{ triggertime  }}
    id: BadOpenTooLong
 [...]
action:
  - choose:
      - conditions:
          - condition: trigger
            id: BadOpenTooLong
        sequence:
          - repeat:
              until:
                - condition: state
                  entity_id: binary_sensor.aussen_bad
                  state: "off"
              sequence:
                - if:
                    - condition: state
                      entity_id: person.xxx
                      state: home
                  then:
                    - service: notify.mobile_app_xxx
                      data:
                        title: ACHTUNG!
                        message: >-
                          {{ trigger.from_state.attributes.friendly_name }}
                          schließen!
                      enabled: true
                - service: notify.mobile_app_xxx
                  data:
                    message: >-
                      {{ trigger.from_state.attributes.friendly_name }}
                      schließen!
                    title: ACHTUNG!
                - parallel:
                    - service: tts.google_translate_say
                      data:
                        cache: true
                        entity_id: media_player.xxx
                        message: >-
                          {{trigger.from_state.attributes.friendly_name }}
                          schließen!
                        language: de
                      enabled: true
                    - service: tts.google_translate_say
                      data:
                        cache: true
                        entity_id: media_player.xxx
                        message: >-
                          {{trigger.from_state.attributes.friendly_name }}
                          schließen!
                        language: de
                      enabled: true
                - delay:
                    minutes: >
                      '{{ repeattime  }}'
 
mode: parallel
max: 10
variables:
  triggertime: >
    '{{ states("sensor.zeitgeber_warnung_fenster_offen_2") | float }}'
  repeattime: >
    '{{ states("sensor.zeitgeber_warnung_fenster_offen_2") / 2  | float }}'

Thanks
Martin

It’s difficult to read unformatted YAML and almost impossible to validate it for indentation errors. Please format everything as explained in FAQ guideline 11.

Hi, @123

thank you for that pointer, I forgot these backticks.

Please find the updated script above.

Thank you very much,

Martin

If you are using multi-line templates, you should not quote them. Either:

triggertime: >
    {{ states("sensor.zeitgeber_warnung_fenster_offen_2") | float(0) }}

or

triggertime: '{{ states("sensor.zeitgeber_warnung_fenster_offen_2") | float(0) }}'

(with defaults added).

Same error in your delay statement.

HI @Troon,

thanks for your response. I adjusted my code acccordingly.
I actually tried to implement the single-line variant with ’ indentation, but HA converts them to " with some strange results, see below.

Anyway, I created another automation to test if this is the only issue. I created annother set of variables, with set values (so triggertime2 and repeattime2) and tried to trigger the automation with reference to them.

Still no luck — automation did not fire —, so annother issue must be in the for: statement

Thanks for everything so far.
Martin

alias: test
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.aussen_bad
    to: "on"
    for:
      minutes: "{{ triggertime2 }}"
    id: BadOpenTooLong
action:
  - choose:
      - conditions:
          - condition: trigger
            id: BadOpenTooLong
        sequence:
          - repeat:
              until:
                - condition: state
                  entity_id: binary_sensor.aussen_bad
                  state: "off"
              sequence:
                - parallel:
                    - service: tts.google_translate_say
                      data:
                        cache: true
                        entity_id: media_player.woburnblack6bb7
                        message: >-
                          {{trigger.from_state.attributes.friendly_name }}
                          schließen!
                        language: de
                      enabled: true
                - delay:
                    minutes: "{{ repeattime2  }}"
mode: parallel
max: 10
variables:
  triggertime: "{{ states(\"sensor.zeitgeber_warnung_fenster_offen_2\") | float(60) }}"
  repeattime: "{{ states(\"sensor.zeitgeber_warnung_fenster_offen_2\") / 2  | float(60) }}"
  triggertime2: 1
  repeattime2: 1

I don’t think you’re using variables correctly. Look at the scope here:

You don’t need to set variables at all, of course. You can use:

    for:
      minutes:"{{ states('sensor.zeitgeber_warnung_fenster_offen_2')|float(60) }}"

Hi @Troon Troon,

that helped, thank you very much! Guess I added just too much complexity to the mix.

1 Like