Get notify if temperature was under 0°C more than 2 hours at night

Hi,
I’m new to HA and newer on template which are extremly power full!
I’ve figured out how to get notify with this code :

alias: Avertissement Dégivrage
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.temp
    below: 0
    for:
      hours: 2
      minutes: 0
      seconds: 0
condition:
  - condition: template
    value_template: "value_template: '{{ now().hour == 23 or now().hour < 7 }}'"
    enabled: true
action:
  - wait_for_trigger:
      - platform: time
        at: "07:00:00"
    continue_on_timeout: false
  - service: notify.mobile_app_pixel_6a
    data:
      message: "Température extérieure basse, dégivre la voiture ! "
mode: single
max_exceeded: silent

The only problem is if the temperature goes below 0 before 23h, I won’t get notified.
Is there a way to add a “for” argument to a template?

  • condition: template
    value_template: “value_template: ‘{{ states(‘sensor.temp’) < 0 | duration > 2 hours }}’”
    I’d love to go to this code adapted to add a notion of delay :
alias: Dégivrage v2
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.temp
condition:
  - condition: template
    value_template: "value_template: '{{ now().hour == 23 or now().hour < 7 }}'"
    enabled: true
  - condition: template
    value_template: "value_template: '{{ states('sensor.temp') < 0 }}'"
action:
  - wait_for_trigger:
      - platform: time
        at: "07:00:00"
  - service: notify.mobile_app_pixel_6a
    data:
      message: "Température extérieure basse, dégivre la voiture ! "
mode: single
max_exceeded: silent

You could also look at history stats and use that value for your automation
History Stats - Home Assistant (home-assistant.io)

I would approach this a different way instead of relying on long for: durations and waits.

Create a template binary sensor that turns ‘on’ when the temperature is less than 0.

template:
  - binary_sensor:
      - name: Temperature under 0
        state: "{{ states('sensor.temp') | float(0) < 0 }}"

Create a History Stats sensor based on that binary sensor, setting the start and end variables to cover your 23:00 - 07:00 timespan.

sensor:
  - platform: history_stats
    name: Overnight Temp Below 0
    entity_id: binary_sensor.temperature_under_0
    state: "on"
    type: time
    start: "{{ today_at() - timedelta(hours=1) }}"
    end: "{{ today_at('07:00:00') }}"

Then your automation becomes:

alias: Dégivrage v2
description: ""
trigger:
  - platform: time
    at: "07:00:00"
condition:
  - condition: numeric_state
    entity_id: sensor.overnight_temp_below_0
    above: 2
action:
  - service: notify.mobile_app_pixel_6a
    data:
      message: "Température extérieure basse, dégivre la voiture ! "
mode: single
2 Likes

Thank for this elegant solution!
@123 : argument for in condition did not work (already tried before) : it only works in trigger.

Sorry, that was my mistake; most conditions don’t accept the for option. I have deleted my example to prevent misleading others.

1 Like