I cant make a template that must be true for 5 minutes work

Hi all, this should be simple but I am clearly missing something as this automation says it has never run and yet I can see the template evaluated to true for hours on one occasion. It is intended to warn of a power cut as we are on a home battery, so when the grid in / out senor is zero for 5 minutes that would qualify. the code is

platform: template
value_template: "{{ states('sensor.grid_power') == 0 }}"
for:
  hours: 0
  minutes: 5
  seconds: 0

there are no conditions but the automation has missed several power cuts.
the whole automation is below
Any hints very gratefully accepted
Thanks in advance

alias: Warn if grid is off
description: ""
trigger:
  - platform: template
    value_template: "{{ states('sensor.grid_power') == 0 }}"
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition: []
action:
  - service: input_boolean.turn_on
    target:
      entity_id: input_boolean.grid_failure
    data: {}
  - service: notify.mobile_app_pixel_6
    data:
      title: Grid Warning
      message: No grid for 5 minutes - Needs checking
  - service: notify.mobile_app_pixel_3a_xl
    data:
      title: Grid Warning
      message: No grid for 5 minutes - Needs checking
mode: single

Since everything in HA is a string, I believe you need to convert this to a float. I would also make it > than 10 watts because some systems take and use minor amounts of power to balance flow.

   value_template: "{{ states('sensor.grid_power') | float(0) > 0.01 }}"

Thanks for that - I keep forgetting that!

Re the 10 watts, I am trying to determine exactly zero as the inverter often uses a few watts either way to balance things and I am trying to check for a power failure which obviously is zero.

You also do not need a template sensor for this.

trigger:
  - platform: state
    entity_id: sensor.grid_power
    to: 0
    for:
      hours: 0
      minutes: 5
      seconds: 0

Or for J Gent’s suggestion:

trigger:
  - platform: numeric_state
    entity_id: sensor.grid_power
    below: 10
    for:
      hours: 0
      minutes: 5
      seconds: 0
1 Like