# How do I get Node-red to show envirophat sensor values?

**URL:** <https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260>\
**Category:** General\
**Created:** [19 September 2021 21:34 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260 "2021-09-19T21:34:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Richard238](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/richard238/32/48307_2.png) [@Richard238](https://discourse.nodered.org/u/Richard238)\
**Post date:** [19 September 2021 21:34 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260/1 "2021-09-19T21:34:41Z")

</div>

Using - Raspberry Pi 3B with Pimoroni EviroPhat sensors.

I have envirophat working fine, sending data to Initial State, and would now like to try it on Node-Red.

Debug shows this:

```auto
19/09/2021, 16:22:18node: 618da7a0094594b6
msg.payload : string[0]
""

```

How do I get Node-red to show envirophat sensor values?  
I imagine it needs to be something along the lines of:

```auto
msg.payload = {
  "weather.temperature()": "weather"
};
return msg;

```

Which gives:

```auto
19/09/2021, 22:29:12[node: Test](http://192.168.1.41:1880/#)
msg.payload : Object
{ weather.temperature(): "weather" }

```

Thank you.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [19 September 2021 22:06 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260/2 "2021-09-19T22:06:30Z")

</div>

Do you have a Python program that displays the sensor readings, like [https://github.com/pimoroni/enviro-phat/blob/master/examples/all.py](https://github.com/pimoroni/enviro-phat/blob/master/examples/all.py)?

You could call it from an Exec node. Either reformat the output within Node-Red or rejig the python to output json something like this

```auto
## Output as JSON ---- import json
jsonData = json.dumps(OrderedDict([
    ("temp", temp),
    ("wind", wind),
    ("Pressure", pressure)
]))
print(jsonData)

```

---

<div class="post-metadata">

**Author:** ![Richard238](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/richard238/32/48307_2.png) [@Richard238](https://discourse.nodered.org/u/Richard238)\
**Post date:** [20 September 2021 11:07 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260/3 "2021-09-20T11:07:36Z")

</div>

Yes, there's a _script.py_ file in /home/pi/.node-red/node\_modules/node-red-contrib-envirophat

I'll have a go with the exec node and will doubtless be back for more advice.

Thank you.

---

<div class="post-metadata">

**Author:** ![Richard238](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/richard238/32/48307_2.png) [@Richard238](https://discourse.nodered.org/u/Richard238)\
**Post date:** [20 September 2021 11:44 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260/4 "2021-09-20T11:44:00Z")

</div>

Progress!

Exec node, command:

```auto
python /home/pi/.node-red/node_modules/node-red-contrib-envirophat/script.py

```

_(Timing changed in script.py from running every half second to every 15 seconds as there was simply too much data to keep up.)_

Output = `while the command is running - spawn mode`

All of which results in this:

```auto
20/09/2021, 12:32:31node: Script
msg.payload : string[206]
"'lightLevel': 17, 'lightG': 75, 'pressure': 101579.8757343893, 'motionZ': -0.10272216796875, 'motionY': -0.95782470703125, 'motionX': 0.00030517578125, 'heading': 285.13, 'temperature': 17.715479066109765}↵"

```

Aim now is to:

- Put gauges on a dashboard
- Get a Hue light to change colour depending on value of _motionY_

_motionY_ is the position of my 'up-n-over' garage door (that's a typical UK garage door.), so I know from the colour of a lamp if the garage door's been left open, is closed, or is ajar.

---

<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:** [20 September 2021 12:44 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260/5 "2021-09-20T12:44:03Z")

</div>

Are you sure there is not a `{` on the front? If there is then strip the return off the end, convert all the single quotes to double quotes and feed it into a JSON node If it is valid JSON then it will convert it to a Javascript object for you.

---

<div class="post-metadata">

**Author:** ![Richard238](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/richard238/32/48307_2.png) [@Richard238](https://discourse.nodered.org/u/Richard238)\
**Post date:** [20 September 2021 13:09 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260/6 "2021-09-20T13:09:01Z")

</div>

Yes, there is - bad copy/paste.

```auto
{'lightR': 102, 'lightB': 102, 'lightLevel': 5, 'lightG': 102, 'pressure': 101633.73653824191, 'motionZ': -0.09771728515625, 'motionY': -0.95843505859375, 'motionX': -0.01336669921875, 'heading': 280.28, 'temperature': 18.131538700984038}

```

> _convert all the single quotes to double quotes_  
> Done. Used _Change Function_ node and got the quote marks converted

```auto
: 72, "lightLevel": 21, "lightG": 72, "pressure": 101646.50291694587, "motionZ": -0.10333251953125, "motionY": -0.960693359375, "motionX": -0.0125732421875, "heading": 283.69, "temperature": 18.24269076022938}

```

Next task then...

> _...feed it into a JSON node_

Which is where I am now stuck :-/

---

<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:** [20 September 2021 13:54 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260/7 "2021-09-20T13:54:53Z")

</div>

> [@Richard238](#):
>
> Which is where I am now stuck

What is the problem?

---

<div class="post-metadata">

**Author:** ![Richard238](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/richard238/32/48307_2.png) [@Richard238](https://discourse.nodered.org/u/Richard238)\
**Post date:** [20 September 2021 14:01 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260/8 "2021-09-20T14:01:23Z")

</div>

I think I made it work, sort of a bit-ish.

```auto
{"lightR":109,"lightB":72,"lightLevel":21,"lightG":72,"pressure":101634.76374743499,"motionZ":-0.0994873046875,"motionY":-0.96136474609375,"motionX":-0.01177978515625,"heading":280.98,"temperature":18.542133950823427}

```

But more often, it'll show things like:

```auto
Unexpected end of JSON input
Unexpected token , in JSON at position 2

```

---

<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:** [20 September 2021 14:06 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260/9 "2021-09-20T14:06:43Z")

</div>

I suspect that python is splitting the input up into buffers, so sometimes you don't get the whole thing. Look at the one you posted above, it stars with `: 72,` which is not the start of a full set of data. Change the exec node to send the output when the command has finished.

---

<div class="post-metadata">

**Author:** ![Richard238](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/richard238/32/48307_2.png) [@Richard238](https://discourse.nodered.org/u/Richard238)\
**Post date:** [20 September 2021 14:15 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260/10 "2021-09-20T14:15:22Z")

</div>

Strange, now it puts nothing out to debug.  
The exec is working as the blue PID dot shows, and the PID number is changing.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [20 September 2021 14:15 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260/11 "2021-09-20T14:15:57Z")

</div>

I think the Python script continuously loops, so to do that you'll have to make it print once and exit, then call it from NR at intervals

---

<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:** [20 September 2021 14:17 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260/12 "2021-09-20T14:17:05Z")

</div>

If you run the python program from the command line does it output once and return to the prompt, or does it keep going?

If it returns to the prompt then add debug nodes to the other outputs of the exec and see if anything is there. Set them to output Complete Message.

---

<div class="post-metadata">

**Author:** ![Richard238](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/richard238/32/48307_2.png) [@Richard238](https://discourse.nodered.org/u/Richard238)\
**Post date:** [20 September 2021 14:35 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260/13 "2021-09-20T14:35:40Z")

</div>

It kept going, as expected.

I commented out the while true and the sleep, renamed it as script2.py, and it ran once.  
When I changed the exec node to reflect the .py name change, it failed on invalid indents.  
How can that be?!

---

<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:** [20 September 2021 14:38 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260/14 "2021-09-20T14:38:46Z")

</div>

> [@Richard238](#):
>
> How can that be?!

Because you have messed up the indents in the python.

---

<div class="post-metadata">

**Author:** ![Richard238](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/richard238/32/48307_2.png) [@Richard238](https://discourse.nodered.org/u/Richard238)\
**Post date:** [20 September 2021 14:43 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260/15 "2021-09-20T14:43:49Z")

</div>

Curse of my life.

But they're happy now.

Debug says

```auto
msg.payload : string[10]
{"code":0}

```

Now the string to object isn't working.

 ![Capture1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/1/71c65a83910e02f58b40b136a3dc672bd5f5ab7f.png)

---

<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:** [20 September 2021 15:15 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260/16 "2021-09-20T15:15:53Z")

</div>

You are on the wrong output of the exec node

---

<div class="post-metadata">

**Author:** ![Richard238](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/richard238/32/48307_2.png) [@Richard238](https://discourse.nodered.org/u/Richard238)\
**Post date:** [20 September 2021 15:49 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260/17 "2021-09-20T15:49:11Z")

</div>

Ooops, I didn't appreciate the importance of where to link the orange lines to.

```auto
{"lightR":109,"lightB":72,"lightLevel":21,"lightG":72,"pressure":101717.91099599742,"motionZ":-0.09661865234375,"motionY":-0.95880126953125,"motionX":-0.0096435546875,"heading":284.06,"temperature":18.786912876932547}

```

Now that's working, thank you. how to select one of those parameters to show on a gauge or chart . It seems to show several on one at the moment.

I _think_ I've figured this out.

---

<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:** [20 September 2021 16:16 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260/18 "2021-09-20T16:16:53Z")

</div>

In the gauge Output Format put  
`{{msg.payload.the_property_you_want}}`

---

<div class="post-metadata">

**Author:** ![Richard238](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/richard238/32/48307_2.png) [@Richard238](https://discourse.nodered.org/u/Richard238)\
**Post date:** [20 September 2021 16:20 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260/19 "2021-09-20T16:20:32Z")

</div>

Yes, that's it.

Except multiple items show a stationary gauge, and even when I disconnect  
the first one, it still works but the others don't. Most peculiar.

 ![Capture2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/6/16d24e0a8926c2fa339cbdba32e40c3fb2b56d49.png)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [20 September 2021 16:32 UTC](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260/20 "2021-09-20T16:32:34Z")

</div>

Post the output from the exec node (the data not a picture) and your flow so we can see what's going on.

[Next page](https://discourse.nodered.org/t/how-do-i-get-node-red-to-show-envirophat-sensor-values/51260.md?page=2)
