It’s a JSON string, so try using the JSON node to have it turn into an object. Then use a function node:
msg.payload = msg.payload.lux;
return msg;
However since you mentioned python shell, could you post the script you used here? It might be possible to optimise it to ease this part.
If you want to do light measurement directly in node-red I am doing it with a BH1750 lux sensor and reading it with node-red-contrib-i2c followed by a change node to calculate the lux value.
[{"id":"f9655f1b.ef3c1","type":"i2c in","z":"9dcf23c2.6c9ca","name":"Read BH1750","address":"35","command":"16","count":"2","x":300,"y":20,"wires":[["69f7be89.9e122"]]},{"id":"69f7be89.9e122","type":"change","z":"9dcf23c2.6c9ca","name":"Lux","rules":[{"t":"set","p":"lux","pt":"msg","to":"$round((payload.\"1\" + (256 * payload.\"0\"))/1.2,2)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":60,"wires":[["5714a6d3.d06238","a7aae33a.77a05"]]}]
I actually got it working, thx guys!
Only 1 more problem...whenever this pythonshell starts, if I deploy and start the flow, the script itself is set to time.sleep(4), and triggers the flow for every 4 secs.
How do I overwrite the code in python so it runs only once I make an injection in nodered?
the code is:
rom python_tsl2591 import tsl2591
import time
if __name__ == '__main__':
tsl = tsl2591() # initialize
while True:
print(tsl.get_current())
time.sleep(4)
This is a problem, because I have a starting loop thats based upon a joao join command, which basically means that when I'm home, it sends a command to start the flow, when I leave, it should stop the whole flow, which it does, but since the pythonscript goes by itself near the end of the flow, it triggers the lights again
Can't you just read the GPIO output directly in node-red (no need for python).
Well I haven't found any other code besides this python for the TSL2591, BUT I've gotten around this.
I put the reading python script alltogether OUT of the flow (python script reading data every 4 secs, gets only the lux, converts the lux string to number), and made a GLOBAL VARIABLE out of the read lux number.
global.set("LuxValue",msg.payload);
So now, the flow will only obtain the value through a function node
var ObtainedData = global.get("LuxValue")
msg.payload = ObtainedData;
return msg;
if the flow is running, if I stop it, the python script of course still checks the readings every 4 secs, BUT the lights won't change
MAaron hi
can you please share the code and steps on ow you gut the TSL2591 running .. i cant fid documentation anywhere.
Br,
You could also use the i2c node support to read the sensor results. If you search this forum I posted an example of how to read i2c based sensors directly with the i2c java node module in NR.