# Temperature date from xiaomi

**URL:** <https://discourse.nodered.org/t/temperature-date-from-xiaomi/465>\
**Category:** General\
**Created:** [29 May 2018 08:30 UTC](https://discourse.nodered.org/t/temperature-date-from-xiaomi/465 "2018-05-29T08:30:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erbachm](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/erbachm/32/977_2.png) [@Erbachm](https://discourse.nodered.org/u/Erbachm)\
**Post date:** [29 May 2018 08:30 UTC](https://discourse.nodered.org/t/temperature-date-from-xiaomi/465/1 "2018-05-29T08:30:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [29 May 2018 08:39 UTC](https://discourse.nodered.org/t/temperature-date-from-xiaomi/465/2 "2018-05-29T08:39:52Z")

</div>

Have you tried msg.payload.data.temperature ?

---

<div class="post-metadata">

**Author:** ![Erbachm](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/erbachm/32/977_2.png) [@Erbachm](https://discourse.nodered.org/u/Erbachm)\
**Post date:** [29 May 2018 09:02 UTC](https://discourse.nodered.org/t/temperature-date-from-xiaomi/465/3 "2018-05-29T09:02:16Z")

</div>

Well that was a lot easier than I expected 🙂

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [29 May 2018 09:10 UTC](https://discourse.nodered.org/t/temperature-date-from-xiaomi/465/4 "2018-05-29T09:10:03Z")

</div>

if you haven’t found it there’s a great doc  
[https://nodered.org/docs/user-guide/messages](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

---

<div class="post-metadata">

**Author:** ![Erbachm](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/erbachm/32/977_2.png) [@Erbachm](https://discourse.nodered.org/u/Erbachm)\
**Post date:** [3 June 2018 00:37 UTC](https://discourse.nodered.org/t/temperature-date-from-xiaomi/465/5 "2018-06-03T00:37:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [3 June 2018 02:12 UTC](https://discourse.nodered.org/t/temperature-date-from-xiaomi/465/6 "2018-06-03T02:12:22Z")

</div>

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/](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](https://github.com/cflurin/xiaomi-mqtt/blob/master/README.md)) how a valid JSON looks like for this sensor:

```auto
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](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [3 June 2018 04:09 UTC](https://discourse.nodered.org/t/temperature-date-from-xiaomi/465/7 "2018-06-03T04:09:28Z")

</div>

Indeed the original xiaomi data (msg.payload.data) is a string. In`xiaomi-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:

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

```

Example:

```auto
[{"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`

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 June 2018 07:29 UTC](https://discourse.nodered.org/t/temperature-date-from-xiaomi/465/8 "2018-06-03T07:29:17Z")

</div>

> [@Erbachm](#):
>
> {“cmd”:“heartbeat”,“model”:“sensor\_ht”,“sid”:“158d0001f4ecc4”,“short\_id”:1154,“data”:"{“temperature”:“1866”,“humidity”:“7239”}"}

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

---

<div class="post-metadata">

**Author:** ![Erbachm](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/erbachm/32/977_2.png) [@Erbachm](https://discourse.nodered.org/u/Erbachm)\
**Post date:** [3 June 2018 11:29 UTC](https://discourse.nodered.org/t/temperature-date-from-xiaomi/465/9 "2018-06-03T11:29:51Z")

</div>

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.
