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

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:

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:

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

Which gives:

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

Thank you.

Do you have a Python program that displays the sensor readings, like 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

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

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.

Progress!

Exec node, command:

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:

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.

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.

Yes, there is - bad copy/paste.

{'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

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

What is the problem?

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

{"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:

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

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.

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

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

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.

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?!

Because you have messed up the indents in the python.

Curse of my life.

But they're happy now.

Debug says

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

Now the string to object isn't working.

You are on the wrong output of the exec node

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

{"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.

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

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.

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