Temperature date from xiaomi

Hi All,

I’m wanting to capture temperature and humidity data from a Xiaomi sensor. Here’s an example of the output filtered through a json node.

{“cmd”:“report”,
“model”:“sensor_ht”,
“sid”:“158d0001f4ecc4”,
“short_id”:1154,
“data”:"{“temperature”:“1639”}"}

If I convert msg.payload.data to msg.payload I get the following

{“temperature”:“1690”}

Is anyone able to assist me in pulling the number value out?

Have you tried msg.payload.data.temperature ?

Well that was a lot easier than I expected :slight_smile:

Thanks a lot.

if you haven’t found it there’s a great doc
https://nodered.org/docs/user-guide/messages
that explains how to use a debug node and the “tool tips” to copy the route of any data item

Thanks again for the help. It looks like this will be a little more complicated unfortunately. I’m guessing that msg.payload.data is being interpreted as a string rather than an object as msg.payload.data.temperature comes back as undefined. Either that or I’m doing something very wrong indeed. Here is a copy of the unaltered string below…

{“cmd”:“heartbeat”,“model”:“sensor_ht”,“sid”:“158d0001f4ecc4”,“short_id”:1154,“data”:"{“temperature”:“1866”,“humidity”:“7239”}"}

Again, any guidance on how I can pull the temperature data would be much appreciated.

Certainly the value of the data property is not a valid string. It would be necessary to escape the inside quotes with the character \ , what presumably it is not possible since you do not control how the sensor format the output.

If you throw the whole data structure in a JSON validator (https://jsonlint.com/) you will see that it is not a valid JSON string.

Summarizing: I guess the output of the sensor is not exactly like you have shown. Looks like the extra quotes in the data property value are resulting in troubles (you do not have a valid string neither a valid JSON data).

See in this link (https://github.com/cflurin/xiaomi-mqtt/blob/master/README.md) how a valid JSON looks like for this sensor:

payload:
{
  "cmd":"report",
  "model":"sensor_ht",
  "sid":"158d0001a2eb66",
  "short_id":30124,
  "data":{"voltage":3005,"temperature":16.7,"humidity":null}
}

PS: have a read on this topic to better format the text you copy and paste in the forum: How to share code or flow json

Indeed the original xiaomi data (msg.payload.data) is a string. Inxiaomi-mqtt the data is converted to JSON so you can directly access the temperature using msg.payload.data.temperature.

How are you capturing xiaomi sensor_ht?

Edit:

You need to convert the string data to JSON:

msg.payload.data = JSON.parse(msg.payload.data);

Example:

[{"id":"aa5504d2.b38fb8","type":"inject","z":"d0877ed4.9e0cc","name":"","topic":"","payload":"{ \t\"cmd\": \"report\", \t\"model\": \"sensor_ht\", \t\"sid\": \"158 d0001f4ecc4\",\t\"short_id\": 1154, \t\"data\": \"{\\\"temperature\\\":\\\"1639\\\"}\" }","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":4440,"wires":[["ba0be5c0.106318"]]},{"id":"3ee06a5d.510976","type":"debug","z":"d0877ed4.9e0cc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":710,"y":4440,"wires":[]},{"id":"ba0be5c0.106318","type":"json","z":"d0877ed4.9e0cc","name":"","property":"payload","action":"obj","pretty":false,"x":230,"y":4440,"wires":[["db0dfe00.76b3"]]},{"id":"db0dfe00.76b3","type":"function","z":"d0877ed4.9e0cc","name":"convert data","func":"msg.payload.data = JSON.parse(msg.payload.data);\n\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":4440,"wires":[["8cd21ab0.3d2048"]]},{"id":"8cd21ab0.3d2048","type":"change","z":"d0877ed4.9e0cc","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.data.temperature","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":4440,"wires":[["3ee06a5d.510976"]]}]

… and you have to device the temperature by 100 to get 16.39

Feed that into a debug node and post a screenshot here so we can see exactly what you have.

Hi All,

Thanks for your suggestions and advice. I managed to nut it out today.

I was using a UDP connection to collect data from the Xiaomi. Changing to the mi-devices nodes means that the data is now being presented properly.