Passing percentage to a color item


(Kim Skatun) #1

According to
http://docs.openhab.org/configuration/items.html#type
you should be able to pass Percent to a color item:

Color Color information (RGB) OnOff, IncreaseDecrease, Percent, HSB

so then the dimmer item should be able to pass Percentage to the color item or?

Dimmer Item carrying a percentage value for dimmers OnOff, IncreaseDecrease, Percent

Here is my non working code:

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////// Change color freely ////////////////7////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

rule "Set RGB value Living Room"
    when
            Item Color_LivingRoom changed
    then
    	hsbValue = Color_LivingRoom.state as HSBType
		Group_DMX_LivingRoom.members.filter[l|
			l.state.toString != hsbOff
		].forEach[l| l.sendCommand(hsbValue.toString) ]
end


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////// Dim the lights //// ////////////////7//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

rule "Change dimming in Living Room"
when
	Item Dimmer_LivingRoom changed
then
	logInfo("Debug","Received command is " + Dimmer_LivingRoom.Percent.toString() + " what is going on???")
	Color_LivingRoom.sendCommand(Dimmer_LivingRoom)
end

the error code I get is:

2017-09-19 22:41:50.059 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Change dimming in Living Room': An error occured during the script execution: The name '<XFeatureCallImplCustom>.Percent' cannot be resolved to an item or type.


(Rich Koshak) #2

Do you mean

logInfo("Debug", "Received command is " + Dimmer_LivingRoom.state.toString + " what is going on???")
Color_LivingRoom.sendCommand(Dimmer_LivingRoom.state)

PercentType is the type of the state carried by Dimmer_LivingRoom and indeed you can send a PercentType as a command or update to a ColorItem. But you are not using the correct syntax to do so.


(Kim Skatun) #3

I tried that first:

2017-09-19 23:46:46.222 [INFO ] [eclipse.smarthome.model.script.Debug] - Received command is 72 SHOULD NEVER HAPPEN
2017-09-19 23:46:46.232 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Change dimming in Living Room': An error occured during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.sendCommand(org.eclipse.smarthome.core.items.Item,java.lang.String) on instance: null

I thought i had to specify that the dimmer is percent so that I would not pass on or something else to the coloritem


(Rich Koshak) #4

A Dimmer can receive ON/OFF commands but will only ever carry a PercentType.

You may need to do either

sendCommand(Dimmer_LivingRoom.state as PercentType)

or

sendCommand(Dimmer_LivingRoom.state.toString)

I don’t have color lights or dimmers so I don’t know if there is some quirk in how OH is able to cast the state to the right type. It might be ambiguous to the rules engine because a Dimmer’s state is a Number, DecimalType, and PercentType all at the same time.


(Kim Skatun) #5

That did the trick, you are awesome!! But it introduced a new problem!

So I have 30% of my lights in group Group_DMX_LivingRoom off, then I dim the lights with the dimmer or the color wheel, but then it turns all lights on!!

which means that this expression l.state.toString != hsbOff somehow is not working!

the states of my lights in the group should be 0,0,0 according to the log:

2017-09-20 00:37:33.775 [ItemCommandEvent          ] - Item 'LL1' received command 0,0,0
2017-09-20 00:37:33.780 [GroupItemStateChangedEvent] - Group_DMX_TV changed from 123,56,51 to UNDEF through LL1
2017-09-20 00:37:33.782 [GroupItemStateChangedEvent] - Group_DMX_LivingRoom changed from 123,56,51 to UNDEF through LL1
2017-09-20 00:37:33.784 [GroupItemStateChangedEvent] - Group_DMX_Wall changed from 123,56,51 to UNDEF through LL1
2017-09-20 00:37:33.786 [ItemStateChangedEvent     ] - LL1 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:33.788 [GroupItemStateChangedEvent] - Group_DMX_Dinner changed from 123,56,51 to UNDEF through LL1
2017-09-20 00:37:33.791 [ItemCommandEvent          ] - Item 'LL2' received command 0,0,0
2017-09-20 00:37:33.796 [ItemCommandEvent          ] - Item 'LL3' received command 0,0,0
2017-09-20 00:37:33.798 [ItemStateChangedEvent     ] - LL2 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:33.804 [ItemStateChangedEvent     ] - LL3 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:33.806 [GroupItemStateChangedEvent] - Group_DMX_TV changed from UNDEF to 0,0,0 through LL2
2017-09-20 00:37:33.809 [GroupItemStateChangedEvent] - Group_DMX_Lounge changed from 123,56,51 to UNDEF through LL2
2017-09-20 00:37:33.810 [ItemCommandEvent          ] - Item 'LL4' received command 0,0,0
2017-09-20 00:37:33.812 [ItemCommandEvent          ] - Item 'LL5' received command 0,0,0
2017-09-20 00:37:33.816 [ItemStateChangedEvent     ] - LL4 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:33.819 [GroupItemStateChangedEvent] - Group_DMX_Kitchen changed from 123,56,51 to UNDEF through LL3
2017-09-20 00:37:33.821 [GroupItemStateChangedEvent] - Group_DMX_Cooking changed from 123,56,51 to UNDEF through LL3
2017-09-20 00:37:33.824 [ItemCommandEvent          ] - Item 'LL6' received command 0,0,0
2017-09-20 00:37:33.827 [ItemCommandEvent          ] - Item 'LL7' received command 0,0,0
2017-09-20 00:37:33.832 [ItemCommandEvent          ] - Item 'LL8' received command 0,0,0
2017-09-20 00:37:33.836 [GroupItemStateChangedEvent] - Group_DMX_Wall changed from UNDEF to 0,0,0 through LL4
2017-09-20 00:37:33.837 [ItemStateChangedEvent     ] - LL5 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:33.841 [ItemStateChangedEvent     ] - LL6 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:33.846 [ItemStateChangedEvent     ] - LL7 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:33.850 [ItemCommandEvent          ] - Item 'LL9' received command 0,0,0
2017-09-20 00:37:33.854 [ItemStateChangedEvent     ] - LL8 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:33.854 [ItemCommandEvent          ] - Item 'LL10' received command 0,0,0
2017-09-20 00:37:33.859 [ItemCommandEvent          ] - Item 'LL11' received command 0,0,0
2017-09-20 00:37:33.866 [ItemCommandEvent          ] - Item 'LL12' received command 0,0,0
2017-09-20 00:37:33.873 [ItemCommandEvent          ] - Item 'LL13' received command 0,0,0
2017-09-20 00:37:33.873 [ItemStateChangedEvent     ] - LL9 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:33.875 [ItemStateChangedEvent     ] - LL10 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:33.876 [ItemCommandEvent          ] - Item 'LL14' received command 0,0,0
2017-09-20 00:37:33.883 [ItemCommandEvent          ] - Item 'LL15' received command 0,0,0
2017-09-20 00:37:33.883 [ItemStateChangedEvent     ] - LL11 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:33.888 [ItemCommandEvent          ] - Item 'LL16' received command 0,0,0
2017-09-20 00:37:33.890 [ItemStateChangedEvent     ] - LL12 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:33.893 [ItemCommandEvent          ] - Item 'LL17' received command 0,0,0
2017-09-20 00:37:33.899 [ItemStateChangedEvent     ] - LL16 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:33.901 [ItemStateChangedEvent     ] - LL13 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:33.907 [GroupItemStateChangedEvent] - Group_DMX_Dinner changed from UNDEF to 0,0,0 through LL12
2017-09-20 00:37:33.912 [ItemCommandEvent          ] - Item 'LL18' received command 0,0,0
2017-09-20 00:37:33.918 [ItemStateChangedEvent     ] - LL14 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:33.920 [ItemStateChangedEvent     ] - LL17 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:33.921 [ItemCommandEvent          ] - Item 'LL19' received command 0,0,0
2017-09-20 00:37:33.923 [ItemStateChangedEvent     ] - LL15 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:33.928 [GroupItemStateChangedEvent] - Group_DMX_Kitchen changed from UNDEF to 0,0,0 through LL15
2017-09-20 00:37:33.929 [ItemCommandEvent          ] - Item 'LL20' received command 0,0,0
2017-09-20 00:37:33.931 [GroupItemStateChangedEvent] - Group_DMX_Cooking changed from UNDEF to 0,0,0 through LL15
2017-09-20 00:37:33.936 [ItemCommandEvent          ] - Item 'LL21' received command 0,0,0
2017-09-20 00:37:33.942 [ItemCommandEvent          ] - Item 'LL22' received command 0,0,0
2017-09-20 00:37:33.945 [ItemCommandEvent          ] - Item 'LL23' received command 0,0,0
2017-09-20 00:37:33.949 [ItemCommandEvent          ] - Item 'LL24' received command 0,0,0
2017-09-20 00:37:33.953 [ItemStateChangedEvent     ] - LL18 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:33.954 [ItemCommandEvent          ] - Item 'LL25' received command 0,0,0
2017-09-20 00:37:33.954 [ItemStateChangedEvent     ] - LL19 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:33.957 [ItemStateChangedEvent     ] - LL20 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:33.958 [ItemCommandEvent          ] - Item 'LL26' received command 0,0,0
2017-09-20 00:37:33.962 [ItemCommandEvent          ] - Item 'LL27' received command 0,0,0
2017-09-20 00:37:33.965 [ItemCommandEvent          ] - Item 'LL28' received command 0,0,0
2017-09-20 00:37:33.973 [ItemCommandEvent          ] - Item 'LL29' received command 0,0,0
2017-09-20 00:37:33.979 [ItemCommandEvent          ] - Item 'LL30' received command 0,0,0
2017-09-20 00:37:33.982 [ItemCommandEvent          ] - Item 'LB1' received command 0,0,0
2017-09-20 00:37:33.984 [ItemCommandEvent          ] - Item 'LB2' received command 0,0,0
2017-09-20 00:37:33.988 [ItemCommandEvent          ] - Item 'LB3' received command 0,0,0
2017-09-20 00:37:33.991 [ItemCommandEvent          ] - Item 'LB4' received command 0,0,0
2017-09-20 00:37:33.996 [ItemCommandEvent          ] - Item 'LB5' received command 0,0,0
2017-09-20 00:37:34.001 [ItemCommandEvent          ] - Item 'LB6' received command 0,0,0
2017-09-20 00:37:34.007 [ItemCommandEvent          ] - Item 'LL2' received command 45,80,100
2017-09-20 00:37:34.013 [ItemCommandEvent          ] - Item 'LL9' received command 45,80,100
2017-09-20 00:37:34.016 [ItemStateChangedEvent     ] - LL21 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:34.017 [ItemCommandEvent          ] - Item 'LL11' received command 45,80,100
2017-09-20 00:37:34.023 [ItemCommandEvent          ] - Item 'LL14' received command 45,80,100
2017-09-20 00:37:34.024 [ItemStateChangedEvent     ] - LL27 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:34.026 [ItemCommandEvent          ] - Item 'LL17' received command 45,80,100
2017-09-20 00:37:34.032 [ItemCommandEvent          ] - Item 'LL20' received command 45,80,100
2017-09-20 00:37:34.034 [ItemCommandEvent          ] - Item 'LL28' received command 45,80,100
2017-09-20 00:37:34.041 [ItemCommandEvent          ] - Item 'LL29' received command 45,80,100
2017-09-20 00:37:34.044 [ItemCommandEvent          ] - Item 'Light_Wall_Dinner' received command ON
2017-09-20 00:37:34.047 [ItemCommandEvent          ] - Item 'Light_Wall_TV' received command ON
2017-09-20 00:37:34.050 [ItemCommandEvent          ] - Item 'Light_Wall_FirePlace' received command ON
2017-09-20 00:37:34.052 [GroupItemStateChangedEvent] - Group_DMX_Reading changed from 123,56,51 to UNDEF through LL18
2017-09-20 00:37:34.055 [GroupItemStateChangedEvent] - Group_DMX_Reading changed from UNDEF to 0,0,0 through LL23
2017-09-20 00:37:34.057 [ItemStateChangedEvent     ] - LL26 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:34.059 [ItemStateChangedEvent     ] - LL22 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:34.059 [ItemStateChangedEvent     ] - LL28 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:34.061 [ItemStateChangedEvent     ] - LL29 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:34.061 [ItemStateChangedEvent     ] - LL23 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:34.080 [ItemStateChangedEvent     ] - LL24 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:34.082 [GroupItemStateChangedEvent] - Group_DMX_FirePlace changed from 123,56,51 to UNDEF through LL27
2017-09-20 00:37:34.084 [GroupItemStateChangedEvent] - Group_DMX_Lounge changed from UNDEF to 0,0,0 through LL29
2017-09-20 00:37:34.085 [GroupItemStateChangedEvent] - Group_DMX_Bar changed from 123,56,51 to UNDEF through LB1
2017-09-20 00:37:34.087 [GroupItemStateChangedEvent] - Group_DMX_Bar changed from UNDEF to 0,0,0 through LB6
2017-09-20 00:37:34.090 [ItemStateChangedEvent     ] - LL25 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:34.092 [ItemStateChangedEvent     ] - LL30 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:34.096 [ItemStateChangedEvent     ] - LB2 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:34.098 [ItemStateChangedEvent     ] - LB1 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:34.105 [ItemStateChangedEvent     ] - LB4 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:34.106 [ItemStateChangedEvent     ] - LB5 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:34.107 [ItemStateChangedEvent     ] - LB3 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:34.108 [ItemStateChangedEvent     ] - LB6 changed from 123,56,51 to 0,0,0
2017-09-20 00:37:34.112 [ItemStateChangedEvent     ] - LL11 changed from 0,0,0 to 45,80,100
2017-09-20 00:37:34.119 [GroupItemStateChangedEvent] - Group_DMX_FirePlace changed from UNDEF to 0,0,0 through LL25
2017-09-20 00:37:34.121 [GroupItemStateChangedEvent] - Group_DMX_Wall changed from 0,0,0 to UNDEF through LL2
2017-09-20 00:37:34.123 [ItemStateChangedEvent     ] - LL9 changed from 0,0,0 to 45,80,100
2017-09-20 00:37:34.123 [GroupItemStateChangedEvent] - Group_DMX_Dinner changed from 0,0,0 to UNDEF through LL9
2017-09-20 00:37:34.128 [ItemStateChangedEvent     ] - LL2 changed from 0,0,0 to 45,80,100
2017-09-20 00:37:34.130 [GroupItemStateChangedEvent] - Group_DMX_Lounge changed from 0,0,0 to UNDEF through LL9
2017-09-20 00:37:34.131 [ItemStateChangedEvent     ] - LL14 changed from 0,0,0 to 45,80,100
2017-09-20 00:37:34.132 [GroupItemStateChangedEvent] - Group_DMX_Kitchen changed from 0,0,0 to UNDEF through LL14
2017-09-20 00:37:34.133 [ItemStateChangedEvent     ] - LL17 changed from 0,0,0 to 45,80,100
2017-09-20 00:37:34.138 [GroupItemStateChangedEvent] - Group_DMX_Cooking changed from 0,0,0 to UNDEF through LL9
2017-09-20 00:37:34.138 [ItemStateChangedEvent     ] - LL20 changed from 0,0,0 to 45,80,100
2017-09-20 00:37:34.140 [GroupItemStateChangedEvent] - Group_DMX_TV changed from 0,0,0 to UNDEF through LL2
2017-09-20 00:37:34.142 [GroupItemStateChangedEvent] - Group_DMX_Lounge changed from UNDEF to 45,80,100 through LL29
2017-09-20 00:37:34.142 [ItemStateChangedEvent     ] - LL28 changed from 0,0,0 to 45,80,100
2017-09-20 00:37:34.144 [ItemStateChangedEvent     ] - LL29 changed from 0,0,0 to 45,80,100
2017-09-20 00:37:34.149 [ItemStateChangedEvent     ] - Light_Wall_Dinner changed from OFF to ON
2017-09-20 00:37:34.149 [ItemStateChangedEvent     ] - Light_Wall_FirePlace changed from OFF to ON
2017-09-20 00:37:34.151 [ItemStateChangedEvent     ] - Light_Wall_TV changed from OFF to ON
2017-09-20 00:37:34.151 [GroupItemStateChangedEvent] - Group_DMX_Reading changed from 0,0,0 to UNDEF through LL28

I also tried to change my rule to this without any success:

            hsbOff = HSBType::fromRGB(0, 0, 0) 
		Group_DMX_LivingRoom.members.filter[l|
			l.state.toString != hsbOff.toString
		].forEach[l| l.sendCommand(hsbValue.toString) ]

(Rich Koshak) #6

Unless hsbOff is the String “0,0,0” that statement will never work.

If hsbOff is an HSBType then either get rid of the toString or add a toString to the hsbOff.

All of these problems are problems with mixing up your types. You might benefit from a review of Type Conversions.

But ultimately I think you need to log out what each light is set to. They may not be completely off or something.


(Kim Skatun) #7

From your link

Color Item

A Color Item stores an HSBType.
The HSB stands for Hue, Saturation, and Brightness. Often one has the desired color as an RGB values (Red, Green, Blue). The following code can be used to send an RGB value to a Color Item.

import java.awt.Color

...

// in rule body
val newColor = new Color(red, blue, green) // where red, blue, and green are ints between 0 and 255
MyColorItem.sendCommand(new HSBType(newColor))

So I think the issue is that 0,0,0 as rgb set to HSB is not neccesary 0 in saturaion and hue and hence my condition fails!

from the docs I read:
Color Color information (RGB) OnOff, IncreaseDecrease, Percent, HSB
which means that if I set the color item with HSB type, the color object should be able to convert this to OnOffType, so if I set :

val newColor = new Color(0, 0, 0) // where red, blue, and green are ints between 0 and 255
MyColorItem.sendCommand(new HSBType(newColor))

then MyColorItem.state as OnOffType should be OFF, but this fails in my condition MyColorItem!=OFF

also fails to log it:

logInfo("Debug","Light state is " + " " + (l.state as OnOffType).toString )

What am I missing here?

Instead off sending HSBvalue to turn it off could I do this instead?

MyColorItem.sendCommand(new OnOffType(OFF))


(Rich Koshak) #8

This is why I recommended logging out the state of each light.

Like I said above for the Dimmer, you can send ON/OFF or a PercentType to a Color Item but it’s state will only ever be an HSBType. You can’t cast the state of a Color Item to anything else.

Try

MyColorItem.sendCommand(OFF)

But you cannot do

if(MyColorItem.state == OFF)

One last thing, you never have to cast something if all you are going to do is call toString.


(Kim Skatun) #9

Thanks, I now found the right section in the documentation:

Since an item has a SINGLE state, these multiple data types can be considered different views to this state. The data type carrying the most information about the state is usually used to keep the internal state for the item, and other datatypes are converted from this main data type. This main data type is normally the first element in the list returned by Item.getAcceptedDataTypes().

I am not quite happy with my code or the Openhab documentation regarding the HSBType, after reading the https://github.com/openhab/openhab-core/blob/master/bundles/org.openhab.core.compat1x/src/main/java/org/openhab/core/library/types/HSBType.java I did figure out that value is brightness!!

It would have been more elegant to cast it to OnOffType and then check if its equal OFF, also I do

l.sendCommand(hsbValue.toString) because I think this is faster then
l.sendCommand((Color_LivingRoom.state as HSBType).toString)

Here is my working hard to read code!

rule "Set RGB value Living Room"
    when
            Item Color_LivingRoom changed
    then
    	hsbValue = Color_LivingRoom.state as HSBType

		Group_DMX_LivingRoom.members.filter[l|
			//logInfo("Debug","Light brightness for  " +l.name + " is  "  + (l.state as HSBType).value)
			(l.state as HSBType).value!= 0
		].forEach[l| l.sendCommand(hsbValue.toString) ]
end

(Rich Koshak) #10

It would have been more elegant to cast it to OnOffType and then check if
its equal OFF

Unfortunately, that is simply not possible the way everything is written.
Without going into a college level Object Oriented Programming Introduction
course what I can say is that you can only cast from one type to another if
they are in an is a relationship. For example, a DecimalType is a Number
and a DecimalType is a State. So if I’m given an object of type
DecimalType, I can cast it to Number or to State. In the case of HSBType,
it isn’t an ONOFFType nor is it a DecimalType. And there frankly is no way
to make it become one without introducing a whole lot of ugliness and
strangeness to the underlying code and architecture. Also, it would mean
that you would have to store the “state” in the HSBType three times, once
as HSB, once as PercentType, and once as ONOFF. It is usually a sign that
you are doing something wrong when you store the same information three
ways rather than converting from one representation of the data to the
others when asked.

l.sendCommand(hsbValue.toString) because I think this is faster then
l.sendCommand((Color_LivingRoom.state as HSBType).toString)

There is absolutely nothing different about how fast either of those lines
of code work. In fact, they are entirely equivalent in every way except for
the fact that you redundantly added the unnecessary as HSBType and
surrounding parens to the second line. I’ll say again, from the computer’s
perspective, both of those lines are identical.

In the first line you are not casting the value to an ONOFFType,
PercentType, or anything else. You are call toString and since ALL objects
implement toString there is no need to cast.

You only need to cast to a specific type when a more specific class
implements something that you need that the more generic one does not. In
this case State implements toString so there is no need to cast it to the
more specific HSBType.

For example, State doesn’t implement a getBrightness() method so you can’t
say:

val brightness = MyColorItem.state.getBrightness()

because all Items return a State by default. State is the parent of all the
state classes that Items can carry.

So in this case, since you know it is a ColorItem you know that the state
is an HSBType so you have to cast the state to get access to the
getBrightness() method

val brightness = (MyColorItem.state as HSBType).getBrightness()

And in case you missed the hint, you can make your code easier to read by
using the getBrightness method rather than access the value directly which
is usually a better approach anyway. You should always use methods to
access data members unless a method is not provided.

You should also be able to cast l to a ColorItem and its sendCommand method
should take an HSBType so your code could be:

rule "Set RGB value Living Room"
when
    Item Color_LivingRoom changed
then
    hsbValue = Color_LivingRoom.state as HSBType
    Group_DMX_LivingRoom.members.filter[ l | (l.state as
HSBType).getBrightness != 0]. forEach[l as ColorItem |
l.sendCommand(hsbValue) ]
end

Having said all of that, I do notice that the ColorItem (all Items for that
matter) provides a getStateAs method. This method lets you get the state
of the Item as a version other than the default state. You still have to
cast it but you could change the code to the following and get an ONOFFType
to compare to:

rule "Set RGB value Living Room"
when
    Item Color_LivingRoom changed
then
    hsbValue = Color_LivingRoom.state as HSBType
    Group_DMX_LivingRoom.members.filter[ l as ColorItem |
l.getStateAs(ONOFFType.class) != OFF]. forEach[l as ColorItem |
l.sendCommand(hsbValue) ]
end

This works without casting because the enumerated State Types like
ONOFFType and OPENCLOSEDType do not need to be cast to be compared and
comes closer to your desired elegance.

If you wanted the Dimmer’s PercentType you would need to cast it:

Group_DMX_LivingRoom.members.filter[ l as ColorItem |

(l.getStateAs(PercentType.class) as PercentType) != 0]. forEach[l as
ColorItem | l.sendCommand(hsbValue) ]

I’m not so certain that is any prettier or easier to read.


(Kim Skatun) #11

The result of my lighting scheme can be seen here:

Only thing left to implement now is that I can combine switch items and Color Items so that I can loop through the entire light group instead of

		rgbLights.apply(Group_DMX_LivingRoom,Group_DMX_Cooking,hsbValue)
		Light_Kitchen.sendCommand(ON)

Be able to dim my party lights:
val int brightness = 30//Dimmer_LivingRoom.state as PercentType

And to start dimming my lights at evening automatically…

Optional if I get bored I would like to implement a feature thatallows me to play games with my lights…

The current code can be found here:

items here:
https://github.com/skatun/Openhab-CONF/blob/master/items/dmx.items //Color items
https://github.com/skatun/Openhab-CONF/blob/master/items/light.items //Switch lights


(Rich Koshak) #12
val dimmers = Group_Dimmers_And_Lights.members.filter[l | l instanceof DimmerItem]
val switches = Group_Dimmers_And_Lights.members.filter[l | l instanceof SwitchItem]