Simulate presence by randomly switching lights - any rules existing?

@davorf

Could you explain you determine your “short term away” and your “long term away” items? I really like that feature and would love to implement it.

Thanks.

Hello!

Not sure if this is the thing you’ve asked about, but this is what each alarm mode does:

  • Off - pretty self-explanatory :slight_smile:
  • Home - After switching it on, it’s active instantaneously; It reacts only on door/window sensors, ignores motion sensors, and sounds an alarm instantaneously if door/window sensor changes from CLOSE to OPEN
  • Away (Short-term) - After switching it on, it activates one minute after changing General_Alarm_State item (so you have time to leave home without activating alarm); It reacts on both, door/window sensors and motion sensors; After activating, it turns all the lights off; If door/window or motion sensors are activated and it’s past sunset, it turns some lights on; If door/window or motion sensors are activated it waits for a minute (so you have time to deactivate it), and then sounds an alarm
  • Away (Long-term) - Behaves the same as Short-term, but it activates presence simulator (turning lights on/off at semi-random interval) after sunset, and deactivates presence simulator at dedicated time (semi-random time between 0:30 and 3:00, depending on the day of the week and a random additional time - 5 to 30 minutes)

All 3 modes (except off) check if the doors/windows are open in the moment of the activation (home mode instantaneously, other two after they get activated - one minute after turning it on), and send a notification if something is open after activating, so you don’t leave your doors/windows open.

I hope this explanation helps you implementing it.

Best regards,
Davor

That does answer some questions, but I do have one still remaining.

Are your short-term and long-term alarm states activated by different codes/switches? Or do you designate a long-term by the time elapsed since you set the alarm?

For example:

Set X = 1200 (minutes)
Alarm.sendcommand(“Arm”)
Start alarm_timer

when alarm_timer <= X
then alarm_type = 'short-term’
else alarm_type = ‘long-term’

Hello!

No, it’s just the name of the alarm mode. I choose the type of the alarm with Selection widget (although there are some other ways to set it too - NFC for short-term away alarm and turning all alarms off, Night mode for Home alarm etc). But I start the long-term away alarm by explicitly choosing it from the selection widget.

Best regards,
Davor

Perhaps I’m trying to make this harder then it should be but I’d like to do something slightly different which I’m having trouble working out. I’d like to have a variable that is assigned a random time each morning at 12:05am such that the random time is between the astro binding of civilDusk and 11pm. Then I’d like to assign that variable to a text item so I can display it in the sitemap so I can check each day the random time. Then I’d like to rules file to run a rule each day that the current time matches the random time which then turns on lights. Your code helps me think through how I might do that and the alarmmode idea is a good one I didn’t think of so I can turn this function on and off with the app.

However, I’m having trouble working through the first 2 steps of this:

  • create a random time - the util.random shows up several times in your code so I’m not sure what I should be using to just create a random time I can use to compare to [now] (see below)

  • set up the rule to fire when now = my random time, would it be:
    when
    Time now = myrandomtimevariable

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