Power monitoring with an XTM18S and MQTT


@timseebeck It’s very easy. The only problem was the small holes for the ‘big ‘ power cables (…y ahí es donde se nota que el XTM es un producto chino)

@j.assuncao When are you thinking about to have the templates, etc? Do you know how to ‘write’ a new valor in energy accumulated? I would like to have the same value in my HA as in the official meter of the electric company :slight_smile:


I’ve bought an equivalent for the XTM18S. It’s a ORNO OR-WE-501 (Polish concept, but also made in China), with digital display, that can handle power cables with a max. of 6mm2. Are your cables bigger than that??

Do you mean the sensor templates for Current and Apparent Power? I can post then here if you want.


Yes, please

My email address is pharizna(at)gmail(dot)com

Best regards


Nice work on this project, I have it working with a standard electric meter using the flashing LED on the front reading it using a photoresistor.

I had to remove the pullup from the attachInterupt as it got no readings, and then change the 10000L to 50000L as I kept getting double readings (confirmed by adding ‘Serial.println(interval);’ at the end of the PULSE subroutine.

Can’t remember the resistor for the photoresistor, but I’ll check if anyone is really interested.


Very interesting!

Please, more info. Thank :slight_smile:


I already had an electricity monitor I put together on an arduino saving info to an SD card, hence why I already had the flashing LED part working.

I looked at @j.assuncao’s code and realised it would work with the photoresistor.

Anyway, like I say, with the above changes to the code and the following, you should get it working with the flashing LED.

Wire the photoresistor and the 10K resistor as in THIS LINK with the voltage output going to Pin 6 on the ESP8266.


@j.assuncao Is there a way of resetting the kwhaccum variable with an automated mqtt message from HA? I’d like to reset it at midnight.

I’m more of a basic programmer than C, so my arduino knowledge (and the resulting scripts) are er … basic. I’ve taken a look at the code and only understand 30% of it :slight_smile:



Yes there is but I need to think that well. I’ll be home this weekend with time to think about this. Yiu want to reset the NodeMCU counter, right?


Yiu want to reset the NodeMCU counter, right?

If that’s the accumulated kWh then yes, that’s the idea. cheers :slight_smile:


While I’m here ;), I’d like to mention that I’ve broken your software. I use a quantity of electricity and it seems it’s a little too much for your calculations and resets the board.

Here’s a bit of error code.

- - - - - - - - - - - -
- Current kWh: 0.0500

 ets Jan  8 2013,rst cause:4, boot mode:(3,6)

wdt reset
load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
- - - - - - - - - - - - - - - - - - - - - - - - - - -
MAID - Energy Monitor v0.6.6 (15/10/2017)
- - - - - - - - - - - - - - - - - - - - - - - - - - -

at the top there, the first 2 lines are time in milliseconds between pulses, and the second is the number of pulses.

I’ve changed your code a little so I can test without a meter and also added a couple of lines etc. hope you don’t mind.

I’ve made my first (real) repository here … https://github.com/RoadkillUK/MAID2


I’ll take a look at it as soon as possible but it might be something you added to the code.

Try googlingthe cause and boot mode you have. Those two codes will tell you what the error is.


I’ve literally just fixed the crashing issue, it was one of the dtostrf commands, took me hours! It doesn’t seem to like numbers above 9999 (I think).

  dtostrf(watt, 5, 0, wattString);                                          // Convert Current Watt to string


What are you doing with it?


What am I doing with what, the project? I’m using a flashing LED from the front of my electric meter for Home Assistant … it’s working great :slight_smile:

Or did you mean, what am I doing with dtostrf? Then I don’t know, you put it there :slight_smile:

Or did you mean, what am I doing with such a high number? I have an 8kW shower.

Did I cover the original question :wink:


I was asking about the high number, sorry for not being clear


Yeah, an 8kW Shower, 3kW Electric kettle and recently a Nissan Leaf :slight_smile:


My code should handle all that…


Anything above 10000 kW caused a reset as shown. :confused:


Probably the ldr can’t handle all those blinks and the counter is overwhelmed…


Nope, it’s not the LDR, that’s fine, it’s the line I changed.