One rule fires but not the other, why

  • Platform information:
    • Hardware: _RPi 3 Openhabian

    • openHAB version: openHAB 2.2.0 Build #1109

I am trying to set-up a couple of rules to control groups of lamps. The lamps are members of groups like
gMorninglights and gDusklights

As control for the rules I have implemented a TimeOfDay rule that based on time of day, sunrise, sunset etc. will tell if Item String TimeOfDay is either MORNING, DAY, AFTERNOON, EVENING or NIGHT. This is modelled after another topic I have found in the community and it seems to work fine. Now for the issue:

Assuming it is EVENING:

This rule:

rule "Morninglights on" //Lights on all the time when it is not NIGHT

when
Item Solna_TimeOfDay received command
then

if(Solna_TimeOfDay != "NIGHT") {
		 	gMorninglights.members.forEach[ i | 
		 		i.sendCommand(ON)
		 		Thread::sleep(250)	
			 ]
		 logInfo("Lights", "Setting gMorninglights to ON")	
}

end

Works fine.

This rule:
rule “Dusk lights on” //Lights on in the EVENING

when
Item Solna_TimeOfDay received command
then

if(Solna_TimeOfDay == "EVENING") {
		 	gDusklights.members.forEach[ i | 
		 		i.sendCommand(ON)
		 		Thread::sleep(250)	
			 ]
		 logInfo("Lights", "Setting gDusklights to ON")	
}

end
Never fires.

This is what log shows:
2017-12-07 17:40:54.439 [INFO ] [pse.smarthome.model.script.timeofday] - Current time of day is now EVENING
==> /var/log/openhab2/events.log <==
2017-12-07 17:40:54.447 [ome.event.ItemCommandEvent] - Item ‘Solna_TimeOfDay’ received command EVENING
2017-12-07 17:40:54.466 [ome.event.ItemCommandEvent] - Item ‘Solna_TV_Flower’ received command ON
==> /var/log/openhab2/openhab.log <==
2017-12-07 17:40:54.717 [INFO ] [clipse.smarthome.model.script.Lights] - Setting gMorninglights to ON

There should have been a corresponding entry -Setting gDusklights to ON

I don’t know if it helps, but you could have both if-expressions in one rule. There probably shouldn’t be any conflicts between two rules sharing the same trigger, but might be worth a try.

OK! Tried that. Now all rules related to TimeOfDay are in the same rule.
Also tried to change the order of if’s. No change. Only the !=NIGHT rule fires, not the ==EVENING

rule "Morninglights" //Combined all light rules in one
	
when
	Item Solna_TimeOfDay received command
then

if(Solna_TimeOfDay == "EVENING") {
		 	gDusklights.members.forEach[ i | 
		 		i.sendCommand(ON)
		 		Thread::sleep(250)	
			 ]
		 logInfo("Lights", "Setting gDusklights to ON")	
}
else

if(Solna_TimeOfDay != "NIGHT") {
		 	gMorninglights.members.forEach[ i | 
		 		i.sendCommand(ON)
		 		Thread::sleep(250)	
			 ]
		 logInfo("Lights", "Setting gMorninglights to ON")	
}
else

if(Solna_TimeOfDay == "NIGHT") {
		 	gLights.members.forEach[ i | 
		 		i.sendCommand(OFF)
		 		Thread::sleep(250)	
			 ]
		 logInfo("Lights", "Setting gLights to OFF")	
}

end

I believe the issue is that your statement

will always be true as the item is an object and not a string and therefore will always NOT be equal to any string.
Would strongly suggest to read the docs: http://docs.openhab.org/configuration/items.html#state
you could try Solna_TimeOfDay.state in your comparisons instead of only the item name.

You are great Markus!
This seems to work. Now TODO read again the docs (for the XX-time).
I started on OH 1.8 some years ago and have now graduated to 2.1 but it is still an up-hill struggle.

Thanks to the community there always the chance of making progress.

Please keep in mind, that you are actually trying to compare a state to a string (even if the item is a string item). Luckily openHAB does the .toString autoamtically, but to use it correct:

if(MySwitchItem.state == ON)              //MySwitchItem is of type switch, so compare to real state
if(MyContactItem.state == OPEN)           //MyContactItem is of type contact, so compare to real state
if(MyStringItem.state.toString == "Text") //MyStringItem is of type string, so compare string with string
if((MyNumberItem.state as Number) == 15)  //MyNumberItem is of type number, so cast state to number, then compare number with   
if((MyDimmerItem.state as Number) == 45)  //MyDimmerItem is of type dimmer, so cast state to Number, then compare number with  number 
2 Likes

Do I understand the use of the forEach used within rule is to turn ON a light 1 at time in the group ever 250 milliseconds?

Can you simplify it by turning on all lights in the group?

In fact the lights are in a group (named gLights).

Maybe he wants the lights to be switched one after another.
Or maybe the underlying binding is supposed not to fire more than four commands a second.

Udo is right! The lights are operated through Tellstick and I do not want to swamp the Tellstick DUO. Not sure if the binding can queue up commands but this is just to be on the safe side.