Intruder alarm

Hey guys,

this is the intruder part of this thread: Time of Sunrise/Sunset - to make it easier for anybody to search for it and implement it too.


The goal is to have an invasion alarm:

rule "Break-In Alarm"
	when
		Item Alarm_Invasion received update ON
	then
		sendCommand(GroupLight, 100) //Turn on all Lights
			logInfo("Invasion-Alarm","Invasion alarm - turning all Lights on")
		
		// send notifications
		sendTelegram("BotGroup", "Intruder alarm was triggered!!!")
		
		//play sounds?
end

One of the problems I do have is to have the alarm triggered, when the status of an item in the group “GroupDetectorImportant” changes. Dont know if the following could work:

rule "Invasion Detection"
	when
		Item GroupDetectorImportant received update
	then
		if (Away_Status == ON) {
			sendCommand(Alarm_Invasion, ON)
				logInfo("InvasionDetection","Sensor changed, Away Status is On - ALARM!!!")
		}
		if (Sleeping_Status == ON) {
			sendCommand(Alarm_Invasion, ON)
				logInfo("InvasionDetection","Sensor changed, Sleeping Status is On - ALARM!!!")
		}
		if (Away_Status_Smart == ON) {
			sendTelegram("BotGroup", "Invasion alarm - no smartphones are online; are you home?")
				logInfo("InvasionDetection","Sensor changed, Smart Away Status is On - sending notification!")
		}
		else {
				logInfo("InvasionDetection","Sensor changed, but nothing to do")
		}
end

Would be nice if I could answer on that telegram-message to finally trigger the alarm or not. But I could not find anything to read out these messages.


Next thing is a sleep detection:

rule "Sleep Detection"
	when
		Item GroupLight received update OFF
	then
		if (NightState == ON) {
			sendCommand(Sleeping_Status, ON)
				logInfo("Sleep Detection","It is nighttime - switching sleep status")
		}
		else {
				logInfo("Sleep Detection","It is daytime - no sleep detection")
		}
end

This does not work - it does not trigger with all lights off and the nightstate being on… Dont know why. I just want the Sleeping_Status to be on when all lights are off and the nightstate is on.

Here is also the nightstate (found it in the wiki (at least I think so)):

rule "Update NightState"
when
    Item SunElevation changed
then
    if(SunElevation.state >  15){
        if(NightState.state != OFF) {
        	postUpdate(NightState, OFF)
        }
    } 
    else {
        if(NightState.state != ON) { 
        	postUpdate(NightState, ON)
        }
    }
end

This is to automatically switch the Sleeping_Status off. Probably I have to integrate a timer here to turn that status off after 1 minute or so:

rule "Wake Up Detection"
	when
		Item GroupLight received update ON
	then
		sendCommand(Sleeping_Status, OFF)
			logInfo("Wake Up Detection","It is nighttime - switching sleep status")
end

And here is an away detection over smartphones, but I am still very unsure about it. So I just want to use it as backup and only send a telegram-message. I also built a timer in there to check if somebody is really away and not only taking the bin down or so:

var Timer AwayDetectionTimer = null

rule "Away Detection"
	when
		Item SmartphoneAndy received update or
		Item SmartphoneJessy received update
	then
		if (SmartphoneAndy == OFF && SmartphoneJessy == OFF) {
			AwayDetectionTimer = createTimer(now.plusSeconds(600)) [|
				if (SmartphoneAndy == OFF && SmartphoneJessy == OFF) {
					sendCommand(Away_Status_Smart, ON)
						logInfo("AwayDetection","")
				}
				else {
						logInfo("AwayDetection","")
				}
			]
		}
		if (SmartphoneAndy == ON || SmartphoneJessy == ON) {
			if (Away_Status_Smart == ON) {
				sendCommand(Away_Status_Smart, OFF)
					logInfo("AwayDetection","")
			}
			else {
					logInfo("AwayDetection","")
			}	
		}
end

Later I also want to add a timer to trigger the alarm (want the basics working first) and so on. The idea is a keypad in HABpanel to deactivate the away-status and also it does not make sense to activate the alarm instantly after a sensor changed. It would also trigger if you open the maindoor - no time to deactivate the status then.

Also I do think it makes sense to have the sleeping status only trigger the alarm instant at the windows, because you could also come home late.

Thank you very much in advance!

Greetings
Andy

1 Like

Hey,

I get some of it working now, I want to share the code here.

Sleep Detection and Wake Up Detection; Sleeping Status turns on, when no lights are on and the Night State is on, it turns off with 5 seconds delay after a light turned on:

rule "Sleep Detection + Wake Up Detection"
	when
		Item GroupLight changed
	then
		if (GroupLight.state == 0) {
			if (NightState.state == ON && Sleeping_Status.state != ON) {
				sendCommand(Sleeping_Status, ON)
					logInfo("Sleep Detection","It is nighttime - switching sleep status")
			}
			else {
				if (NightState.state == OFF) {
					logInfo("Sleep Detection","It is daytime - no sleep detection")
				}
				if (Sleeping_Status.state == ON) {
					logInfo("Sleep Detection","Sleeping-Status is already on")
				}
			}
		}
		if (GroupLight.state != 0) {
			if (Sleeping_Status.state != OFF) {
				Thread::sleep(5000)
				sendCommand(Sleeping_Status, OFF)
					logInfo("Wake Up Detection","Light turned on - wake up detected")
			}
			else {
					logInfo("Wake Up Detection","Sleeping Status is already off, nothing to do")
			}
		}
end

It is triggered many times at once, guess I need to build a timer in there to not have that behavior:

07:35:54.487 [INFO ] [marthome.event.ItemStateChangedEvent] - kitchen_light_ceiling3 changed from 100 to 0
07:35:54.491 [INFO ] [ome.event.GroupItemStateChangedEvent] - GroupKitchenLightCeiling changed from 100 to 0 through kitchen_light_ceiling3
07:35:54.494 [INFO ] [marthome.event.ItemStateChangedEvent] - kitchen_light_ceiling1 changed from 100 to 0
07:35:54.498 [INFO ] [ome.event.GroupItemStateChangedEvent] - GroupKitchenLight changed from 100 to 0 through GroupKitchenLightCeiling
07:35:54.560 [INFO ] [thome.model.script.Wake Up Detection] - Sleeping Status is already off, nothing to do
07:35:54.567 [INFO ] [thome.model.script.Wake Up Detection] - Sleeping Status is already off, nothing to do
07:35:54.573 [INFO ] [thome.model.script.Wake Up Detection] - Sleeping Status is already off, nothing to do
07:35:54.578 [INFO ] [thome.model.script.Wake Up Detection] - Sleeping Status is already off, nothing to do

I got one of my two Xiaomi gateways in the hall right next to the door, so I thought that it is fancy to integrate a “alarm-light” there:

rule "Alarm Status Light"
	when
		Item Away_Status changed or
		Item Sleeping_Status changed
	then
		if (Away_Status.state == ON) {
			sendCommand(Gateway_Brightness, 20)
			sendCommand(Gateway_Color, "358,100,100")
				logInfo("Away Status Light","Away Status turned on - turning light on")
			if (Sleeping_Status.state == OFF) {	
				sendCommand(Gateway_SoundVolume, 5)
				sendCommand(Gateway_Sound, 3)
				Thread::sleep(1000)
				sendCommand(Gateway_Sound, 10000)		
			}		
		}
		if (Sleeping_Status.state == ON) {
			sendCommand(Gateway_Brightness, 3)
			sendCommand(Gateway_Color, "358,100,100")
				logInfo("Away Status Light","Sleeping Status turned on - turning light on")
		}
		else {
			sendCommand(Gateway_Brightness , 0)
			sendCommand(Gateway_Color, "0,0,0")
				logInfo("Away Status Light","Away Status turned off - turning light off")
		}
end

Last I added a “away-button” right next to the front door, which also helps when you come home late and need to deactivate the detection with the Sleeping Status:

rule "Away Button"
	when
		Channel "XXX:button" triggered
	then
		var actionName = receivedEvent.getEvent()
		switch(actionName) {
			case "SHORT_PRESSED": {
				if (Away_Status.state != OFF) {
					sendCommand(Away_Status, OFF)
						logInfo("Away Button","Button pressed & Status is on - turning off")
				}
				else {
					sendCommand(Away_Status, ON)
						logInfo("Away Button","Button pressed & Status is off - turning on")
				}
			}
			case "DOUBLE_PRESSED": {
				//Turn off the alarm, while the alarm-timer is still active, after front door opened
			}
		}
end

Next I will work on the sensor detection of “GroupDetectorImportant” and the Timer in the Sleep-/Wake Up-Detection to not have it activated all the time.

I hope my little attempt here is doing okay :slight_smile:

Greetings

I ran into a problem. I have all my important (windows/doors to outside) in a group together - “GroupDetectorImportant”.

The state of that group is always “Null”. For example is the state of “office_det_window_Status” “CLOSED” or “OPEN”.

I tried the following rule as test:

rule "Invasion Test"
	when
		Item GroupDetectorImportant received update OPEN
	then
		logInfo("InvastionTest","Group received update")
end

But yeah it is not working, because that Group seems to never receive an update; my item:

Group						GroupDetectorImportant

//-> Detector
//Window - Xiaomi Window Switch
Contact		office_det_window_Status				"[MAP(sensorstatus.map):%s]"				<window>				(GroupOfficeDetector,GroupDetectorImportant)									{channel="mihome:sensor_magnet:158d00019df1c5:isOpen"}
Number		office_det_window_AlarmTimer														<clock>																									{channel="mihome:sensor_magnet:158d00019df1c5:isOpenAlarmTimer"}
DateTime	office_det_window_LastOpened			"[%1$td.%1$tm.%1$tY  %1$tH:%1$tM:%1$tS]"	<clock-on>																								{channel="mihome:sensor_magnet:158d00019df1c5:lastOpened"}
Number		office_det_window_Battery				"Batteriestand"								<battery>				(Battery)																		{channel="mihome:sensor_magnet:158d00019df1c5:batteryLevel"}
Switch		office_det_window_BatteryLow			"Niedrige Batterie"							<energy>																								{channel="mihome:sensor_magnet:158d00019df1c5:lowBattery"}

Is that mapping maybe a problem?..

Hey DaAndy,

This is a very old thread I know but did you get it working? I’m trying to create something similar myself

You need to do something like this:

rule "LastUpdate time"
when
        Member of gLastUpdate received update or
        Member of gLastUpdate changed or
        Member of gZigbee changed 
then
        postUpdate(triggeringItem.name+"_LastUpdate", now.toString)


You need to see if the member of the group gets the change or update not see if the group gets the update or change (because that will never happen)

1 Like

Well, it can, but it depends hows the Group is configured; you’d need to specify an aggregation function. e.g. to detect any Contact changing to OPEN when all were CLOSED (useful for alarms) you could have Group:Contact:OR(OPEN,CLOSE).
'Member of' triggers are generally more useful,but weren’t available 4 years ago