core: Can't set last_reset value for MQTT sensor

The problem

I tried to add an MQTT sensor to the new energy dashboard (many details are here: Energy: Not showing the expected entities for consumption - HA forum).

The sensor looks like this:

sensor:
  - platform: mqtt
    name: "Electricity used"
    state_topic: "home/smart_meter/ELECTRICITY_USED_TARIFF_ALL"
    unit_of_measurement: 'kWh'
    state_class: 'measurement'
    device_class: 'energy'

Eventually I got it to work by adding this:

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

homeassistant:
  customize_glob:
    sensor.*_used:
      last_reset: '1970-01-01T00:00:00+00:00'
      device_class: energy
      state_class: measurement

But I feel that the solution should be this:

sensor:
  - platform: mqtt
    name: "Electricity used"
    state_topic: "home/smart_meter/ELECTRICITY_USED_TARIFF_ALL"
    unit_of_measurement: 'kWh'
    state_class: 'measurement'
    device_class: 'energy'
    last_reset_topic: '1970-01-01T00:00:00+00:00'

The last_reset_topic value is advised as such here: https://www.home-assistant.io/integrations/sensor.mqtt/

However, this leads to following error in the logs:

Invalid config for [sensor.mqtt]: Single-level wildcard must occupy an entire level of the filter for dictionary value @ data['last_reset_topic']. Got datetime.datetime(1970, 1, 1, 0, 0, tzinfo=datetime.timezone.utc). (See /config/configuration.yaml, line 32). Please check the docs at https://www.home-assistant.io/integrations/mqtt

What is version of Home Assistant Core has the issue?

core-2021.8.0

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

mqtt

Link to integration documentation on our website

https://www.home-assistant.io/integrations/mqtt/

Example YAML snippet

sensor:
  - platform: mqtt
    name: "Electricity used"
    state_topic: "home/smart_meter/ELECTRICITY_USED_TARIFF_ALL"
    unit_of_measurement: 'kWh'
    state_class: 'measurement'
    device_class: 'energy'
    last_reset_topic: '1970-01-01T00:00:00+00:00'

Anything in the logs that might be useful for us?

Invalid config for [sensor.mqtt]: Single-level wildcard must occupy an entire level of the filter for dictionary value @ data['last_reset_topic']. Got datetime.datetime(1970, 1, 1, 0, 0, tzinfo=datetime.timezone.utc). (See /config/configuration.yaml, line 32). Please check the docs at https://www.home-assistant.io/integrations/mqtt

Additional information

More details and discussion here: https://community.home-assistant.io/t/energy-not-showing-the-expected-entities-for-consumption/326880 and here: https://community.home-assistant.io/t/energy-management-in-home-assistant/326854/18

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 19 (7 by maintainers)

Most upvoted comments

I can confirm, setting state_class: 'total_increasing' solves the issue, no more workaround needed in 2021.9! Thank you devs!

(now to figure out why I can’t get gas (unit_of_measurement: 'm³') to show up but first some more searching, it may just take some time, like with the power measurement)

@TonyApuzzo total_increasing is a new feature of The upcoming Home Assistant 2021.9

I’m not questioning the need for the template, if you introduce this concept it is needed. But the whole concept is not needed als far as I’m seeing. Take

counter:diff 0:NaN 10:10 15:5 2:-13 -> it’s reset. Discard value, use 2 and generate timestamp

This is how influx or elastic/kibana do it as well. My kwh counters have been working perfectly for years without the need of such a topic.

Why bother with an extra topic where providers need to implement this exact same logic themselves?

please see https://community.home-assistant.io/t/mqtt-sensor-how-to-define-last-reset-value-template/327175/11?u=mariusthvdb

we did some more experimenting, concluding the last_reset_topic can be used on its own, provided the payload contains the correct data already. If not, and one needs to extract data from the payload, then additionally use the last_topic_value_template

the latter can not be used stand alone.

this should be stated more clearly in the docs (provided it is correct of course…) Hope Frenck can confirm, if he revisits here.

I guess that is what he wrote here: https://github.com/home-assistant/core/issues/53988#issuecomment-893013962

You need to set last_reset_topic with a valid topic and last_reset_value_template with a valid template.

He forgot to add we can also use the last_reset_topic on its own…

The simple way I’m going currently is: publish the payload “1970-01-01T00:00:00+00:00” with retain flag to an abritrary mqtt topic (e.g. home/last_reset) and configure your mqtt sensors with last_reset_topic: “home/last_reset”

This way it is working without the customize blob workaround.

I haven’t defined the last_reset_value_template property for my sensors