Dynamic Rules

Hello all,

Firstly, I am new to openhab but what a fantastic bit of software! I am familiar with programming but mainly on cpp.

My first challenge is setting up all the sonoff switches in my house on openhab. All are flashed with tasmota.

Plenty of tutorials on how to do this and I have got it working fine for an individual module.

It seems odd to me that I have to copy and paste this code for each sonoff device and change the unique ID. Copy and pasting code is bad!

Is there a way of creating a dynamic rule?

My though process is:

  1. A rule that is a single line for each of; on, off, status etc etc etc. e.g. in “sonoff.items”:
Switch sonoff_reachable "Sonoff Alive"
    { mqtt="<[mqtt:tele/sonoff_dynamic_UID_goes_here/tele/LWT:state:MAP(unreach.map)]" }
Switch sonoff_switch "Sonoff Switch" <bedroom> (bedroom)
    { mqtt=">[mqtt:cmnd/sonoff_dynamic_UID_goes_here/power:command:*:default],
            <[mqtt:stat/sonoff-0002bedroom/POWER:state:default]" }
Number sonoff_rssi "Sonoff RSSI [%d %%]"
    { mqtt="<[mqtt:tele/sonoff_dynamic_UID_goes_here/STATE:state:JSONPATH($.Wifi.RSSI)]" }
  1. In the sitemap or elsewhere I then define “Bedside_lamp” as “sonoff-ABCD” so that I can do Bedside_lamp.on or Bedside_lamp.status or whatever and it puts the UID into the rule and does the job. e.g. “myhome.sitemap”:
sitemap home label="My Home" {
    Frame label="Bedroom" icon="bedroom"{
        Switch item=sonoff_switch("sonoff-ABCD") label="Lamp"
        Text   item=sonoff_rssi("sonoff-ABCD")

        Switch item=sonoff_switch("sonoff-1234") label="Fan"
        Text   item=sonoff_rssi("sonoff-1234")

Here’s hoping I’ve explained that well. I think it is something that may may many lives easier!


There sure is!

Unfortunately, you are not talking about Rules. You are talking about Items and the Sitemap. There is no way to dynamically create Items. You have to define each Item individually.

You have a little bit more on the sitemap. If you put your Items into a Group and use the Group element on the sitemap. This will put all the members of the Group on the sitemap for you. However, you have zero ability to customize how they appear. You cannot control the order, override the icon, or change the control (for example, you can’t change a Switch to not have the control toggle). Because of these limitations, many users soon abandon using Group on their sitemaps.

You can try to create a custom preprocessor if you want but there is nothing inside of OH that lets you get out of defining each Item individually.

This is really only an issue for 1.x version bindings like MQTT (there is a 2.x version in the works, it won’t be long now I hope). When you define Items that work with 2.x version bindings, all you provide is the Thing’s Channel ID, no more copy and paste of MQTT binding configs with only a slight change in the topic. Now I realize that this moves the problem from the Items to the Things, but with automatic discovery of Things the whole copy-and-paste problem disappears as the Things will be created for you.

So in a very real sense, this is a problem that has already been solved, just not for MQTT yet.