Hi Tom
The answer to your question of can I confirm I got this working, is both a a yes and a no. The code above works. The 1 issue I did struggle with, which I didnt know at the time, was that the VAL lines (for some reason) had to be initially declared in the top of my RULES (literally at the very top of the rules file). This is listed in the documentation https://docs.openhab.org/configuration/rules-dsl.html#the-syntax on the “Variable Declarations” area.
So you would need to put these at the TOP of the rules file:
val contactNumber = ALARMDATA.state.toString.substr(1,3)
val stateOf = ALARMDATA.state.toString.substr(3,1)
If you dont put them there, you get an error in the log about parsing the rules file on the line numbers where the VAL’s are declared.
So that was issue 1 for me… Issue 2 for me was that in the examples above, I listed that my sensors very simply separated Open/Closed/Tamper via the use of A, B and C… so the code example above is reading if there is an A, B or C sent via the sensors and then updating the contact_XXXXX site map entry… However, I ended up with a bunch of sensors that dont work with A, B and C, but give a random code e.g.
Open - USH3438
Closed - AUF8234
Tamper - IAS0238
Obviously, there is NO simple way, when you have multiple sensors with random codes like the above to progmatically use a simple 1 off rule on its own to pick out the difference between multiple sensors… Well, not easily.
I did look at using a MAP file, that would list each unique code and what that code mapped to… But I found an issue with … I cant recall exactly what it was, but somewhere along the line, I found that my variable couldnt work correctly because of the translation… I cannot remember it it was within the ITEMS, RULES or SITEMAP I had a problem… but I did.
So, I have ended up with having to define each code from the sensor and then the action taken for each code, within my rules file… which does look like a lot of code and it could probably be cleaned up by someone with more code knowledge than myself. but I dont have a huge rules file anyway and the code does work.
ITEMS - Creates a variable to pull in the code from a sensor, via a MQTT source
String ALARMSENSOR1 {mqtt="<[mybroker:tele/RFBridge/RESULT:state:JSONPATH($.RfReceived.Data)]"}
ITEMS - I created a variable which is On/Off which is basically is the alarm On or Off (is someone in the house)
Switch HOUSEOCCUPIED “Status”
ITEMS - Creates a variable for use within the sitemap, that will display the Open/Closed/Tamper status
String FRONTDOOR "Front Door [%s]" <doorsensor> (Alarmsensors)
ITEMS - I also wanted to record the LAST sensor triggered information, what time it was triggered and the alert type (Open/Closed/Tamper etc)
String ALERT "Sensor Triggered [%s]" <alerticon>
String ALERTTIME "Alert Time [%s]" <alerticon>
String ALERTTRIGGER "Alert Trigger [%s]" <alerticon>
SITEMAP - As simple as displaying the variables you created above
Frame label="Security" {
Switch item=HOUSEOCCUPIED mappings=["ON"="Armed","OFF"="Disarmed"]
Text label="Alarm System" icon="alarm1"{
Frame label="Last Alert" {
Text item=ALERT
Text item=ALERTTIME
Text item=ALERTTRIGGER
}
Frame label="Sensor Status" {
Text item=FRONTDOOR
Text item=REARDOOR
Text item=SENSORWINDOW
}
Frame {
Text item=MOTIONSENSOR1
Text item=LANDINGSENSOR
}
}
}
RULES - At the top of my rules file I have imported Java date/time
import java.text.SimpleDateFormat
import java.util.Date
RULES - As for a rule to work with 1 sensor, because they are using different codes, each rule would look like the following
For my example below, use the following example as being sent by the sensor:
Open - USH3438
Closed - AUF8234
Tamper - IAS0238
The code will say that IF the alarm is ARMED, and the sensor sends Open/closed, then I DO want a telegram notification message sent to my phone with the details of that sensor & the date and time, along with updating the ALERT, ALERTTRIGGER and ALERTTIME variables.
If the alarm is DISARMED, and the door is Open/Closed then it wont send a notification to my phone OR update the ALERT, ALERTTRIGGER and ALERTTIME variables.
Either of the above actions will update the sitemap text variables to show the current state of the sensor as Open/Closed.
For Tamper events, I have chosen to recieve a notification via Telegram, even if the alarm is disarmed.
So yes, I have ended up with one rule like the below for each sensor, which is messy, but it works, my rules file is not very large anyway and I thought I would re-visit it one day in the future to see if there was a better way to do this, when working with sensors that send codes you cannot easily delinite programmatically.
rule "Alarm sensor 1 - Front Door"
when
Item ALARMSENSOR1 received update
then
if (ALARMSENSOR1.state == "AUF8234" && HOUSEOCCUPIED.state == ON) { FRONTDOOR.postUpdate("Closed")
sendTelegram("TELEGRAMBOTNAME", "Alarm - Front Door Closed" + Current_DateTime.state.format(" %1$td/%1$tm/%1$tY at %1$tH:%1$tM"))
ALERT.postUpdate("Front Door")
ALERTTRIGGER.postUpdate("Closed")
ALERTTIME.postUpdate(Current_DateTime.state.format("%1$td/%1$tm/%1$tY at %1$tH:%1$tM")) }
if (ALARMSENSOR1.state == "AUF8234" && HOUSEOCCUPIED.state == OFF) { FRONTDOOR.postUpdate("Closed") }
if (ALARMSENSOR1.state == "USH3438" && HOUSEOCCUPIED.state == ON) { FRONTDOOR.postUpdate("Open")
sendTelegram("TELEGRAMBOTNAME", "Alarm - Front Door Open" + Current_DateTime.state.format(" %1$td/%1$tm/%1$tY at %1$tH:%1$tM"))
ALERT.postUpdate("Front Door")
ALERTTRIGGER.postUpdate("Open")
ALERTTIME.postUpdate(Current_DateTime.state.format("%1$td/%1$tm/%1$tY at %1$tH:%1$tM")) }
if (ALARMSENSOR1.state == "USH3438" && HOUSEOCCUPIED.state == OFF) { FRONTDOOR.postUpdate("Open") }
if (ALARMSENSOR1.state == "IAS0238" && HOUSEOCCUPIED.state == ON) { FRONTDOOR.postUpdate("Tamper")
sendTelegram("TELEGRAMBOTNAME", "Alarm - Front Door Tamper - Alarm was Armed" + Current_DateTime.state.format(" %1$td/%1$tm/%1$tY at %1$tH:%1$tM"))
ALERT.postUpdate("Front Door")
ALERTTRIGGER.postUpdate("Tamper")
ALERTTIME.postUpdate(Current_DateTime.state.format("%1$td/%1$tm/%1$tY at %1$tH:%1$tM")) }
if (ALARMSENSOR1.state == "IAS0238" && HOUSEOCCUPIED.state == OFF) { FRONTDOOR.postUpdate("Tamper")
sendTelegram("TELEGRAMBOTNAME", "Alarm - Front Door Tamper - Alarm was Disarmed" + Current_DateTime.state.format(" %1$td/%1$tm/%1$tY at %1$tH:%1$tM")) }
end
Obviously, I dont know exactly what sensors/devices you are working with and what they send you, but hopefully this gives you some help figuring out what you need to do.