For-statement for automation with numeric state

Would be great to trigger an automation if the numeric state has been above a value for a given time, like it is possible for the state-platform

For state trigger the following is valid:

automation:
  trigger:
    platform: state
    entity_id: device_tracker.paulus, device_tracker.anne_therese
    # Optional
    from: 'not_home'
    to: 'home'
    # If given, will trigger when state has been the to state for X time.
    for:
      hours: 1
      minutes: 10
      seconds: 5

Is would be nice we could use for statement for a numeric state trigger like:


  - alias: "Too hot"
    trigger:
       platform: numeric_state
       entity_id:  sensor.bath
       value_template: '{{ state.attributes.Temperature }}'
       above: 50
       for:
        minutes: 1
    action:

1 Like

I think you can achieve it in the following way.

Create a Binary Sensor of platform template:

- platform: template
  sensors:
      fortest:
        friendly_name: ForTest
        value_template: '{{ states.<your sensor value> | float > 50 }}'

It will turn a binary sensor to ON if it is above 50 and off if below 50.

Then use this binary sensor in an automation:

automation:
  trigger:
    platform: state
    entity_id: binary_sensor.fortest
    to: 'on'
    for:
      hours: 1
      minutes: 10
      seconds: 5

Does this work for you?

5 Likes

I keep getting

homeassistant.components.sensor.template: UndefinedError: 'None' has no attribute 'attributes'

in the logs when I configure a template sensor like you suggested above.

Any Idea what could be wrong with this config:

sensor:
 - platform: template
   sensors:
      tv_on:
        friendly_name: TV On
        value_template: '{{ states.switch.tv.attributes.current_power_mwh | float > 20000 }}'

OK I found out myself. The above is working. Only it seems that the sensor gets initialised before the device it polls the attributes from is actually initialised (Vera binding)

So some messages in the log but the config is working as expected

For what it’s worth, you could avoid the errors in the log by rewriting you sensor as follows:

sensor:
 - platform: template
   sensors:
      tv_on:
        friendly_name: TV On
        value_template: >
          {% if states.switch.tv %}
            {{ states.switch.tv.attributes.current_power_mwh | float > 20000 }}
          {% else %}
            ??
          {% endif %}

Based on this example.

On-topic: I voted for this feature request too. I made an automation just yesterday to turn some lights off after the luminance level (lux) was above a certain level for 5 minutes, and found out the hard way that numeric_state doesn’t support for. I’m working around it for the time being by using a template sensor so I can use state with for instead.

1 Like

I don’t consider adding templates the end all be all for this, all over the place they are used as workarounds for things that would be direct improvements to HA logic.

In my case I have sensor readings that bounce up and down sometimes, having the for statement would be highly useful.

Also if you are dealing with numeric sensors and personally want the uninitialized value to return 0 instead of undefined you can use the default variable.

value_template: '{{ states.switch.washer.attributes.current_power_w | default(0) | float }}'
3 Likes

Yeah, would be nice to reduce the number of templates and template/template binary sensors for simple things. They do add a bit to load and restart times.

And a total pain in the ass if you change out a sensor or rename something… You have to do all sorts of find and replace.

1 Like

I hear you there. That’s why I try to fully document and ‘index’ my YAML by sectioning things off together and putting them in logical orders. I’d like to move some things out of templates but they’re so damn useful!

So I have been fighting with an automation based on power for my washer, when it turns on it spikes quite high for a moment before settling down then when running it intermittently spikes and drops all the way through the cycle.

Someone suggested using the stats plugin to smooth this out and it does work for when the unit is running however it does not work for the initial spike as essentially the states plugin has nothing to go on yet so it reads really high.

I think the for statement would greatly simplify things and provide for solutions that right now I think I can only provide by creating word sensor readings for different voltages, then doing a FOR on those states to THEN get the desired alert or output state. That would be two sets of template sensors just to show if the unit is running or in spin cycle.

I voted for this.
Sure, there are ways to do it. But why not let us use the “For” in all kind of triggers?

3 Likes

I voted as well. I’m starting out with hass.io and this could be good for beginners. Cheers!

I think the main problem with this is, that the last updated time changes with every value-change. If you ask if a sensor is below 50 for ten minutes, but it updates every 30 seconds with 10, 15 and some other values in that range, then it never actually has a fixed state persisting over the time of 10 minutes. It will be 10 for 30 seconds, then 12 for 30 seconds and so on. Yes, they obviously all are below 50, but from a programmers point of view that’s not the same.

And yet, it looks like this feature request has been implemented. Have a look at this PR on Github.

It was merged 2 days ago so it should be in Home Assistant 0.53, due to be released this weekend.

3 Likes

it doesn’t yet seem to work for conditions with numeric states @fanaticDavid
for triggers yes, for conditions no

Doesn’t seem to be working…?

Is that a bug of a lack of feature?

i think or the new way of thinking to make home assistant easyier for not expert users it would be important that the for statement is possible. because of this i would reopen this Feature Request

You can use for: with the numeric_state trigger, look here. Some fixed were included in Home Assistant 0.96

You can as a trigger not as a condition.
Shouldn’t require a binary_sensor.template imho.

1 Like

This is Ok in a config:

 trigger:
    platform: numeric_state
    entity_id: sensor.temperature
    below: 25
    for:
      seconds: 5

This however is not

conditions:
  - condition: numeric_state
    entity_id: sensor.temperature
    below: 25
    for:
      seconds: 5

I think the second variant should also work, and i don’t really see why it doesn’t, I assume it is an oversight?