My take on scene configuration in OH3

Hi All,

I’m a new user of OpenHAB and this is also my first post on this community.

One of the things I struggled with was scene configuration and selection in OH. I think that the most average beginners of domotica start with automating their lights, I’m such a starter.

As far as I have searched in the online documentation I couln’t find any ‘out the box’ functionality to create scenes and activate them like in Domoticz or HomeAssistant.

I wanted a solution that could be configured without having to access the OS directly, so everything should be doable via the webUI.

Here is what I have so far and it is more than likely that improvements are possible (and maybe needed):

  1. First of all, I have only one group that contains all “scene-enabled” items, it is just a basic group that will be used in scripting later on.

Properties of this group:

Content of this group (as you can see, mixed item types: bulbs and power plugs):

Each item I want to use in a scene MUST be in that group.

  1. Also, that item has a tag that represents the config for a certain scene. The format of that tag is “Scene[]”.
    In this way I can assign several states, including an “off” state.

By using this method, I keep the scene config as close to the item itself without having to mess around with groups or scripts per scene.

  1. To work around the fact that it is kind of hard to use a code library with functions that accept parameters I created an Item of type string that used to set the name of the scene to apply. I called this item “Scene to apply”.

  1. The next part I made was a dsl script, named “Acivate Scene” that will set the desired state of the item(s) for the activated state.
    This script has the following flow:
  • get the scene name from the string item (see 3)
  • get all members from the scene group (see 1) and get the tags (see 2)
  • if that tag contains the name of the scene, the script will extract the state and send that as a command to the item
  • if the item is a RGB bulb resend the dimlevel to the item as the first command does not set the level (might not be necessary in your case).
    Also, do not send the command to soon or it will not be set on the item.
logDebug('ActivateScene', 'Initializing ActivateScene')

val sceneName = SceneToApply.state.toString()
val tagStartIndex = sceneName.length + 6

logDebug('ActivateScene', "Initiating scene: {}", sceneName)

for (Item item : AllSceneItems.members) {
  val tags = item.tags
  for (String tag : tags) {
    if (tag.contains(sceneName)) {
      logDebug('ActivateScene', "{} is part of {} via tag {}.", item.name, sceneName, tag)
      logInfo('ActivateScene', "Send command {} to {}", tag.substring(tagStartIndex, tag.length - 1), item.name)
      item.sendCommand(tag.substring(tagStartIndex, tag.length - 1))
      if (tag.substring(tagStartIndex, tag.length - 1).contains(',')) {
        val levels = tag.substring(tagStartIndex, tag.length - 1).split(',')
        // fix issue with timings, if you send the command too soon, it will not persist.
        Thread::sleep(750)
        logDebug('ActivateScene', "Setting dimlevel to {} for {}.", levels.get(levels.size -1), item.name)
        item.sendCommand(levels.get(levels.size -1))
      }
    }
  }
}
  
logDebug('ActivateScene', 'Done')
  1. The final piece in this scene puzzle is a rule that will fire on change of the “Scene to Apply” item (see 3)
triggers:
  - id: "1"
    configuration:
      itemName: SceneToApply
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      considerConditions: true
      ruleUIDs:
        - ActivateScene
    type: core.RunRuleAction
  1. Now all that’s left is to create buttons on my overview (or any other) page for each scene.
    This can be accomplished by using a default cell, select item “scene to apply” and set action to “Send command”. The value for “Action Command” is the name of the scene (without the prefix Scene) as configured in your item (see 2)
    image

Choose a fancy icon and you might get something like this:

  1. You can schedule a scene simply by using the logic of step 6 in a schedule, nothing fancy.
    e.g. turn on the lights on weekdays at 0730 except when it is a holiday (might be material for another post?):

Recap:

  1. Group for scene items
  2. Scene[] tag for each scene on each item
  3. String Item “Scene to apply”
  4. Script “Activate Scene”
  5. Rule “SceneActivator”
  6. Button to activate a scene
  7. Schedule if necessary

Things to improve:

  • right now, the items are processed one after the other, I might look into finding ways to process it in parallel.
  • I want to make a custom widget for the scenes so that it matches my other custom widgets
  • The need to resend the dimlevel might behave differently with other brands or types of items so I will look into it when that happens.

I hope this post will help other OpenHAB starters to get as hooked on OH.
Feedback is always welcome!

Kind regards,
Roeland

4 Likes

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.