BasicUI Sitemap Automatically Generate Light Controls from Semantic Model

Using the semantic model, I can generate a basicui sitemap that shows all my lights, grouped by each room. In this example, I only included the switch control, but it can easily be extended to include dimmer/brightness, color picker, color temperature, etc.

And here’s the JRuby code to create this (requires openhab-scripting v5.15.1):

def build_lights(builder, location)
  light_bulbs = [Semantics::Lightbulb, Semantics::Fan].flat_map { |tag| location.equipments(tag) }
  unless light_bulbs.empty?
    builder.frame label: location.label do
      light_bulbs.each do |light_bulb|
        builder.switch item: light_bulb.points(Semantics::Light).first || light_bulb.points(Semantics::Switch).first
      end
    end
  end

  location.locations.each { |sub_location| build_lights(builder, sub_location) }
end

sitemaps.build do
  sitemap "lights", label: "Lights Control" do
    text label: "Inside Lights" do |builder|
      lIndoor.locations.each { |sub_location| build_lights(builder, sub_location) }}
    end

    text label: "Outside Lights" do |builder|
      lOutdoor.locations.each { |sub_location| build_lights(builder, sub_location) }
    end
  end
end

In contrast, this is how I did it previously using a sitemap file:

sitemap lights label="Lights Control"
{
	Group item=gInsideLights label="Inside Lights" 
	Group item=gOutsideLights label="Outside Lights"
}

And this is how it would look - no grouping for each room / no correlation with the semantic model. It’s just alphabetically sorted.

5 Likes