OpenHAB 2.0 Rules: Create list of HSBTypes

rules
openhab2
Tags: #<Tag:0x00007fe057b29dc0> #<Tag:0x00007fe057b29c30>

(Robin) #1

Hi,

I want to create an ArrayList of HSBTypes, s.t., I’m able to iterate through different colors on my HUE lightstrips.
I tried the following without success:

var HSB_RED = new HSBType(new DecimalType(0), new PercentType(100), new PercentType(100))
var HSB_GREEN = new HSBType(new DecimalType(120), new PercentType(100), new PercentType(100))
var HSB_BLUE = new HSBType(new DecimalType(240), new PercentType(100), new PercentType(100))
var HSB_WARM_WHITE = new HSBType(new DecimalType(30), new PercentType(20), new PercentType(100))
var List<HSBType> lightstrip_values = new LinkedList(HSB_RED, HSB_GREEN, HSB_BLUE, HSB_WARM_WHITE)

receiving the following error message:

Variable 'lightstrip_values' on rule file 'hue.rules' cannot be initialized with value 'org.eclipse.xtext.xbase.impl.XConstructorCallImplCustom@13f99ac0 (invalidFeatureIssueCode: null, validFeature: false, explicitConstructorCall: true, anonymousClassConstructorCall: false)': null

However, Xtend documentation is not quite clear for me at this point, especially on syntax for creating arrays.
Any ideas? Thank you very much!


(Rich Koshak) #2

Working with arrays is pretty much a pain in the Rules DSL. I think there is a way to initialize the list like this (there is for Maps) but I don’t know it. I do know that what you have there won’t work.

One possible workaround includes populating the List in a System started rule. You could try something like:

var List<HSBType> lightstrip_values = [HSB_RED, HSB_GREEN, HSB_BLUE, HSB_WARM_WHITE]

(Robin) #3

Thank you very much!
Wouldn’t it be possible to initialize it globally? Can I access this List/Array by using indices, e.g., lightstrip_values.get(i) or lightstrip_values[i] ?


(Rich Koshak) #4

Theoretically it is possible but I don’t know the syntax. I know it is possible for maps as I’ve seen examples. But I’ve never seen an example for a List.

Even though Xtend supports them, the Rules DSL does not support arrays. So you have to use .get(i) and not [i].

Personally, I put this sort of thing into Items and put those Items into a Group and the Group becomes my List. You have the further advantage that you can modify the values from the sitemap if desired and you get restoreOnStartup.


(Robin) #5

The group idea sounds great as well (and probably a proper openhab-way to do it). Do you have any pointer or snippet on how to work with groups?


(Rich Koshak) #6

I think I use Groups in almost all of my Design Pattern posts. Here are a couple I know for sure use Groups:




Hopefully these will provide enough examples to get you going.


(Robin) #7

Thank you very much @rlkoshak!