Trigger-based template sensors using numeric_state to check last updated doesn't work

Hey,

I want to create a templated sensor that updates if there is a new value of “energie_productie_zon_watt” or if the templated sensor is not updated for 10 minutes.

some context:
My energy meter return each second the consumed and the returned power to/from the grid
-sensor.energie_consumption_watt
-sensor.energie_production_watt
My solar panels returns the produced power every x minutes:
-sensor.energie_productie_zon_watt

So I calculate the “real consumption” whenever the slowest sensor (=energie_productie_zon_watt) is being updated. But at night that sensor is never updated that’s why I add the 2nd trigger as fallback.

template:
  - trigger:
     - platform: state
       entity_id: sensor.energie_productie_zon_watt
     - platform: numeric_state
       entity_id: sensor.energie_real_consumption_watt
       value_template: "{{ (now() - state.last_updated).seconds }}"
       above: 600
    sensor:
     - name: energie_real_consumption_watt
       unit_of_measurement: "W"
       state: "{{ (states('sensor.energie_consumption_watt') |float + states('sensor.energie_productie_zon_watt') |float - states('sensor.energie_production_watt') |float )  |round() }}"

I also tried
value_template: {{(now() - states.sensor.energie_productie_zon_watt.last_updated).seconds}}
as trigger, but that also doesn’t trigger the calculation.

That would work if you use the template sensor, i.e. sensor.energie_real_consumption_watt
If you trigger on sensor.energie_productie_zon_watt, it will trigger only once, as the last_updated will never be updated before sunrise.

It doesn’t work, I tried the code below:

template:
  - trigger:
     - platform: state
       entity_id: sensor.energie_productie_zon_watt
     - platform: numeric_state
       entity_id: sensor.energie_real_consumption_watt
       value_template: "{{(now() - states.sensor.energie_real_consumption_watt.last_updated).seconds}}"
       above: 10
    sensor:
     - name: energie_real_consumption_watt
       unit_of_measurement: "W"
       state: "{{ (states('sensor.energie_consumption_watt') |float + states('sensor.energie_productie_zon_watt') |float - states('sensor.energie_production_watt') |float )  |round() }}"

Careful that the trigger will only be evaluated once per minute

That would explain a lot.
I always used the 10 seconds to proceed my testings. I will change it to a longer ‘timeout’ and retest.

No, shouldn’t matter. Testing something similar on my side and it doesn’t trigger, indeed.

Strange…
As I was not 100% sure with numeric_state, I test with a template trigger, but sill no dice

EDIT: Nevermind, it is working. Not sure what I did wrong the first time

Yeah, I also thought that I should use a “condition”.
But that way I need to create an automation, I liked the template sensor approach…

Yeah, understand that.

Ping @123 I looked other threads and I found one where you say that the now() trick doesn’t work if another entity is referenced, right?

I don’t recall saying that. Do you have a link to it handy?

Given a Template Sensor, if it’s template employs now() and other entities, the template will be evaluated at least every minute (because of the use of now()) plus at whatever interval the other entities in the template change state.

If the template employs all domains or an entire domain (like states or states.light) it will be throttled to a per minute evaluation interval.

@koying
the following does actually work, but not always.
I think it has something to do with the fact you said it is only evaluated each minute. So it means that one evaluation the “last updated” should be under 20 seconds, and the next evaluation it should be above 20seconds.

Anyhow, I will change it to 120 this evening and do tests if the solar panels are off…

template:
  - trigger:
     - platform: state
       entity_id: sensor.energie_productie_zon_watt
     - platform: template
       value_template: "{{(now() - states.sensor.energie_real_consumption_watt.last_updated).seconds >20 }}"
    sensor:
     - name: energie_real_consumption_watt
       unit_of_measurement: "W"
       state: "{{ (states('sensor.energie_consumption_watt') |float + states('sensor.energie_productie_zon_watt') |float - states('sensor.energie_production_watt') |float )  |round() }}"

Nevermind, I misread the post (Trying to set a notifcation when there is a minute left on timer - #7 by 123)

I believe the observation I made in that post also applies to this one. The template’s evaluation interval of one minute was too long for the other application (“time resolution is too coarse”). It missed events occurring during the 1-minute interval. That appears to be an issue here as well (the template is checking for a 20-second difference but the calculation is computed every minute).

This was just for testing. The original requirement is actually 10min, so should work.

I wasn’t aware of that because I never participated in this thread (because I have no interest in it) and only dropped in because you pinged me (and I don’t always respond to pings). Anyway, glad to hear it’s been sorted out. :+1:

1 Like