Rules for dummies (to be exposed through OHcloud) from broadlink binding

Following this

I’ve endeavoured into rules scripting in OH for the first time.

These are my first ever rules, and the use case isn’t straight forward either (to me at least) - Please check the previous post if in need of more details regarding the funcionalities and the broadlink binding.

The objective is coherently integrating in openhab 3 rooms with the same kind of 433MHz controlled ceiling fan, which integrates a dimmable ceiling light, a fan and a led strip nightlight.
The following rules would affect my guinea pig room, once figured out, the other 2 rooms will be copy pasting+ swapping item names.

before importing them i feel i need to ask for help…

//all of the *_EXPitems are dummy items meant to be EXPosed via openhabcloud to google home
//translation from italian + other: luce=main light, ventilatore=ceiling fan, interruttore=z-wave wall switch, which is exposed

rule " switch off from the wall and reset"
  when 
    item Interruttore_Dario changed from ON to OFF
   then
    if 
      item Luce_Dario_EXP.state !=OFF
     then
     item Luce_Dario_EXP.postUpdate(OFF)        //is this a valid way to update a dummy? is it post as in send or post as in latin for after?
    endif
    if item Ventilatore_Dario_EXP.state !=OFF
     then
     item Ventilatore_Dario_EXP.postUpdate(OFF)
    endif
    if 
      item Led_Dario_EXP.state !=OFF
     then
      item Led_Dario_EXP.postUpdate(OFF)
    endif
    if 
      item Dimmer_Dario_EXP.state !=OFF
     then
      item Dimmer_Dario_EXP.postUpdate(OFF)
    endif
end

rule"switch on from the wall"
  when
    item Interruttore_Dario changed from OFF to ON
   then
    item Luce_Dario_EXP.postUpdate(ON)
end

rule"switch on from remote" 
  while item Interruttore_Dario.state =OFF
    when 
      item Luce_Dario_EXP changed from OFF to ON
     then  
      Interruttore_Dario.sendCommand(ON)
      {
    return;
}
    when 
      item Ventlatore_Dario_EXP changed from OFF to ON
     then
      item Interruttore_Dario.sendCommand(ON)
      wait 0.5 sec                       //how to do it?
      RM3.sendCommand(Dario_Luce_Toggle) //switches off the light
      item Luce_Dario_EXP.postUpdate(OFF)
      wait 0.2 sec                       //how?
      RM3.sendCommand(Dario_Vent_1)      //turns the fan ON
      {
	  return;
}
    when
      item Led_Dario_EXP changed from OFF to ON
     then
      Interruttore_Dario.sendCommand(ON)
      wait 0.5 sec                       //how to do it?
      RM3.sendCommand(Dario_Luce_Toggle) //switches off the light
      item Luce_Dario_EXP.postUpdate(OFF)
      wait 0.2 sec                       //how?
      RM3.sendCommand(Dario_Led_On)      //turns the led ON
      {
	  return;
}
    when
      item Dimmer_Dario_EXP changed from OFF to ON
     then
      Interruttore_Dario.sendCommand(ON)
      wait 0.5 sec                       //how to do it?
      RM3.sendCommand(Dario_Dimmer)      //light goes from 100 to 50
      {
	  return;
}
end

rule"fan command while powered on"
  while 
    item Interruttore_Dario.state =ON
    when 
      item Ventlatore_Dario_EXP changed from OFF to ON
     then
      RM3.sendCommand(Dario_Vent_1)      //turns the fan ON
    when 
      item Ventlatore_Dario_EXP changed from ON to OFF
     then
      RM3.sendCommand(Dario_Vent_Off)    //turns the fan OFF
end

rule "dimmer command while powered on"
  while 
    item Interruttore_Dario.state =ON
    when 
      item Dimmer_Dario_EXP changed from OFF to ON
     then
       RM3.sendCommand(Dario_Dimmer)  //light goes from 100 to 50 OR quickly 0-100-50 if light off at the beginnng
       item Luce_Dario_EXP.postUpdate(ON)
    when 
      item Dimmer_Dario_EXP changed from ON to OFF
     then
       RM3.sendCommand(Dario_Dimmer)  //light goes from 50 to 100
end

rule "led nightlight command while powered on"
  while
    item Interruttore_Dario.state =ON 
    when 
      item Led_Dario_EXP changed from OFF to ON
     then
      RM3.sendCommand(Dario_Led_On)      //turns the led strip ON
        if 
          item Luce_Dario_EXP.state !=OFF
         then
          wait 0.2 sec                      //how?
          RM3.sendCommand(Dario_Luce_Toggle) //switches the main light off
        endif
      item Luce_Dario_EXP.postUpdate(OFF)
      item Dimmer_Dario_Exp.postupdate(OFF)
    when 
      item Led_Dario_EXP changed from ON to OFF
     then
      RM3.sendCommand(Dario_Led_Off)    //turns the led OFF
end

rule"main light command while powered on"
  while 
    item Interruttore_Dario.state =ON
   when 
      item Luce_Dario_EXP changed from ON to OFF
     then
      RM3.sendCommand(Dario_Luce_Toggle)    //switches off
        if 
          item Dimmer_Dario_EXP state !=OFF
         then
           item Dimmer_Dario_EXP.postUpdate(OFF)
        endif
   when item Luce_Dario_EXP changed from OFF to ON
      RM3.sendCommand(Dario_Luce_Toggle) //switches on the light
        if 
          item Led_Dario_EXP state !=OFF
         then
          wait 0.2 sec
          RM3.sendCommand(Dario_Led_Off)    //turns the led OFF
        endif
      item Led_Dario_EXP.postUpdate(OFF)
end 

I don’t dare importing them directly as of yet due to a number of concearns:

  • I am struggling to envision whether one rule would trigger another and i’d end up in an infinite loop of light changes (ive tried to avoid it with item___postUpdate(___) but i feel very unsure about it
  • I don’t know how to have the system wait for half a second or so after the wall switch powers up the ceiling fans(they arent immediately operative when powered, and even z-wave sometimes varies in reaction speed)
  • generally insecure about the syntax. does endif exist, is it used correctly,is while used correctly, do i need to transform something into vars and strings… etc … it’s the first time :baby:
  • do i need to import the map of hex strings from the binding ?

One last thing - i am sure there are ways to shrink the code significantly and render it more elegant. If they are needed to avoid unwanted interactions between rules, im’ all for it - Same if it is going to make me spare few MB of ram on the rpi.
But considering i can’t predict whether i’ll dabble with this kind of coding often or not (ideally if it works better not touch it), i’d like to be able to have it verbose enough to be able to make sense of it after not approaching it for few years… this is maybe the beginners fear speaking tho.

:pray:t2: thanks in advance

In general, you may want to start much simpler, for example, just pick one action and implement it.
And you may need to read this chapter a few times:

Quit a few of your current questions and inevitable future questions are handled there.

Some more details:

Here a direct link to a sub-chapter of above that explains the difference between update and Command: Rules | openHAB

That is a little more complicated, but here a good post about the options:

Understandable, the docs are not the greatest, there is a beginner section in the docs, read this please and especially this overview: https://www.openhab.org/docs/tutorial/rules.html
This one here:

if item Luce_Dario_EXP.state !=OFF 
then 
item Luce_Dario_EXP.postUpdate(OFF) 
endif

will need to look like this:

if (Luce_Dario_EXP.state !=OFF)  {Luce_Dario_EXP.postUpdate(OFF)} 

Note: if there is only one command you want to execute you can omit the {}

Happy reading and please start slow and small. A last tip: read up on the use of logInfo this will help you understand what your rules are doing and if they are doing anything.

AND: use VS Code Editors | openHAB it will highlight all syntax issues for you.

5 Likes

post deleted.
will bang my head and get there, than post for any other noob like me the replies that might be(or not) useful to them.