Graphs Are Not Compatible With Template Sensors: Change My Mind

I’m going into year 3 tinkering with HA and this is my first post. Until now, between search engines and YouTube, I’ve been able to find solutions or answers to all my HA frustrations.

My question has been asked and answered here before and most topics marked solved yet the answer eludes me.

I have a screen capture of an energy graph card, (type: energy-devices-graph) and an entity card, on the same dashboard.


The energy graph card relies on the same sensors as the entity card. The Cabin L1+L2 sensor, (sensor.cabin1_cabin2) is a template sensor:

- sensor:
      - name: "cabin1+cabin2"
        unique_id: 0a7476cc-d6c1-40ba-8ae1-606518c3ee7f
        state: "{{ states('sensor.cabin1_9_1d') | float + states('sensor.cabin2_10_1d') | float}}"
        state_class: "total"
        device_class: 'energy'
        unit_of_measurement: 'kWh'      

The L1 and L2 sensors, sensor.cabin1_9_1d and sensor.cabin2_10_1d, are provided by an Emporia Vue.

Clearly, the stored values are correct or the entity card would be wac too.

Two days ago I was able to tinker with statistics and get it working for the rest of the day. But the next morning it was back to negative values.

Last night I deleted the template sensor and recreated it with a new name and unique ID. It worked perfectly as soon as it was saved. At midnight I verified that the graphs on my dashboard were all blank waiting for the 1:00 AM refresh and went to bed. This morning, negative value equal to yesterday’s usage less today’s usage.

Other threads on the topic suggest it has to do with the timing of daily resets at midnight. While that sounds plausible, how did that happen? How do I change that? Why can’t I create a template sensor that just stays in sync? I want to display the sum of two sensors like one. Is there a better way to do that than a template sensor?

Many people stumble upon the energy-devices-graph using daily sensor data rather then total energy values. Using the totals for all these 3 sources should help. The state_class would then be ‘total_increasing’.

If you think about it, this makes sense as energy-devices-graph can be filtered on the fly to show different timeslots (today, yesterday, this year, …). And the results are calculated from the totals not the daily values.

All my statements here are based on experience and not on the fact that I have found corresponding documentation or analyzed the source code.

Following the advice of a couple of previous threads I added an automation that resets the template sensor and the two source sensors. (Even though every indication is that the sensor data is correct in the database.)
alias: Reset Cabin Daily Energy
description: Reset Cabin Daily Energy every morning at midnight.
triggers:

  • trigger: time
    at: “00:00:00”
    conditions:
    actions:
  • action: homeassistant.update_entity
    data:
    entity_id:
    - sensor.cabin1_9_1d
    - sensor.cabin2_10_1d
    - sensor.cabin_1_2
    mode: single`

The results this morning are: negative value for the template filter in the energy graph.
At this point I’m ready to accept that energy graphs and template sensors are not compatible. However it would go down a lot easier if I understood why. If anyone knows please share.

I think I cannot help you fix the issue or answer the cause but maybe I can give you an alternative solution which might prove even better for your needs.

I looked into what I guess you are using to get the power readings from your Emporia Vue. It seems to handle power readings in the sensor, but not deliver or calculate energy values.

Homeassistant already has everything to calculate the energy values, but maybe you want to try a tool I’m happily using for a long time to get more details about my power wasters, the Homeassistant integration Powercalc (installed e.g. using HACS).

With Powercalc I believe you can solve two things: You can easily get more data from your power meter and also can create a sum of both cabins:

  1. Create “real power sensors” for both cabin power meters (Real power sensor - Powercalc documentation)
  2. Create a summing group out of both devices (Custom group - Powercalc documentation)

As told I am using this solution for a long time and it is compatible with the energy analysis.

I think I cannot help you fix the issue or answer the cause but maybe I can give you an alternative solution which might prove even better for your needs.

Thank you for the suggestions for an alternate means to achieve my ends. I will look into both today.

I’m a boomer and not technically inclined but I have time on my hands and I find it very rewarding when I eventually stumble onto an answer. So I’ll keep plugging at template sensors for a while too.

The frustration is, the data is present and correct in the database. There are any number of ways to graph it. I just can’t graph it with Individual Device graphs on the Energy dashboard, yet.

I wonder if this is a clue? How do I figure out what are the two events depicted at 00:00:00 and 00:18:00? Do I have any means to control when those events take place?

Statistics graph:
statistics_graph

Click on the statistics graph:

Show more:

The problem is not limited to energy graphs. An ordinary statistics graph referencing a template sensor also starts the day with negative yesterday’s sum. From what I can tell it isn’t a bug and it isn’t a feature. It’s just a limitation.

statistics_graph

energy_graph

entities_card

Per daalja’s suggestion, I installed Powercalc and it looks like it will do the trick.

Does the Emporia Vue provide always-increasing sensors, instead of the ones that reset daily?

If not, I’d start by creating a utility_meter helper for each sensor, and don’t define a reset cycle for them. This will give you two always-increasing entities.

With that, you can then feed those two utility meter entities into your template sensor. This avoids issues with the entities resetting to zero at slightly different times from each other.

Depending on the card you use on your dashboard, this might be all you need to do. I’m not familiar with energy-devices-graph, but the normal “statistics graph” card will allow you to choose the period (hourly, daily, etc.) and the value (you’d want “change”) to give you the daily energy.

If you have a need for an entity whose state actually resets to zero, then create a 3rd utility meter that simply consumes the state of your template sensor. And set its reset cycle to “daily”.

Finally, to explain the behavior you are seeing: The template sensor you created has a state class of “total” which affects the statistics that are generated for that entity. HA will create state and sum statistics for that entity, and the sum is calculated assuming that the value can increase or decrease. So a reset is interpreted as a large negative spike that is subtracted from the running sum.

Also, you seem to be confusing the state of the entity with its statistics. Those are different sets of values stored in different tables in the database. It can be very confusing because the graph you see when you click on an entity will be from the statistics table, but if you click on “show more” you’ll see the data from the states table, which is the “normal” history. Unless you change to a date range that is earlier than your oldest history, at which point data will be pulled from the statistics table.