How to include example code from binding docs?

  • Platform information:
    • Hardware: RPi 3+
    • OS: OH
    • openHAB version: 4.2
      1st of all huge applause to all developers! I am an OH starter from scratch and after reading the docs (sufficiently) the installation of the system was successful at my 1st attempt. I could add the bindings for my go-e charger and SMA energymeter immediately. Furthermore I follow the suggestion, to configure anything by the means of the main ui. This worked fine building the semantic model and acoount the things accordingly.
      Now I am at a point of the configuration, that I generally struggle with, just an example:
      The docs of the go-e binding comprehends an examplary code for PV surplus charging that meets my needs close to perfectly. How can I grab it for my configuration?
      Can I stick to the main ui?
      Do I need to code text files which is strictly not suggested for newbies?
      I’d love to go on with enjoying experience.
      Thanks in advance for any help!
      Roland

The rules in the binding docs look like this:

rule "Some Rule Name"
when
  Some openhab event
then
  Several lines
  Of code
end

That pattern is specific to the rulesDSL rules language. The form that is presented there is the form it would use if you were using text file based configuration, but it is very easy to use this rule in the UI instead.

The most common mistake here is to paste all of the rule example in the script editor for a UI rule. That doesn’t work, the point of the UI rule wizard is that it separates all those rule pieces for you (rule, when, and then).

When you create a rule in the UI the wizard starts with the option to give the rule an unique ID. There will be a random one autogenerated, but it is recommended to replace that with something you will recognize when you see it crop up in the logs. Next there’s a line for a human readable rule name, this would be a good place to put the rule text (i.e., Some Rule Name) text from the rule example, but again, it can be anything you like.

Below those, you will see that the wizard dialog replicates the when and then keywords of the rule. So under when you will use the further wizard options to replicate the event trigger in the rule. Much of the time it will be an item event, but there are other options as well such as timing events or events that come directly from things.

Under then you will see you can also add simple graphical rule actions, but in this case you have code you want to run instead of those basic actions, so you’ll select Inline Script as the action type, Rule DSL as the script type, and then you will paste all the code in the then section of the rule example into the text editor that appears.

Edit:
With all of that said, you’ll see many recommendations against new users starting with rulesDSL. It will remain an active part of openHAB, but it is much less user friendly and feature rich than other rule script options. You would be better off spending the little bit of extra time it takes to try and understand the DSL rule and re-create it in blockly. If you are going to stick with OH that will save you many hours of frustration and hassle in the long run.

1 Like

Thanks for the proposed path through the huge variety of options!

I just want to make it clear, you don’t include the “then” and “end” parts. Given the above you would paste only:

    Several lines
    Of code
1 Like