Cycle colours from a specified one

Hello,

Last week I tried to make a rule in order for my light to cycle through colours. All of this worked, but using PercentType was new to me.

What I would like to know how to build, and what I could not accomplish on my own, is how to get the wanted brightness and current colour and cycle forwards from that one. Now, the cycle always starts on 0. Unfortunately, my favourite colour is not pink :sweat_smile:

My items:

Color SKJ_LEDstrip_col
Dimmer SKJ_LEDstrip_bri
Dimmer SKJ_LEDstrip_hue

My rule so far:

var LEDhue = 0
val LEDsat = new PercentType(75)
val LEDbright = new PercentType(100)

rule "LEDStrip kleuren"
when
    Time cron "0 * * ? * *" // Every 60 seconds
then
    if (SKJ_LEDstrip_col_cycle.state == OFF || Mensen_Thuis.state == OFF) return;

    if (LEDhue >= 360) {
        LEDhue = 0
    }
    LEDhue = LEDhue + 1

    SKJ_LEDstrip_col.sendCommand(new HSBType(new DecimalType(LEDhue), LEDsat, LEDbright).toString)
end

So I tried to put the value of SKJ_LEDstrip_bri into the value LEDbright. This did not work because I could not convert it to PercentType. Same goes for the colour.

Do any of you have a clue or hint as to how I could build this rule?

Untested…

rule "LEDStrip kleuren"
when
    Time cron "0 * * ? * *" // Every 60 seconds
then
    if (SKJ_LEDstrip_col_cycle.state == OFF || Mensen_Thuis.state == OFF) return;

    var LEDhue = SKJ_LEDstrip_col.getStateAs(HSBType).hue.intValue + 1

    if (LEDhue >= 360) {
        LEDhue = 0
    }

    SKJ_LEDstrip_col.sendCommand(new HSBType(new DecimalType(LEDhue), SKJ_LEDstrip_col.getStateAs(HSBType).saturation, SKJ_LEDstrip_col.getStateAs(HSBType).brightness))
end
1 Like

Thank you very much! I had to change the second SKJ_LEDstrip_col to SKJ_LEDstrip_bri in order to get the brightness right, and I had to add the .toString at the end of the final sendCommand line.

But now it works :smile: :+1:

rule "LEDStrip colours"
when
    Time cron "0 * * ? * *" // Every 60 seconds
then
    if (SKJ_LEDstrip_col_cycle.state == OFF || Mensen_Thuis.state == OFF) return;

    var LEDhue = SKJ_LEDstrip_col.getStateAs(HSBType).hue.intValue + 1

    if (LEDhue >= 360) {
        LEDhue = 0
    }

    SKJ_LEDstrip_col.sendCommand(new HSBType(new DecimalType(LEDhue), SKJ_LEDstrip_col.getStateAs(HSBType).saturation, SKJ_LEDstrip_col.getStateAs(HSBType).brightness).toString)

I don’t understand. The code I had pulls the existing brightness of the LED, increments it, and then updates the LED with the new hue, using the existing saturation and brightness. Why would you need another Item for the brightness?

It won’t break anything, but that is unnecessary. :slightly_smiling_face:

While typing my original answer it struck me; You are right regarding the redundant brightness item. :grin:
I forgot that SKJ_LEDstrip_col also has a brightness value as part of the HSBType. I will edit my second rule above to reflect that.

Regarding the .toString: If I remove this part, I get the following error in Visualstudio with a red line under ¨sendCommand¨:

Ambiguous feature call.
The extension methods
	sendCommand(Item, Command) in BusEvent and
	sendCommand(Item, Number) in BusEvent
both match.(org.eclipse.xtext.xbase.validation.IssueCodes.ambiguous_feature_call)

After adding the toString, this warning is gone. Am I missing something?

It will work either way. The validation chokes on the command, so use the string. I have not used the DSL in a couple years and I’m spoiled with the ease of scripted automation. There is a lot less complaining about things that don’t need to be fixed! :wink: