Adding Missing Attributes to Sensor for Tasmota Electric Meter Device

Hi,
I’m pretty new to Home Assistant. After some initial, straightforward tasks, I’m now trying to add data from a two-way electricity meter in the Energy Dashboard.

The data comes from a Tasmota IR device that’s attached to the front of the electric meter. I configured the device to send the three values I want (current power, meter reading into national grid, and meter reading taken from national grid ) via MQTT.
In HA, I installed and configured the Mosquito broker and Tasmota integrations.
The device and entities appear in the Tasmota integration. In Developer Tools, I see the entities with the values and the corresponding attributes:

This seemed promising. But based on what I’ve read in some other posts and in the docu., the reason why I can’t add these sensors to the Energy Dashboard are missing attributes: device_class, state_class, unit_of_measurement, and maybe others.

I’m really struggling to figure out what I need to add and where/how to add them.
This is what I have so far in the configuration.yaml:

mqtt:
  sensor:
    - name: "Two-Way Meter"
      state_topic: "tasmota_two_way_meter"
      unit_of_measurement: "kWh"
      device_class: energy
      state_class: total_increasing
      force_update: true

Is this all I should need? One thing I’m not sure about is the state_topic entry. This is the name that I entered in the Tasmota device’s MQTT settings as the topic name; is that what is needed here?

When I look at the entity, I see the required attributes and it is accepted in the Energy Dashboard. But the state is 0.0, which doesn’t match any of the other sensors from the device at the same point in time.

I’m also wondering how I get the three values that I have from the Tasmota device…
I assume I need three sensors in line with the three that I have by default via MQTT, but in the settings that I entered in the configuration.yaml, I don’t see how I can keep the three apart.

Can anyone help me out here? Any pointers would be much appreciated.

After even more searching, trial and error, and burning the midnight oil, I managed to get it to work :slight_smile:

Just in case anyone else is stuck with this, here is what I had to do:

  1. Determine the right state_topic (including path) to enter. In MQTT Explorer, I found two: one that started with tasmota/discovery and another that started with tele. Since most examples in other people’s questions contained tele, I assumed that was the right one. Ironically, it was only the tasmota/discovery path that worked when I tried listening to this topic in the Mosquitto broker integration in HA. I still don’t understand what the difference is between the two. The tele option seemed to give more up-to-date values, though.

  2. The other problem, which I glossed over initially, was the crucial importance of value_template. Without this, I don’t think it would ever have worked. The json payload from the two paths I mentioned above were of course different, so more trial and error. In the end, the following sources helped me to figure out what I needed to enter as the value_template: MQTT, extract data from energy meter - json - #2 by myle (expland the response!) and Templating - Home Assistant

My Energy Dashboard is a little bare at the moment, so it’s time to add more sensors :wink:

Hi, I also have a similar problem. I created a MQTT sensor via the configuration.yaml for my smartmeter and see the power consumption being reported as sensor in the device/entity overview.

mqtt:
  sensor:
    - name: "Energy Total In"
      unique_id: "energy_total_in"
      state_topic: "tele/tasmota_sml/SENSOR"
      value_template: '{{ value_json["MT175"]["E_in"] if value_json["MT175"]["E_in"] is defined else states("sensor.energy_total_in") }}'
      unit_of_measurement: "kWh"
      device_class: energy
      state_class: measurement
#      attribute:
#        last_reset: '1970-01-01T00:00:00+00:00'
    - name: "Power In"
      state_topic: "tele/tasmota_sml/SENSOR"
      value_template: '{{ value_json["MT175"]["P"] }}'
      unit_of_measurement: "W"
      device_class: power
      state_class: measurement
    - name: "Energy Total Out"
      unique_id: "energy_total_out"
      state_topic: "tele/tasmota_sml/SENSOR"
      value_template: '{{ value_json["MT175"]["E_out"] if value_json["MT175"]["E_out"] is defined else states("sensor.energy_total_out") }}'
      unit_of_measurement: "kWh"
      device_class: energy
      state_class: measurement
#      attribute:
#        last_reset: '1970-01-01T00:00:00+00:00'

However, I still got error/warning messages such as last_reset value is missing in the dashboard configuration. All tries to add a fixed value resulted in errors, as attribute/last_reset values are not defined for the mqtt template.

Do you have a hint how/where to add it or which steps are needed to get the values appear properly in the energy dashboard?

Thank you in advance.
Martin

Ok, I found addidional documentation and could figure out that my that my total energy measures were set to the wrong state class “measurement” (which is a point estimate like current power). I had to correct them to: total_increasing as suggested in the sensor entity documentation: Sensor Entity | Home Assistant Developer Docs

So now my configuration.yaml works:

mqtt:
  sensor:
    - name: "Energy Total In"
      unique_id: "energy_total_in"
      state_topic: "tele/tasmota_sml/SENSOR"
      value_template: '{{ value_json["MT175"]["E_in"] if value_json["MT175"]["E_in"] is defined else states("sensor.energy_total_in") }}'
      unit_of_measurement: "kWh"
      device_class: energy
      state_class: total_increasing
    - name: "Power In"
      state_topic: "tele/tasmota_sml/SENSOR"
      value_template: '{{ value_json["MT175"]["P"] }}'
      unit_of_measurement: "W"
      device_class: power
      state_class: measurement
      unique_id: "power_in"
    - name: "Energy Total Out"
      unique_id: "energy_total_out"
      state_topic: "tele/tasmota_sml/SENSOR"
      value_template: '{{ value_json["MT175"]["E_out"] if value_json["MT175"]["E_out"] is defined else states("sensor.energy_total_out") }}'
      unit_of_measurement: "kWh"
      device_class: energy
      state_class: total_increasing

So as additional information about the complex value_template parsing: my smartmeter sends the current power consumption once a second and then only transmits mqtt message with time and P. Once in a minute the message also contains E_out and E_in, such that these are only set if included. Then it takes the old value stored in the states already. (With the wrong state_class: measurement it was actually set to zero/undefined again without the additional checking…)

Best, Martin