Energy Dashboard not showing correct value

The sensors are showing the correct value but the dashboard is showing a totally different (nagitiv) value.
I have waited 2 days to see but no improvement.


I do a calculation of the “rest energy” = total energy - poolpump - poolheater

utility_meter:
  poolheater_today:
    source: sensor.consumption_52
    cycle: daily  

  poolpump_today:
    source: sensor.consumption_48
    cycle: daily  
  - platform: template
    sensors:
     rest_energy:
        value_template: "{{ (states.sensor.accumulated_consumption.state | float - states.sensor.poolpump_today.state | float - states.sensor.poolheater_today.state | float ) | round(2) }}"
        unit_of_measurement: "kWh"

Is this not possible to do?

1 Like

Yes but you need an availability_template for your template sensor to prevent the |float filter converting the state unavailable to 0. Search the forum. There are many examples.

You also need to heed the warning about always using states() in the templating documentation.

Yes, thanks, I have now changed the states() to the recommended syntax.
BUT the strange thing to my knowledge is that the ‘rest_energy’ sensor is showing correct when checking the developer tool as shown in the picture. It is just the dashboard that show wrong value…

Because when one of your sensors is unavailable the value of the template decreases. That is taken by the state_class total_increasing to mean your sensor has reset.

Add an availability template and this will solve your issue.

  - platform: template
    sensors:
     rest_energy:
        value_template: "{{ (states('sensor.accumulated_consumption') | float - states('sensor.poolpump_today') | float - states('sensor.poolheater_today') | float ) | round(2) }}"
        availability_template: "{{ states('sensor.accumulated_consumption') not in ['unknown', 'unavailable'] and states('sensor.poolpump_today') not in ['unknown', 'unavailable'] and states('sensor.poolheater_today') not in ['unknown', 'unavailable'] }}"
        unit_of_measurement: "kWh"
1 Like

Thank’s… I will try that :smiley:

Hey @tom_l ,
thanks for that piece of code, I think I am on the good way to make it a little more dynamic

---
platform: template
sensors:
  kitchen_total_energy_usage:
    friendly_name: Kitchena Daily Energy Usage
    entity_id: sensor.time
    unit_of_measurement: kWh
    icon_template: mdi:counter
    availability_template: >
      {% set ns = namespace(states=[]) %}
      {% for s in states.sensor %}
        {% if s.object_id.startswith('kitchen_') and s.object_id.endswith('_energy_total') %}
          {% if ( s.state ) not in ['unknown', 'unavailable'] %}
            {% if ( ns.state ) not in ['unknown', 'unavailable'] %}
              {% set ns.states = "True" %}
            {% endif %}
          {% else %}
            {% set ns.states = "False" %}
          {% endif %}
        {% endif %}
      {% endfor %}
      {{ ns.states }}
    value_template: >
      {% set ns = namespace(states=[]) %}
      {% for s in states.sensor %}
        {% if s.object_id.startswith('kitchen_') and s.object_id.endswith('_energy_total') %}
          {% if ( s.state | float ) > 0.000 %}
            {% set ns.states = ns.states + [ states(s.entity_id) | float ] %}
          {% endif %}
        {% endif %}
      {% endfor %}
      {{ ns.states | sum | round(2) }}

This way it will include all the plugs/devices based on the search pattern, so that I can group per-room (or do the whole house) without having to specify each single plug/device.

1 Like

Hi @tom_l, unfortunately it doesn’t seem to be working as I would have expected…

And the problem with that is that the 2 devices didn’t drop 1 bit…
image

The only thing I did, and I think it was about that time, was to update HA, so I guess a restart…

That caused the sensor to cleanup and maybe be a “0” on startup, at least this is my current opinion…

Any idea?

Try the simple availability template.

Ok, will try that, but theoretically it is working… the test were all successfull, only on restart it seems not to be working

Didn’t help, on each restart it goes to 0 and up.

I have this set on a per-room basis, and I noticed that this issue only appear in the rooms with 1 power meterer device…

I think that might be interesting… I will have to try to assign a second device to those rooms and retry again…

If confirmed, that might be a bug… or a logic missing in that loop

Ok, it’s not even that…

It is the only plug that doesn’t have a very nice reception on the wifi… and he keep dropping and reconnecting:

The device itself, report as expected “empty” values

The problem is that I don’t really know what else to do as it seems that the instability of the device causes the sensor to miss-report even if the availability template is there…

If you are on 2021.10 you now have to set 0 as a default value when that happens, using

    value_template: >
      {% set ns = namespace(states=[]) %}
      {% for s in states.sensor %}
        {% if s.object_id.startswith('kitchen_') and s.object_id.endswith('_energy_total') %}
          {% if ( s.state | float(0) ) > 0.000 %}
            {% set ns.states = ns.states + [ states(s.entity_id) | float ] %}
          {% endif %}
        {% endif %}
      {% endfor %}
      {{ ns.states | sum | round(2) }}
1 Like

Sorry, not sure I understand what you mean.
The value of the template needs to result 0 if the device is not available?
Also, the code you paseted seems to be the same as mine, is that correct or I don’t see something in there?

Thanks for taking the time to follow my madness, as usual, much appreciated :slight_smile:

Are you on version 2021.10?

It is not the same this is required now:

          {% if ( s.state | float(0) ) > 0.000 %}
                                 ^^^
                                 |||

Ah, damn I missed it :smiley:
Yes I am on 2021.10 :slight_smile:

Will try that,but do I still need to keep the availability_template or that’s no longer needed?

Probably not needed.

Ok, will test that, thanks a lot!

Ok… it seems that did it… amazing!

Will update the thread if anything goes south… but so far so good… finally!

1 Like

Maybe the availability_template it’s actually still needed, or how could it deal with multiple device in that loop?

For what I understand, that will work for the 1 device that is set to “0”, but if all other are correct, I might potentially have a drop and then an increase again when the offline device comes back, is that wrong?

Ex.
Studio got 2 devices:
Desk 1 250kWh
Desk 2 250kWh

The room total would be 500, but the moment 1 of those 2 devices goes offline, the room would drop to 250, to go back to 500 once the device is back.

Makes sense?

For multiple devices, like your use case yes it does make sense.