Background
So I setup sending SMS messages from my Raspberry Pi (see article I wrote here) and have a bash script to automate this. The next step is to integrate this into my openHAB server, so I can send SMS messages to my mobile phone when my UPS system (managed by openHAB) is on battery power (e.g. due to a mains network failure).
Installing the EXEC Binding to openHAB
See here. Note especially the requirement to add commands to the whitelist.
Adding a Thing to openHAB
Once the EXEC binding is installed the next step is to setup a thing file to use the binding for sending bash shell commands:
$ nano /etc/openhab/things/exec.things
Add the following line:
Thing exec:command:sendsms [ command="/usr/local/bin/sendsms.sh %2$s", autorun=true, interval=0 ]
Save the file. As you can see this thing is used to pass the command to run the bash shell script to send an SMS and pass the text sent as "%2$s" to the script.
Adding Items to openHAB
Two items are required, one to populate a string with the message that should be passed as text to the exec thing created earlier, and a switch item used to control when the SMS should be sent.
$ nano /etc/openhab/items/sms.items
Add the following lines:
//Send SMS
Switch smssend {channel="exec:command:sendsms:run", autoupdate="false"}
// switch to fire message in smstext
String smstext {channel="exec:command:sendsms:input"}
// input string to pass text to SMS send message
Save the file.
Adding rules to openHAB
A rule is required to pass a message text and control sending the message via the switch item.
$ nano /etc/openhab/rules/sms.rules
Add the following lines:
rule "Send SMS"//Sends an SMS via an external shell script controlled by the EXEC binding thing where smstext is set by another rule and smssend is the switch which is turned ON to send commandswhenItem smstext received commandthenlogInfo("SMS:", "SMS Text sent")smssend.sendCommand(ON)sms_timer1 = createTimer (now.plusSeconds(2),[|smssend.sendCommand(OFF)])end
I also added commands to log the event in the openHAB event log.
Comments
Post a Comment