Benjamin Schieder


2022 July 12 | 0 comments

I still use HomeAssistant to monitor and automate my house, from presence of anyone, to the status of my 3DPrinter to temperature and whatnot. Recently I wanted to add monitoring of my power consumption to the mix. Should be easy, right? Yeah, right….

First off, I have an LK13BE power meter. These come with an IR diode sending OBIS information in a text format about kWh consumed total, last 365 days, last 30 days, last 7 days, current power consumption and a host of other useful information. Unfortunately, the IR diode on my device seems to be too weak to be picked up by an IR receiver, which apparently is a common issue with these devices. I could file a complaint with my power company and have it replaced, but that feels like a lot of hassle for very little personal gain.

I tried a few other things over the course of 2 weeks, but eventually settled on a workable solution: I attached an LM393 photo resistor module to D4 of an ESP8266 flashed with Tasmota, configured that pin as a switch and enabled SetOption114 to make the switch send an MQTT message to Home Assistant instead of actually switching a relay.

On the Home Assistant side, things were a little trickier. I recently attached a similar setup (Tasmota + Reed switch) to my gas volume counter to monitor my gas consumption. For that, I simply configured a sensor like this which gets increased every time the Reed switch closes:

- sensor:
  - name: "Gas consumption m³"
    unit_of_measurement: "m³"
    device_class: gas
    state_class: total
    unique_id: gas_m3
    state: >
      {{ states.counter.gasconsumption.state | float / 100 }}

That works quite well and I can configure this sensor in the energy dashboard. I thought that doing the same (creating a counter, attach it to a template sensor, use sensor in Energy Dashboard) would work for the power consumption as well:

- name: "Total Power consumption kWh"
  unit_of_measurement: kWh
  device_class: energy
  state_class: total_increasing
  unique_id: power_kwh
  state: >
    {{ states.counter.power_wh.state | float / 1000 }}

But no, the energy dashboard would not allow me to use this sensor, at all. After a lot more fiddling, I finally settled on a very strange, but working solution to this problem. First, I defined an MQTT sensor (which I CAN use in the energy dashboard):

- platform: mqtt
  unique_id: "Home/Energy/Consumed/Total"
  state_topic: "Home/Energy/Consumed/Total"
  name: "Total energy consumed"
  value_template: '{{ }}'
  device_class: energy
  state_class: total_increasing
  unit_of_measurement: Wh

This sensor now holds total energy consumed, but wouldn’t remember its value across Home Assistant reboots. the state_class: total_increasing will take care of that. Every time the sensor resets to 0, Home Assistant will still increase the value in the Energy Dashboard.

But how do I actually get values INTO this sensor? Tasmota sends an MQTT message of “switch on” or “switch off” only. I created this automation for that:

  alias: Increase by 2 Wh
  description: ''
  - platform: device
    type: turned_off
    device_id: 7358380390f1d4f96277555630486944
    entity_id: switch.tasmota_powermeterpowermeter
    domain: switch
    condition: []
  - service: mqtt.publish
    topic: Home/Energy/Consumed/Total
    payload_template: >-
      {"energy": { "total": {{ states.sensor.total_energy_consumed.state | int \+ 2 }} }}
  mode: single

Walking throught this automation: First, why do I increase by 2 Wh per impulse? My power meter sends 500 impulses per kWh. Your’s might be different, it’s common to have 1000 impulses per kWh as well. Adjust as required.

Next, the automation triggers when the switch on the Tasmota turns off. It doesn’t really matter if you trigger on on or off, either one will work. No additional conditions required.

The magic now happens in the action. This automation, when triggered, will publish a new MQTT message to the topic Home/Energy/Consumed/Total which is the topic of the MQTT sensor defined above. The payload will look like this: {"energy": { "total": 1234 }}. The value 1234 is calculated from the current value of the sensor and adding 2 to it (remember: 1 impulse equals 2 Wh for my power meter. Yours may be different. Adjust as necessary). This MQTT message will then again be picked up by Home Assistant and the sensor definition will be updated with the new value.

It’s a bit convoluted setup, but it works well and I didn’t need to create a custom integration for it.


Category: blog

Tags: omeAssistant LK13BE SmartMeter Tasmota

Post a comment

All comments are held for moderation; basic HTML formatting is accepted.

Name: (required)
E-mail: (required, not published)
Website: (optional)