Smart Meters and Tasmota

Introduction

Wouldn't it be great to know the energy consumption of your home? Wouldn't it be better to have a solution integrated into your smart home?

Well, that's what we are going to do here. The energy meter in question is the E230 from Landis+Gyr. It has various interfaces to communicate with the outside world. One of them is light pulses based on the current energy consumption. An LED delivers 1000 pulses per kWh, which we can capture and convert into something readable with the help of Tasmota.

Photoresistor

I bought a photoresistor breakout board with adjustable threshold to convert light intensity into digital signals. A photoresistor is not the fastest sensor to detect light, but it is fast enough for this application.

Firmware Compilation

By default, the Smart Meter Interface is not included in the precompiled binaries. This means that we have compile our firmware image by ourselves. Thanks to Docker, this is easier than ever.

First clone the docker-tasmota git repository.

To enable our desired feature, let's edit user_config_override.h. At the very end of the file, but before the last #endif we insert this:

#ifndef USE_SCRIPT
#define USE_SCRIPT
#endif
#ifndef USE_SML_M
#define USE_SML_M
#endif
#ifdef USE_RULES
#undef USE_RULES
#endif

This will enable the Smart Meter Interface with scripting support.

By default the firmware will be compiled for the ESP8266, but since I want to use a ESP32 based platform I need to edit platformio_override.ini. Uncomment tasmota32 and comment tasmota:

; *** Build/upload environment
default_envs =
; *** Uncomment the line(s) below to select version(s)
;                tasmota
;                tasmota-debug
;                tasmota-ircustom
;                tasmota-minimal
;                tasmota-lite
;                tasmota-knx
;                tasmota-sensors
;                tasmota-display
;                tasmota-zbbridge
;                tasmota-ir
                tasmota32
;                tasmota32-webcam
;                tasmota32-minimal
;                tasmota32-lite
;                tasmota32-knx
;                tasmota32-sensors
;                tasmota32-display
;                tasmota32-ir
;                tasmota32-ircustom
;                tasmota32solo1
;                tasmota32-odroidgo

Lastly, we don't want to use the latest bleeding edge version of tasmota. Therefore in compile.sh set USE_STABLE to 1:

# Set to `1=true` to use latest stable release tag
# Set to `0"false` to use `development` branch (default)
USE_STABLE=1

After everything is configured you just need to execute:

./compile.sh

You will find your compiled firmware under Tasmota/build_output/firmware/.

Flashing the firmware

Like last time we use ESP_Flasher to upload the firmware. No suprises here :)

Flashing the firmware

Tasmota will open a WIFI network called tasmota-something (something is replaced by some random number). Connect to it and set up your home WIFI connection. After you connect to it again in your home WIFI, the page should look like this:

Flashing the firmware

Scripting

Navigate to the scripting page of Tasmota Consoles->Edit Script. Here we need to insert our script to load the Smart Meter Interface and specify which sensor we want to use.

The Script

The script for fast copying:

>D  
>B  
->sensor53 r
>M 1  
+1,23,c,0,-10,Landis
1,1-0:1.8.0*255(@1000,Energy Meter,kWh,Total,3
#

You can read everything about the script in the documentation, but I will try a short explanation.

  1. >D

    This section can be used to define text variables for the script. We don't use any, therefore it's empty.

  2. >B

    The bootup section is used to inform the interface to read the meter descriptors.

  3. >M

    This section is where we define the actual meter. The number next to it, so for >M 1 it is 1, specifies the number of connected meters.

    The line +1,23,c,0,-10,Landis consists of the meter number, so for +1 it is 1 (we only have one). 23 stands for the GPIO number. This is were the LDR sensor is connected to. c is the type of meter, meaning "counter type". 0 stands for counter without pullup. -10 stands for the debounce time in ms for IRQ driven counters, if positive it stands for the counter poll interval. Lastly Landis is the prefix for the Web UI.

    The next line 1,1-0:1.8.0*255(@1000,Energy Meter,kWh,Total,3 is for converting the result. The first number 1 stands for the meter number. 1-0:1.8.0*255 aftwards stands for counter value. Don't ask me more about this entry, it just works ;) Next, 1000 is the scale. This means the count is divided by 1000. Then follows the label Energy Meter, the unit of measurement kWh, the MQTT label Total and the precision with 3 decimal places.

Don't forget the # in the last line! I did...

Likewise, don't forget to tick "Script enable".

After a restart the main page should look like this:

Flashing the firmware

Success!

You can now connect your LDR sensor and upon every toggle, the energy counter should increment by 0.001.

Conclusion

Tasmota is great for integrating all kinds of sensors into your smart home. And when the default sensor support is not enough for you, you can compile your own firmware and enable various extensions.

Once again it was fun to work with Tasmota, but it took me a while to get everything right.

By the way, if you are curious how I connected the sensor to the energy meter, here is a picture:

LDR sensor connected to energy meter

This is more of a temporary solution, but it worked great ;)