OpenHAB rules to send email when motion is detected at night

Is it possible to send a security breach mail using openhab when motion detector detects motion?



the rest is the job of a email-client or App

I have implemented something similar using push notifications instead of emails.

I have presence sensing based on OwnTracks and Bluetooth LE beacons, all of the various presence items get placed in a group called “gPresence”.
When everything in gPresence is off for 5 minutes a switch called “HouseEmpty” is turned on:

rule "House Empty ON"
when
	Item gPresence changed from ON to OFF
then
	presenceLock.lock()
	try {
		tim_HouseEmpty=createTimer(now.plusMinutes(5), [ |
				HouseEmpty.sendCommand(ON)
				tim_HouseEmpty = null
		 ])
	} finally {
		presenceLock.unlock()
	}
end

rule "House Empty OFF"
when
	Item gPresence changed from OFF to ON
then
	presenceLock.lock()
	try {
		if(tim_HouseEmpty !== null) {
				tim_HouseEmpty.cancel()
				tim_HouseEmpty = null
				logInfo("Presence","HouseEmpty Timer cancelled")
			}
		HouseEmpty.sendCommand(OFF)
	} finally {
		presenceLock.unlock()
	}
end

All of my motion sensors get placed in a group called “gMotionSensors”, this rule sends a Pushover notification if one of them is triggered while the house is empty:

rule "Empty House Security"
when
	Item gMotionSensors changed from OFF to ON
then
	if(HouseEmpty.state == ON) {
		logInfo("Presence","Motion detector tripped in empty house")
		sendPushoverMessage(pushoverBuilder("House Motion Alarm").withPriority(1))
	}
end

Works well for me, the only gripe I have is sometimes the bluetooth beacons aren’t sensed by the BLE scanner quickly enough, since it’s in a cupboard near the back of the house. This causes the occasional false alarm. I could fix this by adding another BLE scanner near the front door, which I will do soon.

1 Like

Thank you . Your post was helpful.
I have written rules according to what you have told me…
rule "Empty House Security" when Item security changed from OFF to ON if(security.state == 1) { /*logInfo("Presence","Motion detector tripped in house") sendPushoverMessage(pushoverBuilder("House Alarm").withPriority(1))*/ sendMail("*********@gmail.com", "House alarm", "Motion detector tripped in house") }

Basically wha it supposed to do is, when my motion sensor detects a motion, it will send a value 1. openhab subscribes it using mqtt, and changes the switch in basic UI and also should send mail.

The code in my items file looks like this
Switch security "security" <slider> ["Switchable"] {mqtt="<[mosquitto:security:command:ON:1],<[mosquitto:security:command:OFF:0]"}
And my sitemap code is this.

`sitemap b label=“Main Menu” {
Frame label=“Room” icon=light {

	Switch item=cdilight label="Light"
	Text item=Office_temp label="Temperature"
	Switch item=security
	
}

}`

the issue I’m facing is I can subscribe this in openhab but mail is not sent. Is there something I’m missing…?
Am i supposed to change anything else other than mail cong file?

If the pushover message is sent but not the email thene there is a problem with your mail config.
What does the log show?

And please hide your email address…

Also, for inbound MQTT you need a state change not a command:

Switch security "security" <slider> ["Switchable"] { mqtt="<[mosquitto:security:state:MAP(0OFF1ON.map)]" }

map file in transform folder:
0OFF1ON.map

0=OFF
1=ON
1 Like

I have edited items file as you have told…

 Switch security "security" <slider> ["Switchable"] {mqtt="<[mosquitto:security:command:ON:1],<[mosquitto:security:command:OFF:0],<[mosquitto:security:state:MAP(0OFF1ON.map)]" } 

logs are not showing any activity regarding mail…

2018-06-21 14:50:22.469 [ome.event.ItemCommandEvent] - Item 'security' received command OFF
2018-06-21 14:50:23.146 [ome.event.ItemCommandEvent] - Item 'security' received command ON
2018-06-21 14:50:23.146 [vent.ItemStateChangedEvent] - security changed from OFF to ON
2018-06-21 14:50:25.142 [ome.event.ItemCommandEvent] - Item 'security' received command ON
2018-06-21 14:50:25.342 [ome.event.ItemCommandEvent] - Item 'security' received command ON
2018-06-21 14:50:26.146 [ome.event.ItemCommandEvent] - Item 'security' received command ON
2018-06-21 14:50:27.089 [ome.event.ItemCommandEvent] - Item 'security' received command ON
2018-06-21 14:50:28.551 [ome.event.ItemCommandEvent] - Item 'security' received command OFF
2018-06-21 14:50:28.551 [vent.ItemStateChangedEvent] - security changed from ON to OFF

This is how I have done it. I think there is an issue with mail config . any ideas as to what might be the issue…?

Switch security "security" <slider> ["Switchable"] { mqtt="<[mosquitto:security:state:MAP(0OFF1ON.map)]" } 

What happens in the openhab.log when you trigger the rule?

# The SMTP server hostname, e.g. "smtp.gmail.com"
hostname=smtp.gmail.com

# the SMTP port to use (optional, defaults to 25 (resp. 587 for TLS/SSL))
port=587

# the username and password if the SMTP server requires authentication
username=*********@gmail.com
password=*****

# The email address to use for sending mails
from=**********@gmail.com

# set to "true", if STARTTLS is enabled (not required) for the connection
# (optional, defaults to false)
#tls=

# set to "true", if SSL negotiation should occur on connection
# do not use both tls=true and ssl=true
# (optional, defaults to false)
#ssl=

# set to "true", if POP before SMTP (another authentication mechanism)
# should be enabled. Username and Password are taken from the above
# configuration (optional, default to false)
popbeforesmtp=false

# Character set used to encode message body
# (optional, if not provided platform default is used)
#charset=

Should i link the gmail account from which I’m sending the alert message with openhab??

tls=true
#popbeforesmtp

It’s the same thing over and again.

2018-06-21 14:50:22.469 [ome.event.ItemCommandEvent] - Item 'security' received command OFF
2018-06-21 14:50:23.146 [ome.event.ItemCommandEvent] - Item 'security' received command ON
2018-06-21 14:50:23.146 [vent.ItemStateChangedEvent] - security changed from OFF to ON
2018-06-21 14:50:25.142 [ome.event.ItemCommandEvent] - Item 'security' received command ON
2018-06-21 14:50:25.342 [ome.event.ItemCommandEvent] - Item 'security' received command ON
2018-06-21 14:50:26.146 [ome.event.ItemCommandEvent] - Item 'security' received command ON
2018-06-21 14:50:27.089 [ome.event.ItemCommandEvent] - Item 'security' received command ON
2018-06-21 14:50:28.551 [ome.event.ItemCommandEvent] - Item 'security' received command OFF
2018-06-21 14:50:28.551 [vent.ItemStateChangedEvent] - security changed from ON to OFF

I don’t think the rules are getting triggered…

not events.log the other one, openhab.log

rule "Empty House Security"
when
    Item security changed from OFF to ON
then
    if(security.state == 1) {
        //logInfo("Presence","Motion detector tripped in house")
        //sendPushoverMessage(pushoverBuilder("House Alarm").withPriority(1))
        sendMail("*********@gmail.com", "House alarm", "Motion detector tripped in house")
    }
end
2018-06-21 17:16:26.765 [WARN ] [.core.transform.TransformationHelper] - Cannot get service reference for transformation service of type MAP
2018-06-21 17:16:26.765 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'security'
2018-06-21 17:16:27.587 [WARN ] [.core.transform.TransformationHelper] - Cannot get service reference for transformation service of type MAP
2018-06-21 17:16:27.587 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'security'
2018-06-21 17:16:29.896 [WARN ] [.core.transform.TransformationHelper] - Cannot get service reference for transformation service of type MAP
2018-06-21 17:16:29.896 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'security'
2018-06-21 17:16:30.096 [WARN ] [.core.transform.TransformationHelper] - Cannot get service reference for transformation service of type MAP
2018-06-21 17:16:30.096 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'security'
2018-06-21 17:16:32.076 [WARN ] [.core.transform.TransformationHelper] - Cannot get service reference for transformation service of type MAP
2018-06-21 17:16:32.076 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'security'
2018-06-21 17:16:32.277 [WARN ] [.core.transform.TransformationHelper] - Cannot get service reference for transformation service of type MAP
2018-06-21 17:16:32.277 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'security'
2018-06-21 17:16:33.342 [INFO ] [io.openhabcloud.internal.CloudClient] - Shutting down openHAB Cloud service connection

This what openhab log shows. any help on what this is supposed to mean??

Have you installed the MAP transformation Addon?

Now I did.
what exactly does this do??
I’m kinda new to this. Please pardon my stupid mistakes…

See here: Map - Transformation Services | openHAB

1 Like
2018-06-25 12:16:38.859 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'b.items'
2018-06-25 12:16:39.483 [INFO ] [thome.model.lsp.internal.ModelServer] - Started Language Server Protocol (LSP) service on port 5007
2018-06-25 12:16:39.969 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'b.rules'
2018-06-25 12:16:40.121 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'b.sitemap'
2018-06-25 12:16:40.708 [INFO ] [basic.internal.servlet.WebAppServlet] - Started Basic UI at /basicui/app
2018-06-25 12:16:40.720 [INFO ] [arthome.ui.paper.internal.PaperUIApp] - Started Paper UI at /paperui
2018-06-25 12:16:40.755 [INFO ] [panel.internal.HABPanelDashboardTile] - Started HABPanel at /habpanel
2018-06-25 12:16:40.846 [INFO ] [penhab.io.transport.mqtt.MqttService] - MQTT Service initialization completed.
2018-06-25 12:16:40.847 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection 'mosquitto'
2018-06-25 12:16:41.247 [WARN ] [mqtt.internal.MqttPersistenceService] - mqtt-persistence:broker
2018-06-25 12:16:41.247 [WARN ] [mqtt.internal.MqttPersistenceService] - mqtt-persistence:topic
2018-06-25 12:16:41.247 [WARN ] [mqtt.internal.MqttPersistenceService] - mqtt-persistence:message

This is logs
How do I know if rules are triggered

Use the rule that Vincent provided and enable the logInfo line and check the log

rule "Empty House Security"
when
    Item security changed from OFF to ON
then
    if(security.state == 1) {
        logInfo("Presence", "Motion detector tripped in house, sending email")
        sendMail("*********@gmail.com", "House alarm", "Motion detector tripped in house")
    }
end

on a side note: If you don’t use MqttPersistenceService, uninstall it


2018-06-25 15:25:34.102 [INFO ] [panel.internal.HABPanelDashboardTile] - Started HABPanel at /habpanel
2018-06-25 15:25:34.154 [INFO ] [penhab.io.transport.mqtt.MqttService] - MQTT Service initialization completed.
2018-06-25 15:25:34.155 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection 'mosquitto'
2018-06-25 15:25:34.670 [WARN ] [mqtt.internal.MqttPersistenceService] - mqtt-persistence:broker
2018-06-25 15:25:34.670 [WARN ] [mqtt.internal.MqttPersistenceService] - mqtt-persistence:topic
2018-06-25 15:25:34.671 [WARN ] [mqtt.internal.MqttPersistenceService] - mqtt-persistence:message
2018-06-25 15:37:14.918 [WARN ] [rm.AbstractFileTransformationService] - Could not transform 'Ăż' with the file '0OFF1ON.map' : Target value not found in map for 'Ăż'
2018-06-25 15:37:14.919 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'security'
2018-06-25 15:39:20.198 [WARN ] [rm.AbstractFileTransformationService] - Could not transform 'ĂŚ' with the file '0OFF1ON.map' : Target value not found in map for 'ĂŚ'
2018-06-25 15:39:20.199 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'security'
2018-06-25 15:39:40.310 [WARN ] [rm.AbstractFileTransformationService] - Could not transform 'ñ' with the file '0OFF1ON.map' : Target value not found in map for 'ñ'
2018-06-25 15:39:40.310 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'security'
2018-06-25 15:39:49.466 [WARN ] [rm.AbstractFileTransformationService] - Could not transform 'æ' with the file '0OFF1ON.map' : Target value not found in map for 'æ'
2018-06-25 15:39:49.466 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'security'
2018-06-25 15:41:12.018 [WARN ] [rm.AbstractFileTransformationService] - Could not transform 'Ëś' with the file '0OFF1ON.map' : Target value not found in map for 'Ëś'
2018-06-25 15:41:12.019 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'security'
2018-06-25 15:41:30.211 [WARN ] [rm.AbstractFileTransformationService] - Could not transform 'Ăż' with the file '0OFF1ON.map' : Target value not found in map for 'Ăż'
2018-06-25 15:41:30.212 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'security'
2018-06-25 15:41:43.140 [WARN ] [rm.AbstractFileTransformationService] - Could not transform 'Ăż' with the file '0OFF1ON.map' : Target value not found in map for 'Ăż'
2018-06-25 15:41:43.141 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'security'
2018-06-25 15:43:23.096 [WARN ] [rm.AbstractFileTransformationService] - Could not transform 'Ăż' with the file '0OFF1ON.map' : Target value not found in map for 'Ăż'
2018-06-25 15:43:23.097 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'security'
2018-06-25 15:44:05.122 [WARN ] [rm.AbstractFileTransformationService] - Could not transform 'ñ' with the file '0OFF1ON.map' : Target value not found in map for 'ñ'
2018-06-25 15:44:05.123 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'security'
2018-06-25 15:44:17.841 [WARN ] [rm.AbstractFileTransformationService] - Could not transform 'ĂĽ' with the file '0OFF1ON.map' : Target value not found in map for 'ĂĽ'
2018-06-25 15:44:17.842 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'security'
2018-06-25 15:44:18.985 [WARN ] [rm.AbstractFileTransformationService] - Could not transform 'ø' with the file '0OFF1ON.map' : Target value not found in map for 'ø'
2018-06-25 15:44:18.986 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'security'
2018-06-25 15:44:19.986 [WARN ] [rm.AbstractFileTransformationService] - Could not transform 'Ă°' with the file '0OFF1ON.map' : Target value not found in map for 'Ă°'
2018-06-25 15:44:19.986 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'security'

I’m using mqtt persistance
Any help on this??

Why are you using mqtt persistence?