# Node-red Python exec

**URL:** <https://discourse.nodered.org/t/node-red-python-exec/39596>\
**Category:** Industrial\
**Created:** [23 January 2021 07:26 UTC](https://discourse.nodered.org/t/node-red-python-exec/39596 "2021-01-23T07:26:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amit2016-17](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/amit2016-17/32/35782_2.png) [@Amit2016-17](https://discourse.nodered.org/u/Amit2016-17)\
**Post date:** [23 January 2021 07:26 UTC](https://discourse.nodered.org/t/node-red-python-exec/39596/1 "2021-01-23T07:26:02Z")

</div>

I am trying to access dht11 temperature and humidity data using python program on raspberrypi 4b on to node-red 'exec' module , but it is unable to capture the python data and error shows rc:-2 ,

1/22/2021, 6:25:50 PM[node: 60d44db2.3b0944](http://xxxxxxxxxx:1880/#)msg : Object  
object payload: object  
topic: ""  
\_msgid: "6b5db153.10ae"

python program is working fine and displaying output in console.  
Temp: 64.4 F / 18.0 C Humidity: 68%  
Temp: 64.4 F / 18.0 C Humidity: 68%  
Temp: 64.4 F / 18.0 C Humidity: 68%  
Temp: 64.4 F / 18.0 C Humidity: 68%

Any one plz help how to access this data in nodered

---- Node-red -----------

```auto
[{"id":"98f0a244.a3302","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"832a19ea.93caa8","type":"exec","z":"98f0a244.a3302","command":"/home/pi/IOTstack/Adafruit_CircuitPython_DHT/sudo python3 dht_simpletest.py","addpay":true,"append":"","useSpawn":"true","timer":"15","oldrc":false,"name":"dht_simplest.py","x":400,"y":80,"wires":[["9ae44969.7d99a8"],["60d44db2.3b0944"],["60d44db2.3b0944"]]},{"id":"5bbe50.570a71b","type":"inject","z":"98f0a244.a3302","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":80,"wires":[["832a19ea.93caa8"]]},{"id":"f6b7bd75.0b361","type":"function","z":"98f0a244.a3302","name":"","func":"msg['mx:temperature_c'] = msg.payload;\nmsg['mx:humidity'] = msg.humidity;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is deployed\n\n","finalize":"","x":380,"y":240,"wires":[["3d731fff.e628"]]},{"id":"3d731fff.e628","type":"debug","z":"98f0a244.a3302","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"true","targetType":"full","statusVal":"","statusType":"msg","x":750,"y":240,"wires":[]},{"id":"9ae44969.7d99a8","type":"debug","z":"98f0a244.a3302","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":710,"y":60,"wires":[]},{"id":"60d44db2.3b0944","type":"debug","z":"98f0a244.a3302","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":710,"y":140,"wires":[]}]

```

[flow edited by a moderator]

---

<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:** [23 January 2021 09:51 UTC](https://discourse.nodered.org/t/node-red-python-exec/39596/2 "2021-01-23T09:51:25Z")

</div>

Hi, in order to make code more readable and importable it is important to surround your code with three backticks  
`````  
`like this`  
`````

You can edit and correct your post by clicking the pencil icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

Also add debug nodes to all outputs of the exec node, set them to show Complete Message and give them names so you can identify them in the debug pane. Probably there will be a useful error message in one of those.

---

<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:** [23 January 2021 11:00 UTC](https://discourse.nodered.org/t/node-red-python-exec/39596/3 "2021-01-23T11:00:57Z")

</div>

Why not use [node-red-contrib-dht-sensor](https://flows.nodered.org/node/node-red-contrib-dht-sensor)?

---

<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:** [23 January 2021 11:20 UTC](https://discourse.nodered.org/t/node-red-python-exec/39596/4 "2021-01-23T11:20:03Z")

</div>

how exactly are you running the python program in the exec node (and outside of Node-RED) - I suspect one of them may be using sudo .

---

<div class="post-metadata">

**Author:** ![Amit2016-17](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/amit2016-17/32/35782_2.png) [@Amit2016-17](https://discourse.nodered.org/u/Amit2016-17)\
**Post date:** [24 January 2021 12:54 UTC](https://discourse.nodered.org/t/node-red-python-exec/39596/5 "2021-01-24T12:54:08Z")

</div>

THis will not work as this is two year old and does not work with dht11

---

<div class="post-metadata">

**Author:** ![davidz](https://avatars.discourse-cdn.com/v4/letter/d/a88e57/32.png) [@davidz](https://discourse.nodered.org/u/davidz)\
**Post date:** [25 January 2021 16:31 UTC](https://discourse.nodered.org/t/node-red-python-exec/39596/6 "2021-01-25T16:31:32Z")

</div>

A quick (but not elegant) solution is to use your Python program to send data via MQTT to Node Red on the RPI 4b. Node Red has easy MQTT in to get the data. In this case, you can have the Python program run separately in the background.

Of course, you are wasting a little bit system resource by adding the extra MQTT service. Fortunately, MQTT is very light weight and does not add much overhead. And you don't need to use the 'exec' module in Node Red.
