Execute Automation if value is over X for Y seconds

Hey guys,

I am looking for a way to turn on and off a bunch of shellys when a sensor reports above or below X for 30 seconds.

I found that the numeric state trigger only fires if the value actually crosse the specified amount but this will not fire if the sensor value already is above x.

I’v then tried

alias: E-Heizkörper ausschalten
description: ""
  - platform: state
      - sensor.myenergi_xx3_power_import
      hours: 0
      minutes: 0
      seconds: 0
  - condition: numeric_state
    entity_id: sensor.xx3_power_import
    above: 1000

But as soon as i set the for options to 30 seconds the automation does not work at all.

Could you help me out? :slight_smile: Thanks

As you have discovered a standard State trigger will not work in this case because the for: would require that the value remains exactly the same for at least 30 seconds. Numeric state is the correct trigger type for the standard case of your application. If there are non-standard cases you are concerned about you should describe them and we may be able to suggest other triggers to use in addition to your Numeric state trigger.


Coming back to my initial topic since I am stil struggeling with those types of automations.

My use case is this:
I’d like to execute a certain automations if reported energy usage exeeds or is below a certain value for at least 30 seconds.

So if energy usage goes above 500 for 3 seconds, then gets back under 500 the automation would not trigger, but if energy usage goes above 500 for 30 seconds or more it should trigger.

I am fine with the numeric state trigger only executing if a threshold is being crossed but I cannot get the duration to work.

Any help is highly appreciated :slight_smile:

The basic trigger for that would be:

  - platform: numeric_state
    entity_id: sensor.your_energy_sensor
    above: 500
    for: "00:00:30"
condition: []

Thanks for the response! Unfortunately this does not seem to work. The automation triggers immediately, ignoring the duration.

Are there any situations where the duration in the for-statement might be ignored?

This is my automation:

alias: Batteriespeicher Ü-Laden (STOPP)
description: weil kein Überschuss mehr
  - platform: numeric_state
    entity_id: sensor.myenergi_power_import
    above: 100
      hours: 0
      minutes: 0
      seconds: 30

condition: []
  - type: turn_off
    device_id: ccce626d4c6125e8c3e69fb71a89302c
    entity_id: switch.tasmota_powerstation_laden
    domain: switch