OH2 rule became script in OH3 and stopped working

Hello,

I recently upgraded from OH2 to OH3 and after some time everything seems working again now except one rather complex rule I created for the lights in my hallway to interact with the motion sensor. All things and items needed for the rule are online and working. Sensor output is readable in the Webinterface and lights are controllable manually. However, the rule I created back in the days in OH2 was moved to ‘Scripts’ after the update and some lines where commented out (the ‘when’ part especially). I restored the rule in its original state but kept it in scripts but it is still not working. Am I missing some changes in the syntax?

This is my rules how it is now stored under ‘Scripts’

// Triggers:
- When BewegungsmelderFlurIKEAOfSweden_Presence changed from OFF

// context: flur-1
var Number hour = now.getHourOfDay
     if ((hour >= 9)  && (hour < 16) && (BewegungsmelderFlurIKEAOfSweden_Dark.state == ON )) {
   sendCommand(Licht_im_Flur_anKueche5minOFF, 30)
         Thread::sleep(40)
         sendCommand(Licht_im_Flur_amBad5minOFF, 30)
     }
     else if ((hour >= 16)  && (hour < 19 )) {
   sendCommand(Licht_im_Flur_anKueche5minOFF, 30)
         Thread::sleep(40)
         sendCommand(Licht_im_Flur_amBad5minOFF, 30)
     }
     else if ((hour >= 17)  && (hour < 20)) {
   sendCommand(Licht_im_Flur_anKueche5minOFF, 60)
         Thread::sleep(40)
         sendCommand(Licht_im_Flur_amBad5minOFF, 60)
     }
     else if ((hour >= 20)  && (hour < 22)) {
   sendCommand(Licht_im_Flur_anKueche5minOFF, 90)
         Thread::sleep(40)
         sendCommand(Licht_im_Flur_amBad5minOFF, 90)
     }
     else if ((hour >= 22)  && (hour < 24)) {
   sendCommand(Licht_im_Flur_anKueche5minOFF, 40)
         Thread::sleep(40)
         sendCommand(Licht_im_Flur_amBad5minOFF, 40)
     }
     else if ((hour >= 0)  && (hour < 5)) {
   sendCommand(Licht_im_Flur_anKueche5minOFF, 20)
         Thread::sleep(40)
         sendCommand(Licht_im_Flur_amBad5minOFF, 20)
     }
     else if ((hour >= 5)  && (hour < 9)) {
   sendCommand(Licht_im_Flur_anKueche5minOFF, 60)
         Thread::sleep(40)
         sendCommand(Licht_im_Flur_amBad5minOFF, 60)
     }

You need to explain further what you mean by “became script”. Is this a UI rule or written in a .rules file? If in a file, please post the full rule. If a UI rule please post the contents of the “Code” tab.

There are breaking changes between OH 2 and OH 3 that exist in this rule. For example now.getHourOfDay is now now.getHour.

Thanks for you reply. It was written in a .rule file under OH2 and after I updated to OH3 it appeared under the scripts section. What I posted above is actually the full content visible from the code tab of the scripts view under OH3.

It’s still in the .rules file?

It doesn’t matter at all what’s shown in the UI for anything that is defined in a text config. The rule itself hasn’t change. It still is as it was defined in the .rules file.

UI rules (and a Script is just a special case for a rule) do not include the “when” section. That;'s handled elsewhere. And a Script is a special rule that has no triggers at all. It’s a rule that can be called from another rule or it can be run manually but it cannot be triggered by an event.

Thanks for these clarifications. I suspect it is still in a .rules files as I did not change anything manually. I will confirm that. Just to be sure not to change the wrong copy: I noticed after the update there where several folder where copies of my original .rule and .item files could be found. Which one is the one read and used under OH3? I have installed openhabian on a Raspberry pi.

I came to the conclusion, that it is more appropriate to recreate the rule. Therefore I created a new rule in the UI under Rules, selected the ‘when’ trigger in the UI and selected as ‘then’ function to execute a rule-dsl script. Then appeared a editor where I pasted my old rule and tried to modify it accordingly to what you mentioned above. I removed the ‘when’ trigger from the script in this editor and changed now.getHourOfDay to now.getHour.

When I open the Code Tab of the new rule it now looks like this
EDIT: Like rlkoshak mentioned below I needed to change the state in the trigger from off to OFF

triggers:
  - id: "1"
    configuration:
      itemName: BewegungsmelderFlurIKEAOfSweden_Presence
      previousState: OFF
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: >
        var Number hour = now.getHour
             if ((hour >= 9)  && (hour < 16) && (BewegungsmelderFlurIKEAOfSweden_Dark.state == ON )) {
           sendCommand(Licht_im_Flur_anKueche5minOFF, 30)
                 Thread::sleep(40)
                 sendCommand(Licht_im_Flur_amBad5minOFF, 30)
             }
             else if ((hour >= 16)  && (hour < 19 )) {
           sendCommand(Licht_im_Flur_anKueche5minOFF, 30)
                 Thread::sleep(40)
                 sendCommand(Licht_im_Flur_amBad5minOFF, 30)
             }
             else if ((hour >= 17)  && (hour < 20)) {
           sendCommand(Licht_im_Flur_anKueche5minOFF, 60)
                 Thread::sleep(40)
                 sendCommand(Licht_im_Flur_amBad5minOFF, 60)
             }
             else if ((hour >= 20)  && (hour < 22)) {
           sendCommand(Licht_im_Flur_anKueche5minOFF, 90)
                 Thread::sleep(40)
                 sendCommand(Licht_im_Flur_amBad5minOFF, 90)
             }
             else if ((hour >= 22)  && (hour < 24)) {
           sendCommand(Licht_im_Flur_anKueche5minOFF, 40)
                 Thread::sleep(40)
                 sendCommand(Licht_im_Flur_amBad5minOFF, 40)
             }
             else if ((hour >= 0)  && (hour < 5)) {
           sendCommand(Licht_im_Flur_anKueche5minOFF, 20)
                 Thread::sleep(40)
                 sendCommand(Licht_im_Flur_amBad5minOFF, 20)
             }
             else if ((hour >= 5)  && (hour < 9)) {
           sendCommand(Licht_im_Flur_anKueche5minOFF, 60)
                 Thread::sleep(40)
                 sendCommand(Licht_im_Flur_amBad5minOFF, 60)
             }
    type: script.ScriptAction

Unfortunately it seems I am still missing something, as it does not trigger the lights, when presence is detected.

OFF not off. Case always matters in OH. There is no “off” state.

1 Like

Thank your for your wonderfully precise review! It works like a charm!