I have various lights with dimmers. Some of these i would like to dim in non-linear steps, individual for each lamp. So when a physical momentary button controlling the light is pressed, the dimmer will “jump” to the next step in a list defined for this particular light. For example one light I’d like the dimming steps to be 0%, 5%, 10%, 25%, 50% and 100%. For another the steps could be 0%, 2%, 8%, 50%, 100%.
Preferably I’d like to define these steps in lists, that the rule governing the dimming looks to know what the next level is.
Of course i can write rules along these lines:
if dimmer.state < 5 %
else if dimmer.state < 10 %
and so on and so forth.
But this is very clumsy(and bulky) code, and editing the steps is a pain.
Is there a clever way to write rules for this, that can be reused for the individual lights with each their list of steps?
Which rules language are you willing to consider? If using pretty much any rules language except Rules DSL you can put the list into Item metadata. Then in the rule parse out the list from the metadata, figure out what the current value is and from there it’s a simple comparison to determine the next.
You could even record the current index into the list as metadata and then it become just increment the index and extract the metadata.
See Design Pattern: Using Item Metadata as an Alternative to Several DPs for some details on working with Item metadata.
If in Rules DSL you can use local variables and Maps or List or additional Items to do the same.