Simulate presence by randomly switching lights - any rules existing?

Hi Everyone,

I’m trying to use a modified version of the rule in openhab2, but I’m not having much luck.
Only thing I see in the log is "Configuration model ‘randomlights.rules’ is either empty or cannot be parsed correctly!"
Can anyone see a problem with this?

var Timer tRandomLights = null

rule "Randomly turn on & off lights between 16.00-23.00 if after sunset and nobody is home"
	when
		Time cron "0 0/5 16-23 * * ?"
	then
		if ((Presence.state == OFF) && (now.isBefore((LocalSun_Rise.state as DateTimeType).calendar.timeInMillis) || now.isAfter((LocalSun_Set.state as DateTimeType).calendar.timeInMillis))) {

			// Only turn a light on/off ocasionally
		 	if ((new java.util.Random).nextInt(2) == 1) { 
		 	
		 		// Create a timer with a random value
				var int randomTime = (new java.util.Random).nextInt(300)
				logInfo("org.openhab","Setting random lights timer to " + randomTime + " seconds.")
				tRandomLights = createTimer(now.plusSeconds(randomTime), [|
					var randLightIndex = (new java.util.Random).nextInt(RandomLights.members.size)
			 		var randLightStateCurrent = RandomLights.members.get(randLightIndex).state
			 		var randLightStateNew = if (randLightStateCurrent == ON) OFF else ON
			 		logInfo("org.openhab","Switching light " + RandomLights.members.get(randLightIndex).name + " from " + randLightStateCurrent + " to " + randLightStateNew)
			 		sendCommand(RandomLights.members.get(randLightIndex), randLightStateNew)
		        ])
			}
		}
end

rule "Turn all random lights off at 23.10 if nobody is home"
	when
		Time cron "0 10 23 * * ?"
	then
		if (Presence.state == OFF) {
		 	logInfo("org.openhab","Turning all the random lights off.")
		 	sendCommand(RandomLights, OFF)
		}
end

Regards,
Josh

Does there not follow a Loading model 'xyz.rules'?

2017-05-28 17:15:05.313 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'xyz.rules'
2017-05-28 17:15:05.336 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'xyz.rules' is either empty or cannot be parsed correctly!
2017-05-28 17:15:38.795 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'xyz.rules'

I’m seeing this very often and all the rules are loading without any problems after that WARN message.

Hi @Anquietas86, did you already find a solution for this?
regards,
Markus

No I haven’t, but I’ve since noticed some of my other rules that used to work are not running either, so I think I have a bigger problem on my hands than I first thought.

Hi,

I notice that you all try to simulate presence by randomly switching on/off a (selection of) lights.

I’m not sure if someone trying to break in will be convinced that there is presence, since he might see some illogical patterns. They are smarter than we think.

I still have a Gira HomeServer (which I am gradually making obsolete by moving logic to OpenHAB), and this server applies a better approach. It records all (selected) lights, and as soon as you enable “occupied-home simulation”, it will start to play the recording of e.g. 1 week ago (depending on your config). This makes more sense, because it’s more realistic.

But I haven’t figured out how to do this in OpenHAB… If some people here have a good idea…

Hello!

In my case, turning lights on/off is really random and pretty chaotic. Every night, while watching movies on Plex, lights are turned off while playing movie, and back on while stopped/paused. So, sometimes lights go on and off few times in 5 minutes, and sometimes they are off for hour or more. There isn’t a pattern to be followed, so, doing it randomly is pretty realistic for my use case.

Regarding your question, take a look at Google Calendar Presence Simulator (it’s located in Paper UI -> Add-ons -> Persistence). I haven’t tried it, but I think that’s the same thing you’re talking about.

Best regards,
Davor

1 Like

Thanks! Looking at the information on GitHub, it is exactly doing that. I’ll try to set it up in the near future (need to tackle some other OH stuff first).

This is what I am using and it has been working.

rule "Randomly turn on & off lights between 07:00-22:00 if before or after sunset and if alarm is activated"
	when
		Time cron "0 */20 22-00 * * ?" or
		Time cron "	0 */20 0-7 * * ?"
	then
		if ((now.isBefore((Sunrise_Time.state as DateTimeType).calendar.timeInMillis) || now.isAfter((Sunset_Time.state as DateTimeType).calendar.timeInMillis)) && OutsideEntryLux.state < 30) {

			// Only turn a light on/off ocasionally
		 	if ((new java.util.Random).nextInt(2) == 1) { 
		 	
		 		// Create a timer with a random value
				var int randomTime = (new java.util.Random).nextInt(300)
				logInfo("org.openhab","Setting random lights timer to " + randomTime + " seconds.")
				tRandomLights = createTimer(now.plusSeconds(randomTime)) [|
			 		if(RandomLights.state == ON){sendCommand(RandomLights, OFF)}
			 		else {sendCommand(RandomLights, ON)
			 		}
		        ]
			}
		}
		else {if(RandomLights.state == ON && OutsideEntryLux.state > 40){sendCommand(RandomLights, OFF)}}
end

I did it the same way as you in the beginning, but you could also add them all to one group called light and then later check what type it is, i.e if it switch then ON/Off, also you could send a random value to the dimmer

sendCommand(dimmer, Math::random *100)

Great rule though!

Your 2nd code helped me a lot, but I found I had to put the variable at the start of the rule or get a “cant assign” error with the rule ignored. Maybe that is a difference between OH1 and OH2

Hello all,

This sounds absolutely great but I am stuck at the point to set the AlarmMode.state. What does the Items file and Sitemap look like?

I like to use that to apply for my knx lights and also with the Philips Hue Binding.

I tried with the following:
"Switch AlarmMode “Alarm Mode” in my xyz.items and
"Switch item=AlarmMode label=“Alarm Mode” mappings=[1=“OFF”, 2=“ON”] in xyz.sitemap

What should I do?
many thanks

Hello!

I’m using Number item type for Alarm mode because I have more than just On and Off alarm states (Off, Home, Away, Away - long term). You should be able to use switch as item type, but I’m pretty sure you can’t map 1 and 2 to a Switch item type on your sitemap. If you’re using switch, just avoid mapping part of the sitemap switch definition.

Best regards,
Davor

Hi Community

Here another approach. It fulfills the following requirements:

  • Presence simulation from sunset until 22:11h and from 06:00h until sunrise. Only when item PresenceSim is turned on.
  • There is always at least 1 and max 2 lamps (to preserve energy) turned on out of a definable group of lamps (gPresenceSim)
  • Behaving rather randomly, good enough to convince burglars. But short and easy code
// Presence Simulation ///////////////////////////////////////////////////////////////
rule "After Daylight is gone, turn lights on/off. Max 2, min 1 concurrent"
	when
		Time cron "0 4/30 6-9,16-22 * * ?"  //Every 30 mins triggered
	then
		if ((PresenceSim.state == ON) && !(Sun_Phase.state.equals("DAYLIGHT"))) {	
                var randLightIndex = (new java.util.Random).nextInt(gPresenceSim.members.size) //Randomly pick a light out of the group gPresenceSim
				var randLightNrON = gPresenceSim.members.filter[s|s.state == ON].size		   //Amount of the lights in the group which were switched on
				if (randLightNrON>=2) {
					logInfo("org.openhab",randLightNrON + " Lights out of " + gPresenceSim.members.size + " on. Switching " + gPresenceSim.members.get(randLightIndex).name + " from " + gPresenceSim.members.get(randLightIndex).state + " to OFF")
					sendCommand(gPresenceSim.members.get(randLightIndex), OFF) //When 2 or more are ON, then turn this light off
				}
				if (randLightNrON<=1) {
					logInfo("org.openhab",randLightNrON + " Lights out of " + gPresenceSim.members.size + " on. Switching " + gPresenceSim.members.get(randLightIndex).name + " from " + gPresenceSim.members.get(randLightIndex).state + " to ON")
					sendCommand(gPresenceSim.members.get(randLightIndex), ON) //When 1 or less are ON, then turn this lgiht on
				}        	
		}
end

rule "Turn all lights off at night 22:11 an in the morning at Daylight"
	when
		Time cron "0 11 22 * * ?" or											  // Bed Time
		Channel "astro:sun:local:rise#event" triggered END //Daylight started
	then
		if (PresenceSim.state == ON) {
		 	logInfo("org.openhab","Turning all the random lights off at 22.11h or at Start Daylight")
		    sendCommand(gPresenceSim, OFF)
		}
end
////////////////////////////////////////////////////////////////////////////

Don’t forget to install the astro binding!

Enjoy, Cheers Thomas

5 Likes

Thanks for sharing this code - looks good!

I did a quite similar approach here - but the code is quite old and doesn’t work smooth.

Hello

Was looking for some help with your code, i think i have it working however i have a group of 9 lights, if 2 lights are already on when it randomly picks a new one it will send the off command and the 2 lights already on will stay on, is the correct and if so can it not turn off one of the two already on and turn on a different one. as when i had it running the same two lights stayed on for a number of hours. not sure if i have set it up wrong any help would be amazing thanks

Hi Chris

Yes when 2 lights are on let say Light A and B they will stay on until it randomly picks again light A or B, then it will turn the picked one of. Next time, as there is only 1 on, it will turn the picked one on. eg light E.

This means, the more lights you have in your group the shorter the trigger interval you have to choose for a decent change activity. I have in my group 3 lights and I trigger every 30mins. Maybe for your 9 lights every 5mins would make more sense.
Time cron “0 4/5 6-9,16-22 * * ?” //Every 5 mins triggered

Best
Thomas

Hello

Thanks for your help and code will play around with the number of lights in the group and the time they are triggered.

am i right in thinking if i wanted to extend the time they run for until 11PM i would change 22 to 23?
Time cron “0 4/5 6-9,16-23 * * ?” //Every 5 mins triggered
&
Time cron “0 11 23 * * ?”

many thanks
Chris

My adaption of the rule below unfortunately has some issues, mainly

  • it sometimes works and sometimes doesn’t
  • It switches lights off after 23.00h that are already off.

The first problem probably has something to do with my var setup and using both rules in one file. Unfortunately I am more of a customiser than a developer, so a solution is beyond my skills. Can anyone please help me?

var Timer tRandomLights1 = null
var Timer tRandomLights2 = null

// -————————————————————————————
// RandomLights On between 7-22 when nobody at home
// -————————————————————————————
rule "Randomly turn on off lights 7-22"
	when
		Time cron "42 3/20 7-22 * * ?" 
	then
		if ((JemandZuHause.state == OFF) && (Lock_EG_Danalock.state != OFF) && (Dark.state == ON)) {
			// Only turn a light on/off ocasionally
		 	if ((new java.util.Random).nextInt(2) == 1) { 

		 	
		 		// Create a timer with a random value
				var int randomTime = (new java.util.Random).nextInt(600)
				logInfo("org.openhab","Setting random lights timer to " + randomTime + " seconds.")
				tRandomLights1 = createTimer(now.plusSeconds(randomTime)) [|
					var randLightIndex = (new java.util.Random).nextInt(gRand.members.size)
			 		var randLightStateCurrent = gRand.members.get(randLightIndex).state
			 		var randLightStateNew = if (randLightStateCurrent == ON) OFF else ON
			 		logInfo("org.openhab","Switching light " + gRand.members.get(randLightIndex).name + " from " + randLightStateCurrent + " to " + randLightStateNew)
			 		sendCommand(gRand.members.get(randLightIndex), randLightStateNew)
		        ]
			}
		}

end 


// -————————————————————————————
// All Lights randomly Off after 23.00 when nobody at home
// -————————————————————————————
rule "Randomly turn on off lights after 23"
	when
		Time cron "42 4/6 23 * * ?"
	then
		if ((JemandZuHause.state != ON) && (Lock_EG_Danalock.state != OFF) && (Dark.state == ON)) {
			// Only turn a light off ocasionally
			if ((new java.util.Random).nextInt(2) == 1) { 
		 		// Create a timer with a random value
				var int randomTime = (new java.util.Random).nextInt(240)
				logInfo("org.openhab","Setting random lights timer to " + randomTime + " seconds.")
				tRandomLights2 = createTimer(now.plusSeconds(randomTime)) [|
					var randLightIndex = (new java.util.Random).nextInt(gLights.members.size)
			 		var randLightStateCurrent = gLights.members.get(randLightIndex).state
			 		var randLightStateNew = if (randLightStateCurrent == ON) OFF else OFF
			 		logInfo("org.openhab","Switching light " + gLights.members.get(randLightIndex).name + " from " + randLightStateCurrent + " to " + randLightStateNew)
			 		sendCommand(gLights.members.get(randLightIndex), randLightStateNew)
		        ]
			}
		}
end


Awesome rule. Works perfectly:-)

Hi there, I have been trying to use this rule in Openhab 3 but it throws an unspecific error message. Is there something beside this rule (and the Astro binding) I have to set?
Thanks!