Long term statistics from mqtt?

So after todays release of 2021.08 with the new long term statistics and energy focus I wanted to include my IKEA Sparsnäs reciever. I read up on the info page Sensor Entity | Home Assistant Developer Docs and added what I thought was the required parts to my mqtt:

sensor:
  - platform: mqtt
    state_topic: "EspSparsnasGateway/+/state"
    name: "House power usage"
    unit_of_measurement: "W"
    value_template: '{{ float(value_json.watt) | round(0)  }}'
    icon: mdi:flash-circle
    state_class: measurement
    device_class: power

  - platform: mqtt
    state_topic: "EspSparsnasGateway/+/state"
    name: "House energy usage"
    unit_of_measurement: "kWh"
    value_template: '{{ float(value_json.total) | round(0)  }}'
    icon: mdi:flash-circle
    state_class: measurement
    device_class: energy

I can see the attributes in the dev tools but I still can’t select it in the energy config screen?

Is there some other setting I need to configure to?

1 Like

You need the attribute last_reset as well, you can configure it through customization.

E.g. from my system

homeassistant:
  customize:
    sensor.energy_daily_lights:
      last_reset: "2021-07-30T00:00:00+00:00"
3 Likes

Awsome works great now!

Seems that after a day (the normal reset period), or maybe even directly after you’ve added it to the Energy dashboard you should remove this customization.

Or am I missing something?

I think you can keep it, just change the date if it ever resets for some reason?

If you have a sensor which never reset you can also add this:

according to the dev. documentation:

last_reset: "homeassistant.util.dt.utc_from_timestamp(0)"

The time when an accumulating sensor such as an electricity usage meter, gas meter, water meter etc. was initialized. If the time of initialization is unknown and the meter will never reset, set to UNIX epoch 0: homeassistant.util.dt.utc_from_timestamp(0). Note that the datetime.datetime returned by the last_reset property will be converted to an ISO 8601-formatted string when the entity’s state attributes are updated. When changing last_reset, the state must be a valid number.

See this topic for a solution without customize :

very interesting if this works, since I would have expected an actual value & not a function to get a value. This is also not a template field afaik (typically surrounded by {{ }}

The topic started with the question why you can’t do it, so likely not possible at the moment :wink:

I could not do it through MQTT auto-discovery either (by adding last_state to the config payload)

So the workaround as proposed above is to use customization

MQTT can do it without problem

This is autodiscovery from Zigbee2MQTT :

{
  "availability": [
    {
      "topic": "zigbee2mqtt/bridge/state"
    }
  ],
  "device": {
    "identifiers": [
      "zigbee2mqtt_0x04cf8cdf3c77ea45"
    ],
    "manufacturer": "Xiaomi",
    "model": "Mi power plug ZigBee EU (ZNCZ04LM)",
    "name": "mi_outlet",
    "sw_version": "Zigbee2MQTT 1.21.0"
  },
  "device_class": "energy",
  "json_attributes_topic": "zigbee2mqtt/mi_outlet",
  "last_reset_topic": "zigbee2mqtt/mi_outlet",
  "last_reset_value_template": "1970-01-01T00:00:00+00:00",
  "name": "mi_outlet_energy",
  "state_class": "measurement",
  "state_topic": "zigbee2mqtt/mi_outlet",
  "unique_id": "0x04cf8cdf3c77ea45_energy_zigbee2mqtt",
  "unit_of_measurement": "kWh",
  "value_template": "{{ value_json.energy }}"
}

This is a manual created mqtt sensor :

  - platform: mqtt
    name: "Mains Consumed Energy"
    state_topic: "electricity1/tele/RESULT"
    value_template: >- 
      {% set message = value_json.SerialReceived %}
      {% set payload = message[6:14] %}
      {% set payload_len = (payload | length) %}
      {% set result = namespace(value='') %}
      
      {% for i in range(0, payload_len + 1) | reverse -%}
        {%- if i is divisibleby 2 -%}
          {%- set result.value = result.value + payload[i:i+2] -%}
        {%- endif -%}
      {%- endfor -%}
      
      {{ (result.value|float) / 100 }}
    unit_of_measurement: 'kWh'
    unique_id: "mains_consumed_energy"    
    device_class: energy
    state_class: measurement      
    last_reset_topic: "electricity1/tele/RESULT"
    last_reset_value_template: '1970-01-01T00:00:00+00:00'

Both working in the energy tab without customization !

1 Like

Nice hack of the last_reset_value_template!

From Hass 2021.09 the solution going forward would be to use a new state class, state_class: total_increasing. Then you do not need any last_reset, last_reset_topic or last_reset_value_template attributes any more

2 Likes