Creating Simple Scene Rule / Script

  • Platform information:
    • Hardware: Raspberry Pi 4
    • OS: Openhabian
    • Java Runtime Environment: 11.0.13
    • openHAB version: 3.1.0

I am trying to get basic light scenes like in https://demo.openhab.org/. Is the demo source available somewhere? I only found this old thread: Where do I find openHAB Demo sitemap/UI implementation - #4 by ThomDietrich but all links to the demo sources there are outdated.

As far as I understood it, there are rules which define when certain events are triggered, and these events may be script executions or actions specified in the rule.

Based on Scenes - how to create scene in OH3? - #4 by yfre, I tried the following rule:

triggers: []
conditions: []
actions:
  - inputs: {}
    id: "1"
    label: hellesLicht
    configuration:
      type: application/javascript
      script: sendCommand(WohnzimmerlampeHelligkeit,100);
    type: script.ScriptAction

Unfortunately, if I define a button like this:

[...]
          slots:
            default:
              - component: oh-button
                config:
                  text: hellesLicht
                  action: rule
                  actionRule: hellesLicht

the rule does not get found and events.log does not contain any information.

Furthermore, I tried to define a script which does this:

import java.awt.Color

logInfo("Helligkeit ist ", WohnzimmerlampeHelligkeit)
sendCommand('WohnzimmerlampeHelligkeit', 100)
WohnzimmerlampeHelligkeit.sendCommand(100)
wohnzimmerlampeHelligkeit.postUpdate(100)

val newColor = new Color(100, 0, 0)
WohnzimmerlampeFarbe.postUpdate(new HSBType(newColor))

Unfortunately, even if I directly click “Run Now” in the surface (and not use the button), nothing happens (also nothing in events.log).

Could you give me an hint how to achieve basic brightness / color temperature /color changing in a rule? (Or at least get error messages, if something is misconfigured).

That looks like a line of DSL rule to me, not javascript.

1 Like

And in openhab.log? I bet you are seeing errors there when you save the rule. As @rossko57 points out, that’s Rules DSL code, not ECMAScript.

If you are looking for a pre-made scenes management solution, and are running OH 3.2 M3 or later look in the Marketplace. There are some rules and UI widgets that let you set your devices how you want them to be in the scene then save that as a scene that can be activated later, all from the UI.

But if you are just learning how to create rules, you just need to pay special attention to the language chosen for a Script Action/Condition and the code you are trying to use.

1 Like

Thanks for the hint, if I change the rule to

triggers: []
conditions: []
actions:
  - inputs: {}
    id: "1"
    label: kickernRule
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: sendCommand(WohnzimmerlampeHelligkeit,100);sendCommand(Lichttemperatur,160);
    type: script.ScriptAction

the rule can be run fine via the admin web interface or via curl -X PUT localhost:8080/rest/rules/5351940ec4/runnow directly on the Pi. The rule itself also has the name kickernRule, so I see no further option to change the name from the hash to the real rule name.

The button definition looks like this:

   slots:
            default:
              - component: oh-button
                config:
                  text: Kickern
                  action: rule
                  actionRule: kickernRule

If I click on the button, the firefox console shows a POST request to http://openhabian:8080/rest/rules/kickernRule/runnow (and openhab.log contains 2021-12-13 21:15:02.275 [INFO ] [utomation.rest.internal.RuleResource] - Received HTTP PUT request for run now at 'rules/kickernRule/runnow' for the unknown rule 'kickernRule'.).

The same happens for the kinoRule:

triggers: []
conditions: []
actions:
  - inputs: {}
    id: "1"
    label: KinoRule
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: |
        // This is an example script
        sendCommand(Lichttemperatur,500);

        var i = 100; 
        while (i > 0){
          sendCommand(WohnzimmerlampeHelligkeit,i);
          i-=1;
          Thread.sleep(50);
        }
    type: script.ScriptAction

Do you see why the rules are not found by the buttons? The only solution which I found for now was copying the hashes (so to add actionRule: 5351940ec4), but this does not seem to be a very nice solution.

And in openhab.log? I bet you are seeing errors there when you save the rule. As @rossko57 points out, that’s Rules DSL code, not ECMAScript.

Thanks for that hint! openhab.log helps a lot for debugging.

Since I do not only want scenes but also dynamically changing scenes, I’m trying to get to know the language myself.

In the UI rules editor, there is a UID field and a Name field. The name field is the human readable name of the rule, but, as you have discovered, when trying to access that rule from different parts of OH you must use the UID and not the name. The UID is automatically generated when you first open up a new rule editor, but you can change it at that time. After you have saved a rule, you cannot change the UID.

If you wish to have rules with your own custom UIDs you can copy the relevant code from the old rule, create a new rule which you set the UID for and then paste the code into the new rule.

1 Like

Or you would just recommend to use blocklies similar to here (replace the item names accordingly Office_Main_Light => WZHelligkeit and NanoColor => WZLampeFarbe). I think this should work

which is pretty simple and much less error prone.

In the UI rules editor, there is a UID field and a Name field. The name field is the human readable name of the rule, but, as you have discovered, when trying to access that rule from different parts of OH you must use the UID and not the name. The UID is automatically generated when you first open up a new rule editor, but you can change it at that time. After you have saved a rule, you cannot change the UID.

Thanks for the explanation, so I’ll just use the rule UIDs.

Or you would just recommend to use blocklies similar to here (replace the item names accordingly Office_Main_Light => WZHelligkeit and NanoColor => WZLampeFarbe). I think this should work

Thanks for the hint, but I would like to do something like setting different brightness levels in a loop, and I think this is easier writing the code directly (additionally, I know how to use Java, so this variation of it will not be too hard to learn).