Template sums wrong value

I have an simple template which sums 3 phases together {{ phase_1_power + phase_2_power + phase_3_power }} and it works fine - sometimes.

image

Sometimes the total value is only adding one phase, evaluating others as zero (as it seems), but why? It hasn’t become unavailable.

Thanks for any help.

EDIT: The exact template is here

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.

2 Likes

Quite an impressive solution! Thanks for taking your time.

However, there’s something wrong with it

It shows the sum of the three sensors, but most of the time it shows:

Error rendering template: UndefinedError: 'state' is undefined

Oops, made an edit. Try it again in the template editor. I don’t have these devices so I wasn’t able to debug the code.

No problem, I just updated it and now the error’s gone. I have now moved the template into the template_sensor to fully test it.

Now it doesn’t changes its value anymore and stays always the same …

image

image

:confused:

Ah, should have seen that coming, give me a second to edit the post.

EDIT: Actually, just add entity_id: to your whole template sensor./

sensor:
  - platform: template
    sensors:
      all_3_phases_power:
        friendly_name: "All Phases Power"
        entity_id:
        - sensor.phase_1_power
        - sensor.phase_2_power
        - sensor.phase_3_power
        value_template: >
          {%- 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 %}
        unit_of_measurement: "W"
        icon_template: mdi:power
1 Like

Aaahhh, now it seems to work :smiley:

You’re awesome! Thanks again for your time to do this.

I’m still a bit overwhelmed with the complexity of this template, but I’m sure I’ll grasp it over time.

I will let this stay overnight and report tomorrow and let you know if the initial bug’s gone.

1 Like

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.

They don’t update exactly at the same time, they update one after another very fast, but not always in the same order.