Python node passing data to mqtt/debug

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)


[{"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":"","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]


2/3/2020, 4:15:12 PM
node: payload
msg.payload : 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

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

test = 25
msg['payload'] = test
return msg
1 Like

not sure .... I've done

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

resulting in

2/3/2020, 4:35:33 PM
[node: luminosity]( : string[35]
"Traceback (most recent call last):↵"

2/3/2020, 4:35:33 PM
[node: luminosity]( : 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]( : string[42]
"Python Function process exited with code 1"

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

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

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

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

ah gotcha, that worked
I was blind :slight_smile:

thank you @afelix

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

import Adafruit_DHT

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

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?


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.

iam using
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?

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

1 Like

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

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

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 :wink:

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

How did you install Adafruit_DHT? Did you follow this:

Did you install it for both Python2 and 3?

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

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.

1 Like

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