LIFX Developer Zone

Dynamically changing brightness while Python script running

I have created a basic script in Python using the LifxLAN library. This simply cycles the color of my lights. The script uses a for loop to set the colors saved in a list using set_color(color, [duration], [rapid]). I run this script in the background through the terminal on a Mac.

Sometimes, I would like to override on-the-fly the original brightness levels. I can in fact do this by running Python in the terminal (while the script runs in the background) and setting the desired brightness level using set_brightness(brightness, [duration], [rapid]) .

The problem with this approach is that in the next iteration of the for loop the brightness reverts back to the value specified originally. Is there a way to prevent this from happening?

I am asking because if I set the brightness for a bulb (or group of bulbs) in the LIFX app on iOS and then animate a theme, if I change the brightness again while the animation is running this does not revert back to the one specified originally.

How are you specifying the color in your for loop? If you’re using HSBK values, just don’t send a brightness. Your loop will keep cycling colours, but won’t change their brightness.

If you provide a Gist of the script (without identifiers like MAC addresses), then it may be easier to assist.

Hello,

You need to send a SetWaveformOptional with the set_brightness property set to 0 so that the brightness of the device doesn’t change.

I’m unfamiliar with how to do that with LIFXLan.

But it’s fairly easy with Photons.

If you have python3.6 or above installed, copy cycle.py somewhere

python3 -m venv .photons-core
./.photons-core/bin/pip install lifx-photons-core
./.photons-core/bin/python cycle.py

I have actually managed to do this using your advice. More specifically, I used the individual functions:

set_hue(hue, [duration], [rapid])
set_brightness(brightness, [duration], [rapid])
set_saturation(saturation, [duration], [rapid])
set_colortemp(kelvin, [duration], [rapid])

The problem now though is that when I specify a duration, the whole thing gets messed up.

Example:

colors = [[0, 65535, 65535, 0], [21845, 65535, 65535, 0], [43690, 65535, 65535, 0]]

for c in colors:
    bulb.set_hue(c[0], 5000, 0)
    bulb.set_saturation(c[1], 5000, 0)
    time.sleep(10)

Here the colors do not change correctly. The loop only works correctly if duration is set to zero.

Curiously, if I use set_color(color, [duration], [rapid]) instead of the individual functions, the loop also works correctly when I specify a duration.

Any ideas?

The problem in this case is that the durations conflict. I have now modified the loop accordingly but this is not an ideal solution because the hue transition must first finish and then the saturation transition to start. Apparently the way to go is WaveForms but these are not fully implemented in LifxLAN.

Is there a way to run both set_hue() and set_saturation() simultaneously? With threading perhaps?

I am not familiar with Photons but will certainly have a look at it, thank you.

That said, I have a related question - does a bulb or group of bulbs take into account duration if rapid (i.e. fast transition) is also specified? or the duration value is simply ignored and the bulb (or group) transitions fast?

Although I am asking in the context of my python script, the question is also relevant in the context of the LIFX app where one can specify this in the animate effect.

I believe rapid just means it won’t wait for the device to reply to the message you sent, so it’s unrelated to the duration of the change.