xiaomi sensors to mqtt

V 0.1: Initial version

Listen to sensor reports of a xiaomi home gateway. This is an early version and it will be improved over time.

What's working is:

  • reliably report motion state
  • reliably report door/window state

Whats missing yet:

  • turn motion sensor off, if there's no update within ~65 seconds (minimum open state interval is 120 seconds and could be reduced to 62~65 seconds)
  • retain values for door and motion sensors
  • not all switch reports are handled properly (unhandled no_close)
  • not all cube reports are handled properly (unhandled rotation)
[{"id":"7f85a82e.909a48","type":"mqtt out","z":"be3a4531.4610c8","name":"","topic":"","qos":"","retain":"","x":670,"y":500,"wires":[]},{"id":"99abc2c4.2dbda","type":"debug","z":"be3a4531.4610c8","name":"","active":false,"console":"false","complete":"false","x":690,"y":460,"wires":[]},{"id":"d3529a36.34cd8","type":"function","z":"be3a4531.4610c8","name":"","func":"var newMsg = {};\nif (msg.payload.cmd == 'report') {\n    var data = JSON.parse(msg.payload.data);\n    if (data.no_motion) {\n        newMsg.payload = \"no_motion\";\n    } else if (data.status ){\n        newMsg.payload = data.status;\n    } else {\n        newMsg.payload = msg.payload.data;\n    }\n    newMsg.topic = 'xiaomi/' + msg.payload.model + '/' + msg.payload.sid + '/status';\n    return [msg, newMsg];\n} \n\nreturn null;","outputs":"2","noerr":0,"x":520,"y":480,"wires":[["99abc2c4.2dbda"],["7f85a82e.909a48"]]},{"id":"df3c8f11.dc1e7","type":"json","z":"be3a4531.4610c8","name":"","x":380,"y":480,"wires":[["d3529a36.34cd8"]]},{"id":"62b13610.673b5","type":"udp in","z":"be3a4531.4610c8","name":"","iface":"","port":"9898","ipv":"udp4","multicast":"true","group":"","datatype":"utf8","x":170,"y":480,"wires":[["df3c8f11.dc1e7"]]},{"id":"d21e5455.31c39","type":"comment","z":"be3a4531.4610c8","name":"Listen for Xiaomi sensors","info":"every msg type != cmd:report gets filtered out.","x":190,"y":420,"wires":[]}]

Flow Info

created 3 weeks, 4 days ago

Node Types

  • comment (x1)
  • debug (x1)
  • function (x1)
  • json (x1)
  • mqtt out (x1)
  • udp in (x1)


  • xiaomi
  • homeautomation
  • mqtt
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option