[SOLVED] Xiaomi Mi Smart Plug - Weird Duplicated Payload in NodeRed

For some reason my Xiaomi Smart plug duplicat’s On or Off payload when switching eg if plug is turned the normal payload is OFF but then a ghost one appears and I get a 2nd payload arrive of OFF again! This is causing much grief when I want to read it’s state to do something.

For example in the attached lighting flow. The one part of my circuit turns on a boolean switch coz the switch was turned on, I then have another node turn off the boolean and turn on another boolean but the the 2nd duplicate comes and turns on the boolean I just turned off…grrr!!!

It doesn’t matter if I switch the plug on via lovelace or through nodered inject node the state still duplicates.

None of my other switches behave in this way I switch them off and there is only one payload “off”.

Has anyone had a switch behave in this way?

download

Ok I did change the title to SOLVED it’s more like “quick fix” but it does what i want it to do so for me that is a result.

Problem:
Xiaomi Smart Plug produces a duplicated (double) msg.payload for On or Off whenever or where ever it is turn off. The duplicated payload occurs either instantly or randomly up to 30 seconds after the state is changed. Which is super annoying on IF THEN flows.

Fix:
Installed node-red-contrib-deduplicate

Screenshot_1

For any newbs not sure how to get the deduplicate in your nodered. It is not there by default you need to install it.

  1. In your nodered click on the right side menu
  2. Choose manage pallet
  3. Click on install and paste the node-red-contrib-deduplicate
  4. Click on install from the searched result
  5. Once installed you will now find it in your pallete (left menu)
  6. Note there is search box at the top of the left pallete so you can find the new node quickly by starting to type duplicate/
  7. All you need to do then is stick it in between where the duplicated message is happening and the time in seconds you want any duplicate message that happens within those seconds to be caught.

Ok I thought I had solved it and then the next day I noticed the light was on again…

I put the green debug node on the state node for the Xiaomi smart plug and found that the plug is broadcasting it’s state every XX seconds unlike the other switches which only show when there is a state change.

Now the random duplicated payload makes total sense. What is happening is the plug is broadcasting it’s state every XX seconds and then me or the script is changing the state which fires off the state change but also the plug still sticks to it’s XX seconds broadcast so depending when me or the my script turns the light off in relation to the auto broadcasting of the current state will depend how close the 2nd payload happens.

It should be an easy fix once I get the time probably something using something that can implement ‘if’ ‘state’ ‘changed’ ‘then’

Diagram below showing the debug window monitoring the switch which you can see payload occuring even though there was no state change.

The solution is so stupidly simple I feel embarrassed, one of them can’t see what’s right in front of you moments where all I needed to do was tick the ‘only output on state change’ box on the state node for the xiaomi switch :man_facepalming:

Before seeing this tick box I used a function node and this code to do what the tickbox does as seen in this thread by @marthocoo

var newState = msg.data.new_state.state;
var oldState = msg.data.old_state.state;
if (oldState == "on" && newState == "off") {
    return [ msg, null ];
}
else if (oldState == "off" && newState == "on") {
    return [ null, msg ];
}
else { return [ null, null ]; 
}