Help simplifying rule

Hi folks,

I have what is turning out to be a bit of a dog’s breakfast with rules and hoping someone can show that there is a simpler way.

I have presence detection which switches a switch called Present (Yes, plagiarised from here :slight_smile: )
I have a switch called NightState which is controlled by Astro
I have a rule which sets a switch called BedTime at 23:00

Now, what I want is when it’s night certain lamps (g_Lamps) turn on but if no one is home then g_Lamps_NoOneHome must turn on.
At 23:00 certain lamps must turn off and only leave certain ones one (Hallway lamp)
At daytime all lamps must turn off unless someone is home then cupboard LEDs must stay on.

At the moment I have it sort of working but it’s weaving in and out of code which is not clean. Any change to it and it breaks. It’s also quite slow (RPi). I am sure I can speed it up with groups.

As much as this gets controlled from the time or night time, I also want it to be controlled from the Present state. i.e. if it’s night and we leave, go into night-no-one-home mode.
When we return it needs to go into night-someone-home mode.

All very simple :slight_smile:

rule "Night has started"
when
   Item NightState changed from OFF to ON
then

	if (AutoDayNight.state == OFF){
		logInfo("Night Rule", "It's getting dark. Auto Day/Night disabled")
		return
	} 

	//someone home
	if (Present.state == ON) {
		proxyNightPresent.sendCommand(ON)
	}
	else {
		proxyNightNotPresent.sendCommand(ON)
	}
end



rule "Night has ended"
when
   Item NightState changed from ON to OFF
then

	if (AutoDayNight.state == OFF){
		logInfo("Night Rule", "Getting light. Auto Day/Night disabled")
		return
	}


	logInfo("Night Rule", "It's getting Light!")

	g_Lamps.sendCommand(OFF)
	g_LampsPC.sendCommand(0)
	g_Lanterns.sendCommand(0)   
	
	if (Present.state == ON){
		g_WetroomLEDS.sendCommand(25)
		g_KitchenCupboards.sendCommand(25)
		
	}	
	else {
		g_WetroomLEDS.sendCommand(0)
		g_KitchenCupboards.sendCommand(0)
	}
end


rule "Night state and someone is home"
when Item proxyNightPresent received command ON then

	if (NightState.state !== NULL && NightState.state != ON){
		return
	}
	logInfo("proxyNightPresent", "Night, someone home.")
	g_HallwayLamps.sendCommand(ON)
	g_Lanterns.sendCommand(ON)
	g_Lamps.sendCommand(ON)
	g_LampsPC.sendCommand(50)
	g_WetroomLEDS.sendCommand(100)
	g_KitchenCupboards.sendCommand(100)
		
	proxyNightPresent.sendCommand(OFF)
  
	   
end 

rule "Night state and nobody is home"
when Item proxyNightNotPresent received command ON then
	if (NightState.state !== NULL && NightState.state != ON){
		return
	}

	logInfo("proxyNightPresent", "Night, nobody home.")

	g_AwaySW.sendCommand(ON)
	g_AwayPC.sendCommand(25)
	
	
	proxyNightNotPresent.sendCommand(OFF)
	   
end 

I hope that you can easily adapt from here:

and here:

Thanks for the hints but I have read them. I think I have simplified it a huge amount but there is still scope for removing a rule.

Group:Switch:AND(ON, OFF) g_On1 
Group:Switch g_On2
Group:Switch:NOR(ON, OFF) g_On3
Group:Switch g_On4


Group:Switch g_NightStateX (g_On1, g_On3)
Group:Switch g_PresentStateX (g_On1, g_On3)

rule "Test "
when Item g_NightStateX changed or Item g_PresentStateX changed
then

	 if (
			g_PresentStateX.state == OFF
		&& 	g_NightStateX.state == ON) {
			g_On2.sendCommand(ON)
		}
		else {
			g_On2.sendCommand(OFF)
		}

	 if (
			g_PresentStateX.state == ON
		&& 	g_NightStateX.state == OFF) {
			g_On4.sendCommand(ON)
		}
		else {
			g_On4.sendCommand(OFF)
		}
end 

The above works mostly as I need it. I still need a few rules but they are simpler than they used to be.
I continue :slight_smile: