# Raspberry Pi - Philips Hue light control with TSL2591 sensor

**URL:** <https://discourse.nodered.org/t/raspberry-pi-philips-hue-light-control-with-tsl2591-sensor/12811>\
**Category:** General\
**Created:** [2 July 2019 09:04 UTC](https://discourse.nodered.org/t/raspberry-pi-philips-hue-light-control-with-tsl2591-sensor/12811 "2019-07-02T09:04:03Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [9 July 2019 14:03 UTC](https://discourse.nodered.org/t/raspberry-pi-philips-hue-light-control-with-tsl2591-sensor/12811/21 "2019-07-09T14:03:49Z")

</div>

It’s a JSON string, so try using the JSON node to have it turn into an object. Then use a function node:

```auto
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.

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [9 July 2019 14:34 UTC](https://discourse.nodered.org/t/raspberry-pi-philips-hue-light-control-with-tsl2591-sensor/12811/22 "2019-07-09T14:34:26Z")

</div>

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.

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![MAaron](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maaron/32/9813_2.png) [@MAaron](https://discourse.nodered.org/u/MAaron)\
**Post date:** [9 July 2019 15:09 UTC](https://discourse.nodered.org/t/raspberry-pi-philips-hue-light-control-with-tsl2591-sensor/12811/23 "2019-07-09T15:09:25Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [9 July 2019 15:21 UTC](https://discourse.nodered.org/t/raspberry-pi-philips-hue-light-control-with-tsl2591-sensor/12811/24 "2019-07-09T15:21:04Z")

</div>

Can't you just read the GPIO output directly in node-red (no need for python).

---

<div class="post-metadata">

**Author:** ![MAaron](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maaron/32/9813_2.png) [@MAaron](https://discourse.nodered.org/u/MAaron)\
**Post date:** [9 July 2019 15:35 UTC](https://discourse.nodered.org/t/raspberry-pi-philips-hue-light-control-with-tsl2591-sensor/12811/25 "2019-07-09T15:35:49Z")

</div>

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.

```auto
global.set("LuxValue",msg.payload);

```

So now, the flow will only obtain the value through a function node

```auto
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 🙂

---

<div class="post-metadata">

**Author:** ![Ahmedgmm](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ahmedgmm/32/75817_2.png) [@Ahmedgmm](https://discourse.nodered.org/u/Ahmedgmm)\
**Post date:** [9 February 2023 12:47 UTC](https://discourse.nodered.org/t/raspberry-pi-philips-hue-light-control-with-tsl2591-sensor/12811/26 "2023-02-09T12:47:35Z")

</div>

MAaron hi

can you please share the code and steps on ow you gut the TSL2591 running .. i cant fid documentation anywhere.

Br,

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [26 February 2023 03:45 UTC](https://discourse.nodered.org/t/raspberry-pi-philips-hue-light-control-with-tsl2591-sensor/12811/27 "2023-02-26T03:45:17Z")

</div>

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.

[Previous page](https://discourse.nodered.org/t/raspberry-pi-philips-hue-light-control-with-tsl2591-sensor/12811.md?page=1)
