Hacks to get last user notifications, and with keyfob

Here’s my end-goal: get an email any time the alarm is armed or disarmed that tells me the name of the person who did it. i.e. “Alarm changed state to ‘Armed’ by ‘Bob’”.

I tried a few approaches to achieve this goal. The “simplest” I first thought of was to create triggers for every user number. But I couldn’t get that to work at all. I tried entering user numbers as single digits, 2-digit padded with leading zeros, 3-digit, 4-digit, 5-digit, and 6-digit. I also tried entering the PIN codes instead of user numbers – nothing I tried worked. I also found that arming the panel with my keyfob doesn’t show up like a regular arm/disarm event, and doesn’t update the LastUser field. So I worked around that.

So what did I do? I created a scene called “Alarm last user” that watches for the “LastUser” varible on the panel to change, and when it does it will directly generate an email using the SmtpNotification plugin, and also sends a push notification to Automator.app. It has the user number to friendly name translation table directly embedded in the code. Not pretty, but works fine.

It’s not really a “scene” in a classical sense…it’s more of a plugin masquerading as a scene. That’s the kind way of saying it’s just a dirty hack by a newbie writing Lua and using Luup for the first time. This is absolutely not polished or nice in any way. But it works for me. :slight_smile:

If you want to use it you’ll need to change the hard-coded device numbers, email addresses, and of course the user number to friendly name mapping table.

Start by create a new scene. I called mine “Alarm last user”. Create several triggers for this scene:

[ul] [li]Trigger: Alarm state-Device is Armed (last user armed)[/li]
[li]Trigger: Alarm state-Device is Disarmed (last user disarmed)[/li]
[li]Trigger: Vendor Status Code-701 (keyfob armed)[/li]
[li]Trigger: Vendor Status Code-751 (keyfob disarmed)[/li][/ul]

After creating all these triggers open up the list of triggers and click on “Luup event” for the first two, and put this same code in both of them:

if (LAST_USER_TRIGGER ~= nil) then
   return false


Now open up the second two triggers and put in this code:

luup.variable_set("urn:micasaverde-com:serviceId:AlarmPartition2", "LastUser", "0001", 12)
return false

You’ll want to change “0001” to whatever user number you want associated with the keyfob being used to arm/disarm the system. You’ll also need to change “12” to whatever you actual device number is.

Now you get to the meat of it. Before doing this you’ll need to have the “eMail Notification” plugin (free, I used version 0.92) and a working SMTP notification device, or an Automator.app push notification device (not free). Or you can have both like I do. Go back to the scene you’ve created and select the “LUUP” tab, and paste in the following code. Don’t forget to change all the device numbers and the email address, and the user number to name mapping. Delete the code referencing automatorapp if you’re not using that.

if (LAST_USER_TRIGGER == nil) then

if (LAST_USER_TRIGGER == 0) then
   return false

function cb_LastUser(dev, svc, var, val_old, val_new)

users = {}
users["0001"] = "John Smith"
users["0002"]="Unused 02"
users["0003"]="Joe Blow"
users["0004"]="John Doe"
users["0005"]="Jane Doe"
local svc = "urn:micasaverde-com:serviceId:AlarmPartition2"
local dev = 12

local last_user_num_str = luup.variable_get(svc, "LastUser", dev)

if (users[last_user_num_str] == nil) then
    luup.log("Last user #: " .. last_user_num_str, 1)
    luup.variable_set(svc, "LastUserStr", last_user_num_str, dev)
    luup.log("Last user: '" .. users[last_user_num_str] .. "'", 1)
    luup.variable_set(svc, "LastUserStr", users[last_user_num_str], dev)

local last_user_str = luup.variable_get(svc, "LastUserStr", dev)
local arm_mode      = luup.variable_get(svc, "ArmMode", dev)

if (arm_mode == nil) then
    arm_mode = "unknown"

local user_action = last_user_str .. " " .. arm_mode
luup.variable_set(svc, "LastUserStrAction", user_action, dev)

local msg_body = "Alarm set by and to: '" .. user_action .. "'"
local t_push = { message = msg_body, badge = "+1", sound = "" }
luup.call_action("urn:automatorapp-com:serviceId:Push1", "SendPushNotification", t_push, 77)

svc = "urn:upnp-org:serviceId:SmtpNotification1"
dev = 81
local t_email = { Subject = "Alarm state change", Recipient_eMail = "youremail@example.com", Recipient_Name = "", PreMessage = "State changed by and to: '", PostMessage = "'", DeviceID = 12, DeviceType = "urn:micasaverde-com:serviceId:AlarmPartition2", VariableName = "LastUserStrAction" }

luup.call_action(svc, "SendDynamicEmail", t_email, dev)


luup.variable_watch("cb_LastUser", "urn:micasaverde-com:serviceId:AlarmPartition2", "LastUser", 12)


That’s it. Tested on Vera version 1.5.408, DSC Alarm Panel Plugin version 0.38.