[SOLVED] Two select items with rules do not work together

Hello everybody,
this is the first time I write in the forum but I have been using openhab for about a year, mainly to control heating lights in the home

to make the boiler work and the vacuum cleaners I created two select items, but when I select the boiler menu the aspirator no longer works, I am forced to restart the rules, but only the first one I select works, I can’t understand why in the logs I have no errors, I am attaching the code to your help

thanks

/ls

sitemaps

        Text label="Riscaldamento" icon="heating" {
	    Selection item=FF_Boiler_Modo label="Funzionamento" mappings=[0="Manuale",1="Automatico",2="Presenza",3="Pres+Aut",4="Arieggiare"]
	    Setpoint item=FF_Boiler_Heating step=1 minValue=10 maxValue=30
	    Setpoint item=FF_Boiler_minima step=1 minValue=10 maxValue=30
	    Setpoint item=FF_Boiler_massima step=1 minValue=10 maxValue=30
	    Default item=FF_Boiler_Stato label="Stato Caldaia [%s]" icon="fire"
	    
	}
	
	Text label="Aspiratori" icon="fan_ceiling" {
		Selection item=FF_Aspiratori_Modo icon="movecontrol" label="Modo" mappings=[10="Manuale",11="Automatico"]
                Setpoint item=FF_Bathroom_Imp_Hum step=1 minValue=10 maxValue=100
		Default item=FF_Bathroom_Aspiratore label="Aspiratore Bagno"
		Setpoint item=FF_Kitchen_Imp_Hum step=1 minValue=10 maxValue=100
		Default item=FF_Kitchen_Aspiratore label="Aspiratore Cucina"
        }


    }
}

rules boiler
var modo = 3
var in_casa = 1
var giorno = 1
var lavoro = 0

rule    "Modo Funzionamento"
when
        Item FF_Boiler_Modo received command
then
        switch(receivedCommand.toString) {
                case "0" : {modo=0
		logWarn("mylog","caldaia case 0")}
                case "1" : {modo=1
		logWarn("mylog","caldaia case 1")}
                case "2" : {modo=2
		logWarn("mylog","caldaia case 2")}
                case "3" : {modo=3
		logWarn("mylog","caldaia case 3")}
                case "4" : {modo=4
		logWarn("mylog","caldaia case 4")}
}
end

rule "non sono in casa"
when
    Item gPresence changed to OFF
then
	in_casa=0
end

rule "sono in casa"
when
    Item gPresence changed to ON
then
	in_casa=1
end

rule "giorno"
when
    Time cron "0 0 5 ? * MON,TUE,WED,THU,FRI *" or 
    Time cron "0 0 7 ? * SAT,SUN"
then
    giorno=1
end


rule "notte"
when
    Time cron "0 0 23 ? * MON,TUE,WED,THU,SUN" or 
    Time cron "0 50 23 ? * FRI,SAT"
then
    giorno=0
end

rule "lavoro"
when
    Time cron "0 0 8 ? * MON,TUE,WED,THU,FRI"
then
    lavoro=1
end

rule "non lavoro"
when
    Time cron "0 0 16 ? * MON,TUE,WED,THU,FRI *"
then
    lavoro=0
end 

rule "Accendo Spengo Caldaia"
when
    Item FF_Boiler_Heating changed or
    Item FF_FamilyRoom_Temperature received update or
    Item FF_Boiler_Modo received command or
    Item gPresence changed
then
//manuale
	if(modo==0){
		logWarn("mylog","manuale")}
//automatico
	if(modo==1 && giorno==1 && lavoro==0){
                FF_Boiler_Heating.postUpdate(FF_Boiler_massima.state)
                logWarn("mylog","automatico1")}
	else if(modo==1 && giorno==1 && lavoro==1 || giorno==0){
                FF_Boiler_Heating.postUpdate(FF_Boiler_minima.state)
		logWarn("mylog","automatico2")}

//con presenza
	if(modo==2 && in_casa==1){
                FF_Boiler_Heating.postUpdate(FF_Boiler_massima.state)
                logWarn("mylog","con presenza1")}
	else if(modo==2 && in_casa==0){
                FF_Boiler_Heating.postUpdate(FF_Boiler_minima.state)
                 logWarn("mylog","con presenza2")}

//con presenza in determinati orari
	if(modo==3 && giorno==1 && in_casa==1){
                FF_Boiler_Heating.postUpdate(FF_Boiler_massima.state)
                logWarn("mylog","con presenza orari1")}
	else if(modo==3 && giorno==0 && in_casa==1){
                FF_Boiler_Heating.postUpdate(FF_Boiler_minima.state)
                logWarn("mylog","con presenza orari2")}
	else if(modo==3 && in_casa==0){
                FF_Boiler_Heating.postUpdate(FF_Boiler_minima.state)
                logWarn("mylog","con presenza orari3")}
		

	if(modo==4){
		FF_Boiler_Heating.postUpdate(10)
		logWarn("mylog","imposto arieggiare a 10")}


//alla fine di tutto accendo o spengo la caldaia
    if(FF_Boiler_Heating.state > FF_FamilyRoom_Temperature.state){
		FF_Boiler_Stato.sendCommand(ON)
        } 
    else{
		FF_Boiler_Stato.sendCommand(OFF)
} 

end

rules vacuum

var manuale = 0

rule    "Modo Funzionamento"
when
    Item FF_Aspiratori_Modo received command
then
    switch(receivedCommand.toString) {
            case "10": {manuale=1
                        logWarn("mylog","aspiratore case 10")}
            case "11" : {
                    manuale=0
			logWarn("mylog","aspiratore case 11")}
}
end




rule "aspiratori automatico manuale"
when
Item FF_Bathroom_Humidity received update or
Item FF_Bathroom_Imp_Hum changed or
Item FF_Kitchen_Humidity received update or
Item FF_Kitchen_Imp_Hum changed
then
//automatico
	if(FF_Bathroom_Humidity.state > FF_Bathroom_Imp_Hum.state && manuale==0){
		FF_Bathroom_Aspiratore.sendCommand(ON)
		logWarn("mylog","automatico bagno on")	
} 
else if(FF_Bathroom_Humidity.state < FF_Bathroom_Imp_Hum.state && manuale==0){
		FF_Bathroom_Aspiratore.sendCommand(OFF)
		logWarn("mylog","automatico bagno off")
} 

    if(FF_Kitchen_Humidity.state > FF_Kitchen_Imp_Hum.state && manuale==0){
		FF_Kitchen_Aspiratore.sendCommand(ON)
		logWarn("mylog","automatico cucina on")
}
else if(FF_Kitchen_Humidity.state < FF_Kitchen_Imp_Hum.state && manuale==0){
		FF_Kitchen_Aspiratore.sendCommand(OFF)
		logWarn("mylog","automatico cucina off")
}

//in manuale non faccio nulla
end

widget habpanel vacuum

    <h4>Modo</h4>
<div class="btn-group" uib-dropdown>
  <button id="single-button" type="button" class="btn btn-primary" uib-dropdown-toggle>
    {{itemValue('FF_Aspiratori_Modo') == 10 ? 'Manuale' : itemValue('FF_Aspiratori_Modo') == 11 ? 'Automatico' : ''}} <span class="caret"></span>
  </button>
  <ul class="dropdown-menu" uib-dropdown-menu role="menu" aria-labelledby="single-button">
    <li role="menuitem"><a ng-click="sendCmd('FF_Aspiratori_Modo', '10')">Manuale</a></li>
    <li class="divider"></li>
    <li role="menuitem"><a ng-click="sendCmd('FF_Aspiratori_Modo', '11')">Automatico</a></li>
  </ul>
</div>

widget habpanel boiler

<h4>Funzionamento</h4>
<div class="btn-group" uib-dropdown>
  <button id="single-button" type="button" class="btn btn-primary" uib-dropdown-toggle>
    {{itemValue('FF_Boiler_Modo') == 0 ? 'Manuale' : itemValue('FF_Boiler_Modo') == 1 ? 'Automatico' : itemValue('FF_Boiler_Modo') == 2 ? 'Presenza' : itemValue('FF_Boiler_Modo') == 3 ? 'Pres+Aut' : itemValue('FF_Boiler_Modo') == 4 ? 'Arieggiare' : ''}} <span class="caret"></span>
  </button>
  <ul class="dropdown-menu" uib-dropdown-menu role="menu" aria-labelledby="single-button">
    <li role="menuitem"><a ng-click="sendCmd('FF_Boiler_Modo', '0')">Manuale</a></li>
    <li class="divider"></li>
    <li role="menuitem"><a ng-click="sendCmd('FF_Boiler_Modo', '1')">Automatico</a></li>
    <li class="divider"></li>
    <li role="menuitem"><a ng-click="sendCmd('FF_Boiler_Modo', '2')">Presenza</a></li>
    <li class="divider"></li>
    <li role="menuitem"><a ng-click="sendCmd('FF_Boiler_Modo', '3')">Pres+Aut</a></li>
    <li class="divider"></li>
    <li role="menuitem"><a ng-click="sendCmd('FF_Boiler_Modo', '4')">Arieggiare</a></li>
  </ul>
</div>

For such selection I am using Number items and do present them as a Switch on my sitemap (instead of a Selection in your case).
What does not work? When you select one, the other can no longer be selected or selecting the other does not result in anything in the logs?

yes when I select one, the other does not change state

Personally I do not see a reason for such behavior.

At least you should consider to change your boiler rules slightly. The “main” rule for boiler uses a trigger

Item FF_Boiler_Modo received command 

but in the rule you are using the var modo instead. While the var should be set from the first rule, there is no guaranty that this will take place before the main rule is triggered, so there is a chance that the wrong block is executed, even worse, there is a chance that more than one block is executed, as you are using if() instead of switch(){case{}}. This might result in strange behavior.

As you are using some global vars as boolean (but with a number), why don’t you use Boolean type?

var Boolean giorno = true
var Boolean lavoro = false

rule "giorno"
when
    Time cron "0 0 5 ? * MON-FRI *" or 
    Time cron "0 0 7 ? * SAT,SUN"
then
    giorno = true
end


rule "notte"
when
    Time cron "0 0 23 ? * MON-THU,SUN" or 
    Time cron "0 50 23 ? * FRI,SAT"
then
    giorno = false
end

rule "lavoro"
when
    Time cron "0 0 8 ? * MON-FRI"
then
    lavoro = true
end

rule "non lavoro"
when
    Time cron "0 0 16 ? * MON-FRI *"
then
    lavoro = false
end 

rule "Accendo Spengo Caldaia"
when
    Item FF_Boiler_Heating changed or
    Item FF_FamilyRoom_Temperature received update or
    Item FF_Boiler_Modo received command or
    Item gPresence changed
then
    val Boolean in_casa = if(gPresence.state != ON) false else true
    if(!(FF_Boiler_Modo.state instanceof Number)) {  // in case of no valid number
        FF_Boiler_Modo.postUpdate(3)                 // set to default
        Thread::sleep(500)                           // some delay to sort things out
    }
    switch((FF_Boiler_Modo.state as Number).intValue) {
        case 0: {                                                         //manuale
            logWarn("mylog","manuale")
        }
        case 1: {                                                         //automatico
            if(giorno && !lavoro) {
                FF_Boiler_Heating.postUpdate(FF_Boiler_massima.state)
                logWarn("mylog","automatico1")
            } else if((giorno && lavoro) || !giorno) {
                FF_Boiler_Heating.postUpdate(FF_Boiler_minima.state)
                logWarn("mylog","automatico2")
            }
        }
        case 2: {                                                         //con presenza
            if(in_casa){
                FF_Boiler_Heating.postUpdate(FF_Boiler_massima.state)
                logWarn("mylog","con presenza1")
            } else {
                FF_Boiler_Heating.postUpdate(FF_Boiler_minima.state)
                logWarn("mylog","con presenza2")
            }
        }
        case 3: {                                                         //con presenza in determinati orari
            if(!in_casa){
                FF_Boiler_Heating.postUpdate(FF_Boiler_minima.state)
                logWarn("mylog","con presenza orari3")
            } else {
                if(giorno){
                    FF_Boiler_Heating.postUpdate(FF_Boiler_massima.state)
                    logWarn("mylog","con presenza orari1")
                } else {
                    FF_Boiler_Heating.postUpdate(FF_Boiler_minima.state)
                    logWarn("mylog","con presenza orari2")
                }
            }
        }
        case 4: {
            FF_Boiler_Heating.postUpdate(10)
            logWarn("mylog","imposto arieggiare a 10")
        }
    }
    //alla fine di tutto accendo o spengo la caldaia
    val newState = if(FF_Boiler_Heating.state > FF_FamilyRoom_Temperature.state) ON else OFF
    if(FF_Boiler_Stato.state != newState) FF_Boiler_Stato.sendCommand(newState)
end

thank you now it works, only one thing is wrong

in_casa = if (gPresence != ON) false else true

he does not understand if they are at home
but otherwise everything works now

1 Like

gPresence is an Item?

in_casa = if (gPresence.state != ON) false else true

Jepp, sorry, forgot about the item (like @5iver already mentioned).

The extra rule for this item also isn’t necessary, so I changed the code above (moved the line to the main rule and deleted the extra rule) So the var doesn’t need to be global anymore…