Trigger on not being in a state for a certain duration

I want to trigger on not being in a certain state (i,e, being in any one of many other states) AND not be in that one state for a certain duraiton.

so, similar to this: Automations: is there a "not" for the state trigger?

but add a time value…

trigger:
  platform: state
  entity_id: my_entity
  state: ! foo
  for:
    minutes: 2

normally with a defined state, can add the above for:… but if using templates, then

trigger:
  platform: state
  entity: my_entity
condition:
  condition: template
  value_template: "{{ 'foo' not in states.trigger.to_state.state and trigger.for.time().min > 1 }}'

this doesn’t work.

possibly for numerous reasons. not sure if i’m using the trigger.for component correctly and also wondering if it can discern how long the my_entity has been in the new state (whatever the new state is so long as it’s not foo)?

any thoughts/suggestions?

Use the same template to create a template sensor. Then base your automation on the state of that sensor. Here’s an example of a template sensor that I use to detect when it’s “gloomy” for light handling:

  - platform: template
    sensors:
      gloomy:
        friendly_name: Gloomy
        value_template: >
            {% if states.sensor.dark_sky_cloud_coverage -%}
            {% if (states('sensor.dark_sky_cloud_coverage') | float) > 80 %}on{% else %}off{% endif %}
            {%- endif %}

makes sense, thanks!

This looks interesting for testing for flat batteries in devices (especially tx only devices like RF433 sensors)

Could test for no activity for > a day - then notify that a likely flat battery.

Very cool … and good to see some foo creeping in to the vernacular!

Will give your first variant a try

If you have several sensors (several batteries), you can use the states of multiple entity_id’s to trigger an automation. Automations are able to get data from their trigger. So you could create a single automation to act on any “battery sensor” and then have it report which battery was the problem within the automation.