Am I doing lights the right way? (Showcase + Question)

Using Rules DSL this is a reasonable approach. I see a few little nit picks in the code but nothing that would change the over all approach.

One of the challenges is that Rules DSL is limited in a number of ways. Another challenge is you want to be able to modify most every parameter from the UI. Given that this is probably about as good as it gets.

If using one of the JSR223 rules languages you have a few more tools available to you, such as storing stuff into Item metadata instead of using virtual Items, ability to create libraries of functions, etc. But the hard part is the desire to be able to adjust it from the UI. That means you have to use Items and you can’t hard code much.

But anything like this which has so many possible adjustments is going to be pretty complicated.

I’ve seen a lot of approaches to solving this problem and most of them tend to either hard code or calculate on demand most of the things you have your JSON and sliders in HABPanel for. This makes things a whole lot simpler as you don’t need Items and you don’t need widgets. For example, instead of needing to manually set the color temp, base the color temp on the sun’s azimuth from the Astro binding.

A lot of users, myself included, completely separate the timing of events from the execution of the events. In this approach one sets up a time of day state machine so you have one Item that holds the current time of day as a String. That one Item is then used to trigger rules or used by rules to determine what time of day it is and perform the correct operation. See Design Pattern: Simple State Machine (e.g. Time Of Day). With the time of day I then have this really simple rule to drive all my lights:

triggers:
  - id: "1"
    configuration:
      itemName: TimeOfDay
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: >
        var logger =
        Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.TOD_Lights");


        var offGroupName = "TOD_Lights_OFF_" + items["TimeOfDay"];

        var onGroupName = "TOD_Lights_ON_" + items["TimeOfDay"];


        var switchLights = function(tod, grp, st) {
          logger.info("Turning " + st + " the lights for " + tod);
          ir.getItem(grp)
            .members
            .stream()
            .filter(function(light) { return light.state.toString() != st } )
            .forEach(function(light) { events.sendCommand(light.name, st) });
        }


        switchLights(items["TimeOfDay"], offGroupName, "OFF");

        switchLights(items["TimeOfDay"], onGroupName, "ON");
    type: script.ScriptAction

NOTE: That’s an OH 3 UI created rule using JavaScript as the Script Action. All it does though is look for a TOD_LIGHTS_OFF_<TOD> where <TOD> is the time of day state and turns all those off. Then it finds the ON group and turns those lights on. To change the which lights turn on when all I do is add/remove them from the given group.

But my requirements for light control are very simple. Just on and off. So this is about as simple as it will get. The more knobs you create to control them, the more complex it’s going to get.