# Python node passing data to mqtt/debug

**URL:** <https://discourse.nodered.org/t/python-node-passing-data-to-mqtt-debug/21203>\
**Category:** General\
**Created:** [3 February 2020 15:20 UTC](https://discourse.nodered.org/t/python-node-passing-data-to-mqtt-debug/21203 "2020-02-03T15:20:30Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![krizniq](https://avatars.discourse-cdn.com/v4/letter/k/ec9cab/32.png) [@krizniq](https://discourse.nodered.org/u/krizniq)\
**Post date:** [3 February 2020 15:20 UTC](https://discourse.nodered.org/t/python-node-passing-data-to-mqtt-debug/21203/1 "2020-02-03T15:20:30Z")

</div>

Hello, I'm trying to use python function which will be triggered each x minutes ... (mainly because of learning experience, as I have it crontabed anyways)  
I'm using **node-red-contrib-python-function** but I'm having difficulties to understand how python node passing data

It seems it's passing them, but not in msg.payload as it looks from this flow and debug  
I have two debugs, one for payload and one for whole object.  
In object there is a number

In msg.payload there is nothing, and mqtt node complains about something strange.

Any hint would be really appreciated.

(as for test, there are two outputs just for convenient testing, this function reads luminosity sensor and resulting in number)

flow

```auto
[{"id":"1bdd464f.bab3fa","type":"python-function","z":"941ada45.f00b08","name":"luminosity","func":"import smbus\nimport time\n\n# Get I2C bus\nbus = smbus.SMBus(1)\n\nbus.write_byte_data(0x39, 0x00 | 0x80, 0x03)\nbus.write_byte_data(0x39, 0x01 | 0x80, 0x02)\n\ntime.sleep(0.5)\ndata = bus.read_i2c_block_data(0x39, 0x0C | 0x80, 2)\ndata1 = bus.read_i2c_block_data(0x39, 0x0E | 0x80, 2)\n\n# Convert the data\nch0 = data[1] * 256 + data[0]\nch1 = data1[1] * 256 + data1[0]\n\nmsg = ch0-ch1\n\ntest = 23\n\nreturn [msg, test]","outputs":2,"x":420,"y":120,"wires":[["68e6ee9c.2dc9e","67728ffb.a4ff6"],["f9ccf07f.3b6e6"]]},{"id":"68e6ee9c.2dc9e","type":"debug","z":"941ada45.f00b08","name":"payload","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":680,"y":180,"wires":[]},{"id":"ebd80287.b79b9","type":"inject","z":"941ada45.f00b08","name":"3 min","topic":"","payload":"true","payloadType":"bool","repeat":"180","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":120,"wires":[["1bdd464f.bab3fa"]]},{"id":"67728ffb.a4ff6","type":"mqtt out","z":"941ada45.f00b08","name":"luminosity","topic":"homew/weather/light","qos":"0","retain":"false","broker":"df8d19b3.781d88","x":700,"y":100,"wires":[]},{"id":"f9ccf07f.3b6e6","type":"debug","z":"941ada45.f00b08","name":"msg object","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":690,"y":220,"wires":[]},{"id":"df8d19b3.781d88","type":"mqtt-broker","z":"","name":"MQTT","broker":"192.168.0.25","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

debug:

```auto
2/3/2020, 4:15:12 PM
node: payload
msg.payload : undefined
undefined

2/3/2020, 4:15:12 PM
node: luminosity
msg : error
"TypeError: Cannot create property 'qos' on number '43'"

2/3/2020, 4:15:12 PM
node: msg object
msg : number
23

```

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [3 February 2020 15:30 UTC](https://discourse.nodered.org/t/python-node-passing-data-to-mqtt-debug/21203/2 "2020-02-03T15:30:45Z")

</div>

What you are trying to send/return are not objects, you have to return messages like this:

```auto
test = 25
msg['payload'] = test
return msg

```

---

<div class="post-metadata">

**Author:** ![krizniq](https://avatars.discourse-cdn.com/v4/letter/k/ec9cab/32.png) [@krizniq](https://discourse.nodered.org/u/krizniq)\
**Post date:** [3 February 2020 15:36 UTC](https://discourse.nodered.org/t/python-node-passing-data-to-mqtt-debug/21203/3 "2020-02-03T15:36:30Z")

</div>

not sure .... I've done

```auto
msg = ch0-ch1
msg['payload'] = msg
return msg

```

resulting in

```auto
2/3/2020, 4:35:33 PM
[node: luminosity](http://10.10.1.55:1880/#)msg : string[35]
"Traceback (most recent call last):↵"

2/3/2020, 4:35:33 PM
[node: luminosity](http://10.10.1.55:1880/#)msg : string[144]
" File "<string>", line 90, in <module>↵ File "<string>", line 80, in python_function↵TypeError: 'int' object does not support item assignment↵"

2/3/2020, 4:35:33 PM
[node: luminosity](http://10.10.1.55:1880/#)msg : string[42]
"Python Function process exited with code 1"

```

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [3 February 2020 15:39 UTC](https://discourse.nodered.org/t/python-node-passing-data-to-mqtt-debug/21203/4 "2020-02-03T15:39:36Z")

</div>

Why are you editing `msg` like that? `ch0-ch1` will give a number as value, and thus replacing the existing message. The next line, `msg` will be the number rather than the dictionary (Python)/object (JavaScript) it was before.

Try it as

```python
msg['payload'] = ch0 - ch1
return msg

```

or, if you prefer to set the payload as temporary value:

```python
tmp = ch0 - ch1
msg['payload'] = tmp
return msg

```

---

<div class="post-metadata">

**Author:** ![krizniq](https://avatars.discourse-cdn.com/v4/letter/k/ec9cab/32.png) [@krizniq](https://discourse.nodered.org/u/krizniq)\
**Post date:** [3 February 2020 15:41 UTC](https://discourse.nodered.org/t/python-node-passing-data-to-mqtt-debug/21203/5 "2020-02-03T15:41:30Z")

</div>

ah gotcha, that worked  
I was blind 🙂

thank you @afelix

---

<div class="post-metadata">

**Author:** ![krizniq](https://avatars.discourse-cdn.com/v4/letter/k/ec9cab/32.png) [@krizniq](https://discourse.nodered.org/u/krizniq)\
**Post date:** [3 February 2020 17:51 UTC](https://discourse.nodered.org/t/python-node-passing-data-to-mqtt-debug/21203/6 "2020-02-03T17:51:19Z")

</div>

one more question  
when I try to run this inside python function

```auto
import Adafruit_DHT

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 22

humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

if humidity is not None and temperature is not None:
    temperature['payload'] = "{0:0.1f}".format(temperature)
    humidity['payload'] = "{0:0.1f}".format(humidity)

return [temperature, humidity]

```

I do get error **No module named Adafruit\_DHT**  
Firstly I thought that module is installed only for root and thus not working, so i've tried to install it for nodered instead, but module is installed for all users in system already

```auto
Requirement already satisfied: Adafruit_DHT in /usr/local/lib/python3.7/dist-packages (1.4.0)

```

Is there anything needed to update/install for nodered python node in order to be able to use installed modules? or some kind of debug?

Thanks!

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [3 February 2020 17:55 UTC](https://discourse.nodered.org/t/python-node-passing-data-to-mqtt-debug/21203/7 "2020-02-03T17:55:10Z")

</div>

Are you using python-function or python-function3? The first uses python2 internally, the other python3. As you can see from the path, the library you installed is python3.

---

<div class="post-metadata">

**Author:** ![krizniq](https://avatars.discourse-cdn.com/v4/letter/k/ec9cab/32.png) [@krizniq](https://discourse.nodered.org/u/krizniq)\
**Post date:** [3 February 2020 17:57 UTC](https://discourse.nodered.org/t/python-node-passing-data-to-mqtt-debug/21203/8 "2020-02-03T17:57:02Z")

</div>

iam using  
node-red-contrib-python-function  
I'm aware of node-red-contrib-python3-function but it seemed unmaintained and in the description of the  
node-red-contrib-python-function is:  
**Requires Python (both 2.x and 3.x are supported) installed in the system.**

So shall I assume it's not using python3?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [3 February 2020 18:16 UTC](https://discourse.nodered.org/t/python-node-passing-data-to-mqtt-debug/21203/9 "2020-02-03T18:16:05Z")

</div>

Why go to all that work to read the dht22? why not use node-red-contrib-dht-sensor??

---

<div class="post-metadata">

**Author:** ![krizniq](https://avatars.discourse-cdn.com/v4/letter/k/ec9cab/32.png) [@krizniq](https://discourse.nodered.org/u/krizniq)\
**Post date:** [3 February 2020 18:39 UTC](https://discourse.nodered.org/t/python-node-passing-data-to-mqtt-debug/21203/10 "2020-02-03T18:39:03Z")

</div>

cos I'm not aware of that?  
will take a look, thanks 😉

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [3 February 2020 19:07 UTC](https://discourse.nodered.org/t/python-node-passing-data-to-mqtt-debug/21203/11 "2020-02-03T19:07:39Z")

</div>

This might be a bit off topic - so forgive if it is.

I assume you have the DHT-22 connected to a Raspberry Pi ??

You might want to consider using a Wemos D1 Mini to manage the sensor - they are very inexpensive and have loads of drivers built-in (if you use ESP-Easy or Tasmota). This would mean you could locate the Wemos and the DHT-22 where you want to measure the temperature and get the Wemos to transfer the data via MQTT to your Raspberry Pi and process the data in Node-RED.

Just a thought for you.

Regards, David

---

<div class="post-metadata">

**Author:** ![krizniq](https://avatars.discourse-cdn.com/v4/letter/k/ec9cab/32.png) [@krizniq](https://discourse.nodered.org/u/krizniq)\
**Post date:** [3 February 2020 19:09 UTC](https://discourse.nodered.org/t/python-node-passing-data-to-mqtt-debug/21203/12 "2020-02-03T19:09:41Z")

</div>

i do have many wemoses and many sensors everywhere.  
this instalation is on rpi not only managing dht but quite a lot of other stuff as well 😉

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [3 February 2020 19:10 UTC](https://discourse.nodered.org/t/python-node-passing-data-to-mqtt-debug/21203/13 "2020-02-03T19:10:51Z")

</div>

Oh - ok, it was just a thought to try to make life a bit simpler.  
Have fun.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [4 February 2020 09:10 UTC](https://discourse.nodered.org/t/python-node-passing-data-to-mqtt-debug/21203/14 "2020-02-04T09:10:08Z")

</div>

How did you install Adafruit\_DHT? Did you follow this:

> **[adafruit/Adafruit\_Python\_DHT](https://github.com/adafruit/Adafruit_Python_DHT)**
>
> Python library to read the DHT series of humidity and temperature sensors on a Raspberry Pi or Beaglebone Black. - adafruit/Adafruit\_Python\_DHT

Did you install it for both Python2 and 3?

Do you see this library is deprecated? You are recommended to use CircuitPython

> **[DHT Humidity Sensing on Raspberry Pi or Beaglebone Black with GDocs Logging](https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/python-setup)**
>
> In this tutorial we'll be showing how to utilize a DHT sensor Python library based on C for high-speed GPIO polling to handle bit-banged sensor output. Many low cost sensors have unusual output formats, and in this case, a "Manchester-esque" output...

---

<div class="post-metadata">

**Author:** ![krizniq](https://avatars.discourse-cdn.com/v4/letter/k/ec9cab/32.png) [@krizniq](https://discourse.nodered.org/u/krizniq)\
**Post date:** [4 February 2020 15:59 UTC](https://discourse.nodered.org/t/python-node-passing-data-to-mqtt-debug/21203/15 "2020-02-04T15:59:32Z")

</div>

honestly I dont like circuitpython at all... idn I'm simple guy, why to have another derivate-language when python is just fine.

anyway dht node works perfectly so thank you guys for pointing me that direction.

---

<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:** [18 February 2020 15:59 UTC](https://discourse.nodered.org/t/python-node-passing-data-to-mqtt-debug/21203/16 "2020-02-18T15:59:33Z")

</div>

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