Power monitoring with an XTM18S and MQTT


Hi @Pharizna , i ’ ve connected xtm18sa with Sonoff to my fuse box and finally it seems is working on HA.

However , i only see pulses on graphics at grafana and i don’t be sure it’s showing right data.

Is it necessary to create a template sensor to modify it with any mathematical function to achieve “real” values?


Try using this configs on your graphs:




You have to change Graphana graph line configuration in order to look “continuous” and not pulse… as @j.assuncao has just explained :slight_smile:

Your data seems good because with this configuration one count is equivalent to 120 w and for this reason you can see in your graph a lot of “0” values… and “120” because you are working at minimun of the scale.

You have to create (using templates) some function to “accumulate” data for days, month, etc… and also to reset this data hourly… monthly


thanks @j.assuncao

I already had selected format you mentioned (actually i have cheched all possibilities):

I have no idea what else i can test


thanks @Pharizna

That’s what i thought , but i’m affraid that’s my handicap (templates with math functions).

Is there any possibility to send my a little example in order to start working with?

Thanks in advance!!


Here’s mine, I have fill(linear) in the GROUP BY



Hello, then the cables that go to the wemos or the sonoff can be connected directly without the need of any resistance or logic converter?

Someone came to measure with the oscilloscope the volge of these pulses?


Yes, i have mine running for almost one year without problems.


Hello again,

This sensor measures the apparent power or only the active power (which is the one that matters)?


It measures active power.


Thanks @RoadkillUK

I have change it to fill(linear) and graphics looks more like i expected:

However, i’m still struggling with templates in order to get “real” consumption and cumulative values.

I can’t find any example code about this in HA forums .

I’m starting to think this is a secret (like “Coca-cola formula”…)


Hi @Pharizna

I have ready access to a sonoff basic, do you have updated code for arduino idea, or does it only work with plat form io, I want to try this project with Sonoff, must just get a meter with a pulse output.



Is there a need to alter the code when doing this with Sonoff, I have arduino ide, should I change to platformio.



No, you don’t need to change the code.


I’ve connected with a standard jumper wire the 20 port to the D6 pin in the Wemos D1 mini while the 21 port to the ground pin.

@timseebeck How did you do this task without any soldering?


[…] and this operation does not involve any soldering tasks.

Sorry, my bad :frowning: I’m assuming here that the wemos comes with pin headers already soldered which I don’t think it’s the usual case.


On Aliexpress it’s not the case. But on Bangood, it is. But the price is not the same.


Hello, thank you for your script.
I have only not the good current usage. Can it be that it is calculated by how many pulses there are in 1 minute? That is not realy accurate maybe you can change it so it can be caluted by the times between the pulses?


Hi, thanks! Even if we count the time between two consecutive pulses we are not going ot have the good current usage: only in the precise moment when you get the pulse and do the calculation. Let’s remember this:

So, in summary, with this device we are not going to have an accurate current (or instantaneous) value of the consumed power. I prefer to do the calculations on a fixed time basis rather than on time-difference method because with the latter approach your measurement is not updated if there is no consumption (which will lead you to think that your appliance is ON while maybe it isn’t).


Hi I tested it with a XTM18SA and a 60W bulb … but “Inst power 01” show me 240.00W
…it is 4 times the real consumption