Condition: duration ("for") in value_template

Hi everybody,

as far as I can tell, this is not yet possible; wouldn’t it be helpful to integrate it in the future? Example:

currently

condition: template
value_template: >-
  {{ states("input_number.festlegen_wunschtemperatur_badunten") | float <=
  states("sensor.badunten_messwerte_temperature") | float }}

then

condition: template
value_template: >-
  {{ states("input_number.festlegen_wunschtemperatur_badunten") | float <=
  states("sensor.badunten_messwerte_temperature") | float }}
value_for: 00:00:30

Currently, we can only have a condition template depending on whether that value_template is true; it would be nice to have this value_template value being true for a certain amount of time before continuing the automation.

Please disregard if this is already possible, but I couldn’t figure out how to do it.

Isn’t that the trigger?
Conditions are evaluated directly and doesn’t “hang around” but if this would be your trigger then you could say for: 30 seconds.

But perhaps that doesn’t work with the rest of the automation?

It isn’t as straightforward as it seems. For entities, it is possible to look at the last_changed attribute to determine if the state is that way for longer than the wanted duration. That is why a duration is already implemented in conditions that check an entity state.

For the value_template in a template condition there is no place where HA could look to determine it. So in order for HA to implement a duration it would need to keep the state of the value template somewhere to determine how long it has been that way.

As far as I can tell you have a few options to implement it yourself:

  1. Look at the last_changed attibutes of the entities in your value template and write code that would combine those to the desired result.
  2. Create a template sensor that reacts immediately, and use a state condition for that entity with a duration.
1 Like

Thank you. Unfortunately, this won’t work in this situation:


This sensor (among many others that I use) just won’t provide a last_changed attribute.

I thought of a workaround, but this feels very clumsy: create an input_datetime entity (date only) that updates its status each time the sensor updates. If it were just for one single sensor, I’d do it, but I hoped there was a better solution that would utilize the built-in for: that can already be used in trigger and condition. (by the way, it works there! Even though there is no last_changed attribute, I can use for: just fine as a trigger or condition; just not in any template)

From what I understand, last_changed is not an attribute, it is a property of the state object so you need to use the state object method to access it:

{{ states.sensor.badunten_messwerte_temperature.last_changed }}
1 Like

Yes! That’s it. I didn’t know there was a difference between states....last_changed and state_attr("...", "last_changed"), but apparently, there is. I can work with that, thank you.

The difference is that with state_attr() you can get the values of attributes of an entity. But last_changed is not an attribute.
If you look at developer tools > states, you won’t see it listed as an attribute, but it’s mentioned in a separate section.

1 Like