core: Energy Dashboard (Long-Term Statistics) not compatible with frequently-reset entities

The problem

My energy company provides an MQTT stream of minutely energy usage (effectively “Watt-minutes”). For example:

event/metering/summation/minute {"type":"minute","time":1629385500000,"value":947.7706166666667}
event/metering/summation/minute {"type":"minute","time":1629385560000,"value":880.4405333333333}
event/metering/summation/minute {"type":"minute","time":1629385620000,"value":876.5172916666667}
event/metering/summation/minute {"type":"minute","time":1629385680000,"value":869.5383583333333}

(The time value is a Unix-like timestamp, in milliseconds, indicating the start of the summation, so effectively a “last reset” timestamp when divided by 1000.)

I updated my MQTT sensor (as shown in the YAML section below) which allowed the entity to be selectable under “Grid Consumption” in the Energy Dashboard configuration.

Unfortunately, the new Long-Term Statistics feature only records one entry per hour, so the Energy Dashboard only sees one of the 60 energy measurements, providing no value even though the data is available (and graphable by a history card, for example).

I then attempted to set up a new state-based template sensor from an hourly utility_meter, but last_reset is not valid in a template sensor!

It would be very nice if there was a way to use energy entities that are updated/reset frequently.

If anyone has any suggestions for a work-around, I’d be willing to give them a try.

What is version of Home Assistant Core has the issue?

core-2021.8.8

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

energy

Link to integration documentation on our website

https://www.home-assistant.io/lovelace/energy/

Example YAML snippet

sensor:
  - platform: mqtt
    name: "Household Electricity Usage"
    icon: mdi:transmission-tower
    state_topic: "event/metering/summation/minute"
    unit_of_measurement: 'kWh'
    # the Energy Bridge returns "Watt-minutes" every minute in the "value"; convert it to kilowatt-hours
    value_template: "{{ value_json.value | float / 60000 }}"
    last_reset_topic: "event/metering/summation/minute"
    # the "time" in the message is a Unix-like timestamp (in milliseconds) of the start of the last reading
    last_reset_value_template:  "{{ now().fromtimestamp(value_json.time / 1000).replace(tzinfo=now().tzinfo) }}"
    device_class: energy
    state_class: measurement

I also attempted to create a state-based template sensor from an hour utility meter:

template:
  - sensor:
      - name: hourly_electricity_usage
        icon: mdi:transmission-tower
        unit_of_measurement: 'kWh'
        state_class: measurement
        device_class: energy
        state: "{{ states('sensor.hourly_energy_kwh') }}"
        last_reset: "{{ now().replace(minute=0, second=0, microsecond=0) }}"

Anything in the logs that might be useful for us?

but last_reset isn’t valid:

[homeassistant.config] Invalid config for [template]: [last_reset] is an invalid option for [template]. Check: template->sensor->0->last_reset. (See /config/configuration.yaml

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 38 (33 by maintainers)

Most upvoted comments

Thanks Erik! 👍 I’ve applied the patch.

I assume this will need to run for a while with the updated code to see the effects. I’ll report back in a day or so and see if the cost estimate in HA looks closer to reality.