Power monitoring with an XTM18S and MQTT


I don’t know about 4x but the PDF for the XTM18SA states:

Pulse output rate (pins 20 & 21) 2000 pulses per kWh (0.5Wh/imp)

so, I can see how it would double.


@RoadkillUK @timseebeck I tried again with a 20W bulb and it show me 240.00W… the same with a 15W bulb…

but there is a strange thing… it show 240W for about 15 seconds, then it show 0.00 for about 1 o 2 minutes… then 240W for 16 seconds again

My XTM18SA 50A is this http://f02.s.alicdn.com/kf/HTB1gOuKFVXXXXcnXVXX.PRXFXXXs.pdf


Hi I saw you used XTM18SA… how do you edit the sketch to works fine with it?
I had bad values


Hi @vpomax I would say that there is nothing wrong in your code. I use a SEND_FREQUENCY = 120000 ms, that is, I’m making the calculations and sending the values to HA every 2 min. Based on what you say, I think you have changed this to 15 s (correct me if I’m wrong). So I guess you wanted a more updated info, which is good, but can lead to the issue you are facing.
Let me explain the phenomenon with this table:


First column is time. In second column I’ve placed a device consuming 20W. In the third column I calculate the accumulated energy with:


The fourth column says Yes when the accumulated energy achieves 0.5 Wh (based on the PDF link with the specifications you sent) and the XTM device sends a pulse. The last column represents the power sent to Home Assistant. When there are no counting pulses, the power is zero. When there is a pulse, all the energy is consumed in the time interval you set (15 s) providing the larger value.

In summary, you are trying to see a very small consumption (20-60W) very rapidly (every 15s) with a large range measurement device (1 pulse every 0.5Wh). The solution? There is not. It will work OK when you place it with an appliance that consumes more power and you increase the period between updates. You just have to find the relation between these three factors that best suits to your situation.


hi @timseebeck
I’m using the code in your correct version of April 17, 2017.
I did not change the SEND_FREQUENCY parameter.
Without any change it always gave me 240.00W with both the 20W bulb and 60W.
Then at the suggestion of RoadkillUKvisto that I use the XTM18SA I modified the row of the pulse inserting 2000 instead of 1000
#define PULSE_FACTOR 2000 // Number of pulses per kWh of your meeter.
After this the instantaneous consumption was 120W.

I understand that the problems that I have is due to the low power absorbed by the light bulb, in fact a few days ago trying the 2100W hairdryer the data were more consistent.

Do you suggest me to leave the code like this and test again with hairdryer?


Then I don’t understand why your values are changing every 15 s.

Yes, I would do that.



I tested again with my hairdryer Philips 2100W

It show me…

  • at max power and max temperature a value of 2280W but after 18 secs it show 2160W and after 18 secs 2280W… again… continuosly
  • at half power and half temperature a value of 720W but after 18 secs it shaw 840W and after 18 secs 720W again…

why does it continually float between 2280 and 2160W or between 720 to 840W?

Real values tested with a power meter Peaktech 9024 are 1700W at full power and 600W at half power


For the same reason the values I presented in a table (see post above) floated between 120 a 0 W: the period of the device counting pulses is not the same as the period when values are sent to HA.

If you rely on the accuracy of the Peaktech 9024 then you can modify the PULSE_FACTOR parameter to match the power. So take it as a calibration procedure of the cheap XTM device.


Now I’m using this sketch of @j.assuncao https://gist.github.com/jorgeassuncao/40956bbe4ac8db9fdb8f0526a4e0c0fe

with these variations…

/**************************** Pulses per kWh ************************************/
#define PULSE_FACTOR 2000         // Number of pulses per kWh ---> set to 2000

/**************************** Breathing LED ************************************/
#define BRIGHT    350                   // Max led intensity (1-500)
#define INHALE    1250                  // Inhalation time in milliseconds.
#define PULSE     INHALE*2000/BRIGHT    // Pulse ---> set to 2000
#define REST      1000                  // Rest Between Inhalations.

/****************************  ************************************/
unsigned long SEND_FREQUENCY = 120000;   // Minimum time between send ---> set to 120000
volatile unsigned long pulseCount = 0;
volatile unsigned long lastBlink = 0;
double kwh;
unsigned long lastSend;

And I have good values… at max power and max temp it show me a stable 1845W

I tested consumption of my hairdryer with…
PeakTech 9024 … 1720W
Blitzwolf Smart plug … 1791W
Xioami smart plug … 1789W

I saw there is a updated version of @j.assuncao sketch called MAID-EM but it is for Platformio… I need a Arduino IDE version


Hi guys, I wanted to know if it was possible to reset the meters every day


Now I tested it with a 300W halogen bulb light…

  • Xiaomi smart plug: 331W
  • Blitzwolf smart plug: 327W
  • PeakTech 9024: 304W

…XTM18SA… some times 185W and sometimes 201W and sometimes 247W :cry:

someone can help me?


It will work on Arduino IDE too.


ok… thank you @j.assuncao


Pls i cant compiling MAID-EM v2 - Energy Monitor, pls it is complet code for platformio ??