Templating a countdown timer

I am getting desperate! I cannot get to grips with creating a countdown template and to retain my sanity I really need some help. Scenario: I have a sensor in my fridge that reports it’s temperature to my HA. If the temp gets above 12°c an automation sends a notification to my iPhone. No problems with that! However I don’t want to receive messages between 23:00 and 08:00, however I would like any incidents where the temp is more than above 12°c to be reported at 08:00 in the morning. So I created three helpers, one to record the temperature at the time it is reported and another helper to record the time of the incident and the third one to countdown the delay until 08:00 in the morning and allow the notification to be sent. HELP PLEASE!

alias: Test for Fridge notification
description: ""
triggers:
  - type: temperature
    device_id: f4c19433741d0d6e93610336a0bc0752
    entity_id: ab6f3e3c084577c84dc5fd79cfd0e4ec
    domain: sensor
    trigger: device
    above: 12
    for:
      hours: 0
      minutes: 15
      seconds: 0
conditions: []
actions:
  - choose:
      - conditions:
          - condition: time
            after: "08:00:00"
            before: "23:00:00"
        sequence:
          - action: notify.mobile_app_peter_iphone_11
            metadata: {}
            data:
              message: >-
                Temperaturen i fridge is: {{
                states('sensor.esphome_web_e06909_kylen') }} °c
              title: WARNING FRIDGE KITCHEN
              data:
                push:
                  sound:
                    name: News_Flash.caf
                    critical: 1
                    volume: 0.75
      - conditions:
          - condition: time
            after: "23:00:00"
            before: "08:00:00"
        sequence:
          - action: input_number.set_value
            metadata: {}
            data:
              value:
                {{states('sensor.esphome_web_e06909_kylen') }}
            target:
              entity_id: input_number.kylskapet_koket_temp
          - action: input_datetime.set_datetime
            metadata: {}
            data:
              datetime: as_timestamp(now())
            target:
              entity_id: input_datetime.koket_kylskap_event_happened
## This next section should be controlled by a countdown timer or equivilent...##
## and should definethe time between the event happening and 08:00 hrs ##
          - delay:
              hours: 6
              minutes: 0
              seconds: 0
              milliseconds: 0
## End of that section  ##
          - action: notify.mobile_app_peter_iphone_11
            metadata: {}
            data:
              message: >-
                Thr temperatur of the fridge at {{
                states('input_datetime.koket_kylskap_event_happened') }} was {{
                states('input_number.kylskapet_koket_temp') }} °C
              title: WARNING FRIDGE KITCHEN
              data:
                push:
                  sound:
                    name: News_Flash.caf
                    critical: 1
                    volume: 0.75
mode: single

Hope someone will help me out.

Hi Peter_G,

So this can be a problem for many reasons. If HA is restarted, etc, the notification will not happen. Long running automations and long delays are things I don’t suggest or use myself. HA is better suited to event driven actions.
Consider setting a helper toggle (input_boolean) there that says there should be a notification, and at the appropriate time, another automation can trigger when that helper is on, send your message, and turn the helper off.
I state a seperate automation, but you could add a trigger at the appropriate time in that automation and condition that if the helper is on to send that message from this automation as well.

OK I get your thinking. Just wondering how to calculate and include “the appropriate time” so that the second automation knows when to start. I found this piece of code that does the calculation of hours and minutes remaining but don’t understand how I can use it in your scenario.

{% set t = now().replace(hour=8, minute=0, second=0, microsecond=0) %}
{{ ((t + timedelta(days=1) if now() > t else t) - now()).total_seconds()
   | timestamp_custom('%-Hh %-Mm', false) }}

One option is to add an action to create a Calendar event that contains the necessary data and starts at whatever time you want the notification to be sent. The have an automation that triggers off the calendar.

Add a trigger, trigger at 8am. (every day)

Have the notify in a choose with a condition that the boolean is on.

Not as hard as you are doing with that template.

1 Like

Thanks for your input, much appreciated. I have created a separate automation as you suggested, set to run every day at 8am when an input_boolean helper is “on”. I also turn this helper ‘off’ at the end of the automation. I had not considered the implication of my original automation not restarting if HA went down, but now that scenarion is catered for.
I am still annoyed with myself that I cannot get to grips with formatting time in templates. I can find snippets of information on the internet and of course the university of YouTube but do not have any formal education in programming and it’s very frustrating not to be able to format information as required. For example the message output below include the actual time of an incident. I want to display the event time as H:M but the timestamp configuration below gives an error. Have you any suggestions what I am doing wrong here?

alias: Send Notification at 8am
description: ""
triggers:
  - trigger: time
    at: "08:00:00"
conditions:
  - condition: state
    entity_id: input_boolean.send_notification_yes_or_no
    state: "on"
actions:
  - action: notify.mobile_app_peter_iphone_11
    metadata: {}
    data:
## I would like the result of the formating on the input_datetime... to read 09:42 but with my formatting ##
## it gives an error. without any formatting it reads 2024-11-14 09:42:13 ##
      message: >-
        Temperaturen i Kylskåpet i natt kl. {{
        states('input_datetime.koket_kylskap_event_happened' | 'timestamp_custom("%H:%M")) }} var
        {{states('input_number.kylskapet_koket_temp') }} °C
      title: VARNING KYLSKÅPET KÖKET
      data:
        push:
          sound:
            name: News_Flash.caf
            critical: 1
            volume: 0.75
  - action: input_boolean.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.send_notification_yes_or_no
mode: single

1 Like