MQTT Sensor : how to define last_reset_value_template

I only once find ZP Import on this page, and it is an example from marius using yaml anchors. You can ignore it.

The important message from this whole topic is: if you want to use your mqtt sensor in the energy tab, it needs at least 4 extra items :

    device_class: energy
    state_class: measurement
    last_reset_topic: 'electricity1/tele/RESULT'
    last_reset_value_template: '1970-01-01T00:00:00+00:00'

device_class, state_class, last_reset_value_template: always as shown
last_reset_topic: set it to your state topic

2 Likes

Like francisp explained, that’s in the example posted by Mariusthvdb. When I posted a revised version of his example, I purposely excluded the YAML anchor to simplify it and avoid confusion. :slightly_smiling_face:

A YAML anchor let’s you duplicate a section of YAML elsewhere simply by referring to it by name. I provided an example for someone else in this thread.

1 Like

Tanks but for me it needs to be without a uppercase letter

sensor.*_energy:

I also have my power and engery readings via mqtt. But I couldn’t add these lines using VSCode as an editor. Well, I could! But it complaints.

    state_class: measurement
    last_reset_topic: 'electricity1/tele/RESULT'
    last_reset_value_template: '1970-01-01T00:00:00+00:00'

Because it keeps complaining that “Property state_class is not allowed”. Same complaints for the other two lines of YAML.

Does anyone know a solution for this annoyance?

The complaint comes from VS Code Config Helper. It works with VS Code to identify potential errors in your YAML code. It probably hasn’t been updated yet so it falsely identifies those two options as being “not allowed”.

The next update will probably correct it.

I know this is an older thread but I am really struggling with getting this to work.

I have a ZooZ z-wave power strip. The MQTT topic for power monitoring returns this json:

{
    "value_id": "6-50-1-0",
    "node_id": 6,
    "class_id": 50,
    "type": "decimal",
    "genre": "user",
    "instance": 1,
    "index": 0,
    "label": "Instance 1: Electric - kWh",
    "units": "kWh",
    "help": "",
    "read_only": true,
    "write_only": false,
    "min": 0,
    "max": 0,
    "is_polled": false,
    "value": 111.02,
    "lastUpdate": 1651274701101
}

I believe the ‘lastUpdate’ element is what should be used for the last_reset_value_template:

Here’s what my Yaml Looks like:

 ### Power Monitoring ###
  - platform: mqtt
    name: Office_Power_Strip_Total_kwh
    state_topic: "zwave2mqtt/6/50/1/0"
    value_template: "{{value_json.value}}"
    last_reset_topic: "zwave2mqtt/6/50/1/0"
    last_reset_value_template: "{{value_json.lastUpdate}}"
    unit_of_measurement: "kWh"
    device_class: energy
    state_class: measurement

But for the life of me, I cannot get this last reset value to populate.

Does anyone have any ideas on what i’m doing wrong?

That does not seem a date.

Its an epoch / unix timestamp.

try this:

 last_reset_value_template: "{{value_json.lastUpdate | int | timestamp_custom('%d.%m.%Y %H:%M'}}"

still nothing… (Had to put a close “)” on that to make it work)… I’ve even tried hardcoding the date in the right format but never got HA to recognize it.

I know this topic is old and you may know why this happened now but for anyone arriving here as I did I believe it’s because of the state_class being set to measurement in your case.

I used the state_class: total and the value I put in to last_reset_value_template is then populated.

But TOTAL defeats the purpose of measurement. TOTAL means acumulated amount, while measurement mean sin current time. Lets say energy - some device gives you how many watts it’s using right now, TOTAL wont be good in this case. It’s DUMB to require reset with measurement. What reset? it resets everytime device is off :smiley: