# BMP180 sensor to MQTT get multiple values

**URL:** <https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528>\
**Category:** General\
**Created:** [23 March 2020 18:43 UTC](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528 "2020-03-23T18:43:08Z")\
**Posts on this page:** 19\
**Page:** 2

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [25 March 2020 16:47 UTC](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528/21 "2020-03-25T16:47:37Z")

</div>

Still trying to get this working. Can you please advise...from my reading, node red is expecting Java Scrip in the received MQTT message, correct? If this is true, I am struggling to find a method within Python to translate the data from the sensors into a properly formatted Java Script package/message. Thx

---

<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:** [25 March 2020 17:54 UTC](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528/22 "2020-03-25T17:54:29Z")

</div>

As I said I don't know python. It is not javascript you are trying to generate but JSON, which is nothing directly to do with javascript. If the two values in your variables are, for example, 21.2 and 63.5 then the message you need to send to mqtt is a string containing  
`{"temp": 21.2, "hum": 63.5}`  
Googling for "insert variable into string in python" seems to yield an number of suggestions. One complication may be getting the double quotes in which might add a complication. Again googling for "python double quotes in string" looks likely to be useful.

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [25 March 2020 20:11 UTC](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528/23 "2020-03-25T20:11:45Z")

</div>

Colin, Thank you...JSON it is. I have done the exact search you describe but every time I get the information over to the MQTT listener in node red, it is still a string. I am able to get the numbers from the sensors (variables) into the sent message via methods found...but only as a string and not objects. Thus the issue is not capturing and sending the variables from Python, but rather how node red MQTT sees that data.

---

<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:** [25 March 2020 20:32 UTC](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528/24 "2020-03-25T20:32:55Z")

</div>

What string are you getting out of the MQTT In node?

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [25 March 2020 21:14 UTC](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528/25 "2020-03-25T21:14:25Z")

</div>

The third line in the shot below is the msgs variable from Python generated by: client.publish("bmp\_temp",msgs);

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/c/0cafa186f0c53ff72e79af99d8f95541b5738406.png)

When this hits the node red MQTT, below is the error received:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/8/387aa3541a979a50185fc5dc9eb15ea0460296df.png)

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [25 March 2020 21:19 UTC](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528/26 "2020-03-25T21:19:59Z")

</div>

Even getting the required double quotes into the string and the colon via:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/8/387aa3541a979a50185fc5dc9eb15ea0460296df.png)

Which gives the output of:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/1/0185f0deeb5c706b28736f25d7a0e5bf068d0465.png)

Still results in the same "Failed to parse JSON string error

---

<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:** [25 March 2020 21:26 UTC](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528/27 "2020-03-25T21:26:37Z")

</div>

That says it is not getting a valid json string, obviously, so change the Output dropdown in the MQTT In node to Auto Detect so you can see the raw data it is receiving. What does it show then?

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [25 March 2020 21:26 UTC](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528/28 "2020-03-25T21:26:55Z")

</div>

Dooh, never mind...forgot the comma, this in Python works (for others that may have a similar issue)  
msgs = "{{"temp:": {} ,"pres:"{}}}"..format(temperature, pressure)

Succes:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/3/03417b58cc686d1ee23bdb677f0d378368a23303.png)

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [25 March 2020 21:28 UTC](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528/29 "2020-03-25T21:28:23Z")

</div>

A sincere thank you Colin!! 💯 Hopefully I will be able to offer someone assistance in the future. I truly am loving node red and will sing its praises!

---

<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:** [25 March 2020 21:30 UTC](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528/30 "2020-03-25T21:30:31Z")

</div>

> [@Tomadoggy](#):
>
> msgs = "{{"temp:": {} ,"pres:"{}}}"..format(temperature, pressure)

You have too many colons, take out the ones extra ones inside the double quotes, so  
`msgs = "{{"temp": {} ,"pres"{}}}"`

As you have it at the moment you have attributes called temp: and pres: whereas you want them called temp and pres.

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [25 March 2020 21:35 UTC](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528/31 "2020-03-25T21:35:19Z")

</div>

Minor change to eliminate the double colon

msgs = "{{"temp": {} ,"pres" :{}}}".format(temperature, pressure)

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [25 March 2020 23:48 UTC](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528/32 "2020-03-25T23:48:12Z")

</div>

May I extend the question a bit? Now that I have the 2 objects coming in via MQTT, I need to execute a function on the temp object to do a conversion. Multiple experiments have failed. Ideas on how I can consume the temp object coming from MQTT in the function node to execute a math operation against it? Currently have:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/8/78010e42c45876f90a274b2bbcbb08c1dd98e463.png)

Which results in the following error:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/ca30176bd447d8252d2ba9b990ea5dd453912aba.png)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [26 March 2020 00:02 UTC](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528/33 "2020-03-26T00:02:03Z")

</div>

It’s arrives as msg. So you need msg.payload.temp

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [26 March 2020 00:07 UTC](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528/34 "2020-03-26T00:07:00Z")

</div>

Thank you for the quick reply, however when attempting the following:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/7/776158a77c9e620ad35c9004195b79e8bd811e1e.png)  
I get the error:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/7/e7b0d0d0ad093e6ed04030166e5dcfc01756caf1.png)

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [26 March 2020 00:16 UTC](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528/35 "2020-03-26T00:16:25Z")

</div>

actually I believe this is the correct error, attempting to resolve  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/3/530b2eefc884c43ca211923ccd7b35de3b1ad095.png)

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [26 March 2020 01:28 UTC](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528/36 "2020-03-26T01:28:05Z")

</div>

OK, so there may be a more elegant way, but what is working for me is to use the change node to set the payload to a number prior to doing the conversion by using the following:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/4/74864a3ec78bc92ab8df367ceb04000cc5b2ff49.png)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [26 March 2020 07:39 UTC](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528/37 "2020-03-26T07:39:45Z")

</div>

It comes in as msg AND you need to return msg, (not return msg.payload)  
But your jsonata version is just fine.

---

<div class="post-metadata">

**Author:** ![BobD](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobd/32/16663_2.png) [@BobD](https://discourse.nodered.org/u/BobD)\
**Post date:** [27 March 2020 22:09 UTC](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528/38 "2020-03-27T22:09:45Z")

</div>

I use MQTT to broker a similar message from a basic DHT sensor. It arrives into Node-Red as a msg.payload (which is just a string based message). I simply put that through a CSV node to make it into a buffer as such, by adding the "temp" and "humidity" to the msg. The gauges can then extract what they need from the msg.payload directly, as msg.payload.temp and msg.payload.humidity. It keeps the MQTT message size to a minimum.

msg = (b'{0:3.1f},{1:3.1f}'.format(t,h))  
client.publish(TOPIC, msg)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [10 April 2020 22:09 UTC](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528/39 "2020-04-10T22:09:50Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.

[Previous page](https://discourse.nodered.org/t/bmp180-sensor-to-mqtt-get-multiple-values/23528.md?page=1)
