Rules do not work and have strange behaviour... what is wrong?

Hey there, I am playing around with some rules. After my first complex try did not work I tried easier rules to find the error but I cannot find one. Can somebody explain me where the mistake is?

This rule does not work:

rule "Check if enough power is beeing produced for venting"                                                                                                                             
when                                                                                                                                                                                          
        Time cron "0 */1 * * * ?" 
then   
        if(Solar_Aktuell.state >= 200)   {       //Check if there is more than 200W power                                                                                                                                                                       
           if(lg500_anaus != ON)                    //If switch lg500_anaus is set to off then ...                                                                                                                                             
           {                                                                                                                                                                                     
           sendCommand(lg500_anaus, ON)                   //...set it on ... and...                                                                                                                                       
           sendTelegram("bot1", "venting started")          // ...send me a telegram                                                                                                               
           } 
       }                                                                                                                                                                                    
end                                                                                                                                                                                           
                                                                                                                                                                                              

This rule sends me a telegram every minute and switches the switch on every minute… but why if it IS already on??

This thing does not do anything at all:

rule "LĂĽftungsanlage anschalten wenn genug Strom produziert wird"                                                                                                                             
when                                                                                                                                                                                          
        Time cron "0 */1 * * * ?" && Solar_Aktuell.state >= 200                                                                                                                               
then                                                                                                                                                                                          
        if(lg500_anaus != ON)                                                                                                                                                                 
        {                                                                                                                                                                                     
        sendCommand(lg500_anaus, ON)                                                                                                                                                          
        sendTelegram("bot1", "Lueftungsanlage wird angestellt")                                                                                                                               
        }                                                                                                                                                                                     
end                                                                                                                                                                                           

Hello!

In both examples, you have an error in if (lg500_anaus != ON) which should be if (lg500_anaus.state != ON)
In the second example, I’m not sure if it’s even possible to have trigger defined that way (with && and item state value).

Best regards,
Davor

You have to keep types in mind:

rule "Check if enough power is beeing produced for venting"                                                              
when
    Time cron "0 * * * * ?" // */1 is the same as *
then
    if((Solar_Aktuell.state as DecimalType) >= 200)   {       // .state is a state, but has to be of decimal type here
        if(lg500_anaus.state != ON) {                         // compare the state, not the item!
            lg500_anaus.sendCommand(ON)                       // method is more reliable as it is 
                                                              // capable in doing simple type conversions
            sendTelegram("bot1", "venting started")
        }
    }
end

In question of trigger in the second example, this is completely wrong. The trigger part (between when and the first then) is a listing of triggers, which would trigger the execution of the rule. If there has to be a restriction, you will have to do this after the first then.

OK! Thank you very much, I got it now and will have a look at the code and watch if the behaviour is correct, but as long as I can see… it seems to be logic.

Still does not work the way I expectet it to work…

rule "LĂĽftungsanlage anschalten wenn genug Strom produziert wird"                                                                                                                             
when                                                                                                                                                                                          
        Time cron "0 */1 * * * ?"                                                                                                                                                             
                                                                                                                                                                                              
then                                                                                                                                                                                          
        if ((Solar_Aktuell.state as DecimalType) >= 1000)                                                                                                                                     
        {                                                                                                                                                                                     
                if(lg500_anaus.state != ON)                                                                                                                                                   
                {                                                                                                                                                                             
                sendCommand(lg500_anaus, ON)                                                                                                                                                  
                sendTelegram("bot1", "Lueftungsanlage wird angestellt")                                                                                                                       
                }                                                                                                                                                                             
        }                                                                                                                                                                                     
end                                                                                                                                                                                           
                                                                                                                                                                                              
rule "LĂĽftungsanlage ausschalten wenn zu wenig Strom produziert wird"                                                                                                                         
when                                                                                                                                                                                          
        Time cron "0 */1 * * * ?"                 //check every minute                                                                                                                                            
then                                                                                                                                                                                          
        if ((Solar_Aktuell.state as DecimalType) < 1000)                                                                                                                                    
        {                                                                                                                                                                                     
                if(lg500_anaus.state != OFF)                                                                                                                                                  
                {                                                                                                                                                                             
                sendCommand(lg500_anaus, OFF)                                                                                                                                                 
                sendTelegram("bot1", "Lueftungsanlage wird ausgeschaltet")                                                                                                                    
                }                                                                                                                                                                             
        }                                                                                                                                                                                     
end   

You should use the method and not the action. Maybe this fixes your issue.

But you should remember, that you have no hysteresis for your ON an OFF switching of the vent.
If your solar power is around 1000 W the vent is switching on and off while the value is varying between 999 and 1000 W.
For real life use it will better to Switch on >= 1000 W and switch off < 900 W. These values should be adjusted to the bandwith of the real values, maybe together with a timing condition, e.g. switch on for a minimum of 2 minutes …

The logic is similar to automatic shade control with light intensity sensors and moving clouds…

Instead of your time based rule you can also check for changing of your Solar_Aktuel item.

rule "Check amount of solar power"
when
   Item Solar_Aktuell changed
then
   if ((Solar_Aktuell.state as DecimalType) > 1000)
   { // Switch on
   }
   else if ((Solar_Aktuell.state as DecimalType) < 900)
   { // Switch off
   }
end
1 Like

Thank you! That was the issue. I really forgot to change from action to method. And thank you for the usefull hint with the values.

probably a silly question, but what’s the difference between using the method and using the action?

I’ve used actions exclusively in my - moderately complex - set of openHAB 1.8 rules and haven’t had any problems (or at least none that I’ve spotted!)

Dan

…sorry… here again :smiley:

I found out something strange:

When I use the trigger

 Time cron "0 */1 * * * ?" 

the rule workes as espected. But when I use the trigger

 Item Solar_Aktuell changed

it does not.

My imports are:

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*         //not actually needed
import org.openhab.model.script.actions.*    //not actually needed

is there something missing? is it a mistake to leave the unused/not needed imports?

Please try

Item Solar_Aktuell received update

instead to trigger the rule.

At least, there was an issue, that under certain circumstances the action was not able to set the correct state from the given value.
As the method has knowledge about the correct type of value (integer, float, string, state…) where the action has not, the method will be more reliable than the action. Maybe there was a PR which solved the issue.

1 Like