Template sensor doesnt update with parent sensor

Hi there,

I have a template sensor that calculates the difference between the highest and lowest electricity prices the coming evening and next day to determine if the house battery should charge or not. The sensor works but the problem Im having is that the sensor doesnt seem to update its values along with the parent sensor (nordpool spotprice).

Any ides on how to make sure it updates at the same time?

Here is the teomplate sensor:

{% set CompleteListNight=(state_attr('sensor.nordpool_kwh_se3_sek_2_00_0', 'today')[21:24]) | list + state_attr('sensor.nordpool_kwh_se3_sek_2_00_0', 'tomorrow')[0:6] | list %}

{% set CompleteList=(state_attr('sensor.nordpool_kwh_se3_sek_2_00_0', 'today')[21:24]) | list + state_attr('sensor.nordpool_kwh_se3_sek_2_00_0', 'tomorrow')[0:24] | list %}

{% set ValuesLow=((CompleteListNight) | sort(reverse=false))[0] %}
{% set ValuesHigh=((CompleteList) | sort(reverse=true))[0] %}

{{ ValuesHigh - ValuesLow}}

Try adding this to your template:

{% set dummy_state = states('sensor.nordpool_kwh_se3_sek_2_00_0') %}

That may force the template to be evaluated when the nordpool sensor state changes as well as when the attribute values change.

If you only want it to be evaluated when the state changes, then use a triggered template sensor.

Your statements do not make sense. This template will execute whenever the state object updates for sensor.nordpool_kwh_se3_sek_2_00_0 and sensor.nordpool_kwh_se3_sek_2_00_0. There are test cases built into home assistant to verify that this functionality works on every build of home assistant. I.e. this functionality will likely always work and never break.

It’s likely that the data in your sensors does not change or there is a flaw in your templating logic.

Or did Kevin ‘optimise’ the template engine to only monitor for changes in attributes if only attributes are used?

Even if that were true, that would show that the attributes are not updating thus the calculation wouldn’t update.

Just looking through PRs, nothing has change in template optimizations in 2023.2. There are some coming though in 2023.3 but they won’t impact this.

Not to mention, tests would still find issues. Tests like this, where only attributes are changing would fail.

It’s also not really clear what they mean. They say this:

So all good right?

But then:

So they seem to be saying that the sensor updates when the attributes change but not when the state changes (as that is not present in the template).

Right but even if it did or didn’t execute when the main state changed, if the attribute didn’t update, the calc won’t update. He’s only referencing attributes, if they are static, then the calc is static. It would result in the state remaining the same.

Personally, based on what he’s doing, I would assume those attributes only update at midnight. He’s getting a list of today and tomorrows values. They aren’t going to change until the day shifts. But that’s an assumption on my part. I would assume that calculation stays the same for 24 hours.

Yeah that’s what I don’t understand.

Sounds like a good application for a triggered template sensor if that is the case.

You’re right, the lists with attributes only updates once a day (at 13:00 CET). So im really only interested in the sensor updating after that point (one update is enough). However, the state of the parent sensor updates once every hour and I thought that the template sensor would refresh at the same rate.

I guess a simple template reload automation would suffice but Im still interested in why the template isnt refreshing once every hour.

Because the state and attributes have not changed.

The state of the nordpoolsensor changes every hour to the current price.

But the attributes don’t change so the template result is the same.

Ok, so a state changed to an attribute value isnt recognized as a “new” state?

No, your calculation is only using the attributes. It will not change state because the result is always the same because the attributes are always the same.

If I have a list of 4 items that is [0, 1, 2, 3], and I ask for the max value in the list every hour… then the result will always be 3 regardless how many times you ask for the result. That’s effectively what’s happening here.

I understand. Thank you for the explanation.