How to analyze data from Xiaomi gateway via xiaomi-mqtt bridge

Hi,
today I managed to run Xiaomi Gateway with xiaomi-mqtt bridge.

I use temperature / humidity / pressure sensor.

The data I get comes for each value one after the other (for temperature, for humidity, for pressure), so three messages (object).

I use the gauge to display temperature value.

When the first message arrives, value on gauge is ok.
However, when other messages arrive, the display goes to zero because the value for temperature is "msg.payload.data.temperature = null".

I managed to solve this in function with:

var sid = msg.payload.sid;
var tmp = msg.payload.data.temperature;

if (sid == "158a" & !isNaN(tmp))
{
    msg.temp1 = tmp / 100;
    return msg;
}

Is this the right solution?
Is there a better or simpler solution?

Debug screenshoot:

I expect you can use a Switch node to send the different messages to their destinations. If you can't see how to do it then show us what you get in a debug node for the different messages so we can fully understand the problem. Most of us are not familiar with the Xiamoi nodes.

Can you show debug output with "complete msg object" selected?

There may be other info in the msg (like a topic) that differentiates the data.

If so, then you could use a switch node to split the values out to 3 outputs, one of which will be the one you're interested in - then feed that single output to your gauge or chart.

Are you using GitHub - cflurin/xiaomi-mqtt: A Bridge between the Xiaomi Mi Smart Home Gateway and the Mqtt broker. ?

Yes, I used that (yours).
I've tried all available nodes from the palette manager but none is working.
Is it possible to publish it in the manger palette?
The instruction is quite correct, except that in "config.json" should use the "ID" for gateway settings instead of "password".
I installed it on raspberry pi.
Xiaomi Gateway is the latest version (with logo; lumi.gateway.v3), and firmware is 1.4.1_167.0158.
Thanks for the excellent job done!

No, xiaomi-mqtt isn't a node-red-contrib-node but an external "app" like homebridge-mqtt.

Concerning your first question:
If you don't want to use a function-node, you can use the switch-node.

Actually I use several switch-nodes to filter:

  • sid
  • cmd
  • data.temperature

Note:
I see that you are using a model (weather.v1) that I didn't test.
Could you please post more infos (picture) about your device?

Debug before function:

Debug after function vith NaN (working correctly):

simulation%20with%20NaN

Debug ater function without NaN(reset gauge, becouse payload:NaN)

Simulation flow:

[{"id":"8536e33b.3185","type":"tab","label":"simulation","disabled":false,"info":""},{"id":"779d214.49cdbe","type":"function","z":"8536e33b.3185","name":"analyze","func":"\nvar sid=msg.payload.sid;\nvar tmp=msg.payload.data.temperature;\nif (sid===\"158a\")\n{\n\nmsg.temp1=tmp/100;\nreturn msg}","outputs":1,"noerr":0,"x":660,"y":320,"wires":[["bc95db1f.e96768","a188cd3.fb0c53"]]},{"id":"5ac59fad.5c771","type":"inject","z":"8536e33b.3185","name":"inject temperature value","topic":"","payload":"{\"sid\":\"158a\",\"data\":{\"temperature\":2544}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":450,"y":280,"wires":[["779d214.49cdbe"]]},{"id":"395fb0ca.8e47c","type":"inject","z":"8536e33b.3185","name":"inject humidity value","topic":"","payload":"{\"sid\":\"158a\",\"data\":{\"humidity\":101850}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":460,"y":360,"wires":[["779d214.49cdbe"]]},{"id":"a188cd3.fb0c53","type":"debug","z":"8536e33b.3185","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":950,"y":320,"wires":[]},{"id":"bc95db1f.e96768","type":"ui_gauge","z":"8536e33b.3185","name":"","group":"d314cc9d.55b41","order":6,"width":0,"height":0,"gtype":"gage","title":"test_temp","label":"units","format":"{{msg.temp1}}","min":0,"max":"40","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":970,"y":280,"wires":[]},{"id":"d314cc9d.55b41","type":"ui_group","z":"","name":"Default","tab":"e54ff8dd.4c97a8","disp":true,"width":"6","collapse":false},{"id":"e54ff8dd.4c97a8","type":"ui_tab","z":"","name":"simulation","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Debug from simulation flow:

simulation_debug

I see that is not a node, just asking is it possible to make node based on this. This is working great!
I am not programmer...:frowning:

I have AQARA temperature/humidity/pressure sensor.
Also I have AQARA motion sensor, i see data but did not make any test.

Here are some pictures:

temp_hum_front



Based on the debug output screen shots, I think using a function node as you have is the most concise. I say that as all messages have the same topic name and I see nothing that clearly separates the messages other than one of them has a object in the payload.

One improvement I would suggest is that you test the payload is an object with a data member before trying to access it's temperature property (to avoid errors) e.g...

var isTemp = (msg.payload && msg.payload.data && msg.payload.data.temperature);
if(!isTemp){
    return null;//halt flow - this is not the droid we are looking for
}
var sid = msg.payload.sid;
var tmp = msg.payload.data.temperature;

if (sid == "158a" & !isNaN(tmp))
{
    msg.temp1 = tmp / 100;
    return msg;
}
1 Like

You might also try the topic_mode full (a PR from an user).

https://github.com/cflurin/xiaomi-mqtt/wiki/How-to-benefit-of-the-full-topic-option

I've first tried with the build-in nodes without success. I wasn't able to use the udp-in and udp-out at the same time with the same config and I didn't want to build another xiaomi-node.

Anyway this way it can also be used with others platform like ESP8266.