Help needed with rule in Openhab3

Hi guys and gals,

Is this me or could this be a bug?

I’m having trouble getting this ‘virtual thermostat’ rule to work in OpenHab3. I have a small Infrared heating panel in my office, controled with a tradfri on/off switch. I also have a nodemcu that gives me the temperature over mqtt. Since i’m relatively new to Openhab, to get me going i searched and found a script on this forum and adapted it to my needs. I created all the necessary things and stuff in the sitemap but it just will not work. When i press ‘Run Now’ in ‘Edit Script’ in the UI, the openhab logfile gives me this:

2021-05-02 22:55:23.553 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘be716a4b4e’ failed: ___ rule ___ “Office heating”
when
Item OfficeHeatingMode chan ___ ged o ___ r
Item TempOfficeSetPoint changed or
Item TempOffice changed or
Item OfficeWindow changed
then

…etcetera…

end

  1. The method or field rule is undefined; line 1, column 0, length 4
  2. The method or field when is undefined; line 2, column 22, length 4
  3. The method or field changed is undefined; line 3, column 54, length 7

… and so on.

Here’s my code:

triggers:
  - id: "1"
    configuration:
      startlevel: 40
    type: core.SystemStartlevelTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: >-
        rule "Office heating" when
            Item OfficeHeatingMode changed or
            Item TempOfficeSetPoint changed or
            Item TempOffice changed or 
            Item OfficeWindow changed
        then
            // 0="Off", 1="On", 2="Auto"
            if (OfficeHeatingMode.state == 0) {
                // heater off
                PowerOffice.sendCommand(OFF)
            } else if (OfficeHeatingMode.state == 1) {
                // heater on
                PowerOffice.sendCommand(ON)
            } else if (OfficeHeatingMode.state == 2) {
                // only fire up the heater if the office window is closed
                if (OfficeWindow.state == CLOSED) {
                    // get the current setpoint for the office
                    var Number setpoint = TempOfficeSetPoint.state as DecimalType
                    // calculate the turn on/off temperatures
                    var Number turnOnTemp = setpoint - 0.5
                    var Number turnOffTemp = setpoint + 0.5
                    // get the current temperature in the nursery
                    var Number temp = TempOffice.state as DecimalType
                    // determine whether we need to turn on/off the heater
                    if (temp <= turnOnTemp) {
                        // turn on temp has been reached so switch on the heater
                        PowerOffice.sendCommand(ON)
                    } else if (temp >= turnOffTemp) {
                        // turn off temp has been reached so switch off the heater
                        PowerOffice.sendCommand(OFF)
                    }
                } else {
                    // heater off
                    PowerOffice.sendCommand(OFF)
                }
            }
        end
    type: script.ScriptAction

I also found on the forum this post Scripts in OH3 - #12 by stefan.oh which refers to this issue on Github: OH3 callScript - scripts are not executed · Issue #1990 · openhab/openhab-core · GitHub . That issue is closed however.

My question: is it me, or is something else bugging me? :wink: I’m not a developer but i don’t consider myself to be too stupid to get an openhab rule to work either :slight_smile: Could anybody give me a hint? I’m on the main branch.

PS:
I have also tried to create a rule for this in the UI, but it seems not possible to compare the Item Status of one Iten to the status of another item, i.e, I can say do something when TempOffice < 15, but i can’t say do something when TempOffice < TempOfficeSetPoint. Or am i missing something?

Nope, finger trouble.

The structure of a DSL rule in a text xxx.rules file -

rule "some name"
when
    some triggers
then
    some code
end

When you enter a rule in the UI, you enter the triggers etc. as in form-filling.
The UI takes care of the structure, the rule-when-then-end stuff.
If you have a script section, that’s where “some code” goes.

Be aware that most examples on this forum will come from the ‘legacy’ files environment and if you want to deploy them in the UI environment you can’t just copy-paste the whole show.

1 Like

The script section of a rule built in the UI rule editor should only include the actual executed body of the rule (the parts after the then). The errors that you are seeing are because the script engine is trying to parse the rule "Office heating" and when sections. These are handled in the graphical config.

The "Office heating" rule name simply gets input in the Name field of the top box.

In the When section of the rule editor you have selected the system being initialized with the run level of 40 - Rules loaded. I’m guessing you interpreted this to mean that you want this rule to be loaded into OH when rules are loaded, but that is not what this means. All rules you create will be loaded into OH during startup, this condition will cause a rule to run once during startup when this runlevel is reached and then it will not run again. You need to delete that trigger condition. What should go here instead are all of the when conditions from the original rule:

Item OfficeHeatingMode changed or
Item TempOfficeSetPoint changed or
Item TempOffice changed or 
Item OfficeWindow changed

When you click the add trigger then select item event and use the item finder window that pops up to select you item. Each trigger added in this manner will be an OR condition so you just need to do this four times, once for each of those items.

Then the rest of the rule gets put into a DSL script via the add actionrun script options.

It is quite possible to compare item states, but one must always be careful to fully understand your items first. It only makes sense to compare item state directly if they are the same type. Units of measurement will also play a role here so if you are running into trouble comparing item states then go back to the items and see where the disconnect between the state types might be.

2 Likes

So it was me after all :smiley: Thanks for clearing that up guys, with that knowledge i’ll have a ball with Openhab!