personally, i’d reorganize the template so that you can easily check all 3 states. Currently the way you have it coded, you never check the states to verify they are valid.
{%- set ns = namespace(values = []) %}
{%- for i in range(1,4) %}
{%- set sensor = states.sensor['phase_{}_power'.format(i)] %}
{%- if sensor != None
and sensor.state not in ['unknown','unavailable']
and now().timestamp() - as_timestamp(sensor.last_changed) < 2 %}
{%- set ns.values = ns.values + [ sensor.state | float ] %}
{%- endif %}
{%- endfor %}
{%- if ns.values | length == 3 %}
{{ ns.values | sum | round(2) }}
{%- else %}
{{ states('sensor.all_3_phases_power') }}
{%- endif %}
This template iterates through all 3 sensors. It verifies they aren’t unknown or unavailable. It also makes sure that all 3 have been updated in the last 2 seconds.
Then, it verifies that we have 3 results. If we do, update. Otherwise don’t update.
Quick update: The template works great now, but I still have one thing to ask…
In the template, we use “and now().timestamp() - as_timestamp(sensor.last_changed) < 2”, but if the Wattage between updates remains the same on one phase (or more), it won’t update and is probably by “design” from last_update. This happens every so often and the template remains the same 20 or more seconds, trying to “find” 3 updates.
How should that be handled? I’ve removed that part of the code, so it updates every 5 seconds when the phases update, the problem is that it now updates 3 times in rapid succession (again). Is there any way, in your opinion, to solve this problem?
If they all update at the same time, just update them based on 1 of the 3 sensors instead of all 3. Change your entity_id list to only contain the first phase.