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}}
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.
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.
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.
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.