Changing colorlight brightness via script


I’m working with openhab 3 on a raspberry pi 3 and a conbee 2 stick. Got everything set up more or less the way I want it. I’ve now been getting started with some scripts, but progress is painfully slow.

Current Problem: I’m trying to change the brightness of my tradfri lamps via a script (aim is to emulate a sunset & sunrise). I’ve figured out how to change the color and saturation. Whenever I adjust the brightness parameter the lights flicker once but don’t change the brightness. Brightness control from the UI via the slider as well as via the remote work fine. I can see in the logs, that the commands being sent to the lights via the script are no different to those appearing when the remote/ UI is used. On the remote I have to hold down the brightness control button for a while though.

My script so far:

lightState = itemRegistry.getItem(‘CouchLightColor_Color’).getState().toString().split(’,’);

hue = parseInt(lightState[0]);
saturation = parseInt(lightState[1]);
brightness = parseInt(lightState[2]);

if (saturation < 100 && brightness == 100){
saturation = saturation +1;
if (brightness > 0){
brightness = brightness -1;

newState = hue.toString() + ‘,’ + saturation.toString() + ‘,’ + brightness.toString()

events.sendCommand(‘CouchLightColor_Color’, newState);

The script is meant to start from a known light state that it has during the day, then it’s called at regular intervals via a cron rule in the evenings to start changing the saturation and then the brightness.

If anyone could shed some light on this for me I’d be very grateful!

Found a workaround: If I add the lightbulb not only as a colorlight but also as a dimmable light, I can dimm it. Seems like that shouldn’t be necessary and like I’m missing something…

If you look in your events.log you will see the commands that the UI sends to your Item.
Then you can emulate from your rule.

You can command a Color type Item with a single number and it will treat that like a Dimmer.
But that does depend on the binding knowing what to do with it.

1 Like