Trying to create a persistant variable in the Vera Lite

I have a Aeon Labs DSC24-ZWUS Smart Switch Appliance Module with a sump pump plugged into it. The device meters the energy used in KWH. I’d like to write a simple Luup code section that checks every hour to see if the pump has run (i.e. if the KWH value has increased since the last hour). I created a scene that runs every hour, and, if true, if sends a notification to my phone. The code is below. Please ignore the luup.log commands; they are there to help me understand whats happening:

local sppm_DEVICE_NO = 33 – the Sump Pump Power Meter device number
local sppm_EM_SID = “urn:micasaverde-com:serviceId:EnergyMetering1” – the Energy Meter service ID

lastKWH = lastKWH or 0
luup.log(“Value of global variable lastKWH follows”)
luup.log(lastKWH)

local currentKWH = luup.variable_get (sppm_EM_SID, “KWH”, sppm_DEVICE_NO)
luup.log("Value of local variable currentKWH is "…currentKWH)

if tonumber(currentKWH) > tonumber(lastKWH) then
lastKWH = currentKWH
luup.log(“Value of global variable lastKWH follows”)
luup.log(lastKWH)
luup.log(“if statement returns true”)
return true
else
luup.log(“if statement returns false”)
return false
end

The code does work as expected, until I save a new configuration via the UI5 interface, then it losses it’s head and doesn’t retain the value of the global variable lastKHW.

Is there a way to write this value to a location that will be essentially permanent? For example, can I store the variable in a virtual device? If so, can you recommend the syntax?

Thanks,
Rob

You can add a state variable to any device just using the luup.variable_set(…) call. State variables are retained over restarts and reboots. It is best to use a unique service ID to avoid any possible contention with existing variables. E.g.

luup.variable_set("urn:rlkindmd-com:serviceId:EnergyMetering1", "KWH", currentKWH, sppm_DEVICE_NO)

This will add the variable KWH to your device 33 using the service ID urn:rlkindmd-com:serviceId:EnergyMetering1. The same statement can be used to save new values. You can access this variable using:

local savedKWH = luup.variable_get("urn:rlkindmd-com:serviceId:EnergyMetering1", "KWH", sppm_DEVICE_NO)

RexBeckett,

Thank you for your perfect answer. It is working now, and the device shows the new variable created with the commands you gave me.

I was just reading over your most excellent thread “Conditional Scene Execution: Some Examples (Read 16078 times)” last night, so I was happy to hear from you.

I had read other descriptions of what luup.variable_set does in these forums over the last few days, and some seem to conflict with your explanation. I find the service IDs one of the most mysterious parts. Who knew you could make one up and it would just work?

Thanks again.

Here is the complete working code:
local sppm_DEVICE_NO = 33 – the Sump Pump Power Meter device number
local sppm_EM_SID = “urn:micasaverde-com:serviceId:EnergyMetering1” – the Energy Meter service ID
local rlk_sid = “urn:myuniquesid:serviceId:EnergyMetering1” – my own unique sid for saving rlklastKWH to device

local lastKWH = luup.variable_get(rlk_sid, “rlklastKWH”, sppm_DEVICE_NO)

local currentKWH = luup.variable_get (sppm_EM_SID, “KWH”, sppm_DEVICE_NO)

if tonumber(currentKWH) > tonumber(lastKWH) then
luup.variable_set(rlk_sid, “rlklastKWH”, currentKWH, sppm_DEVICE_NO)
return true
else
return false
end

ServiceIDs are used to make sure names do not conflict.

A device variable is uniquely identified by the:
DeviceID -
ServiceID -
VariableName

Usually ServiceIDs are create by MCV or Plugin developers. But you can create one as well. That way you can create a “Status” variable and it will not conflict with the “Status” variable in a Z-Wave switch.

I am a plugin developer … so I have created lots of variables. The MCV folks implied to me that all devices variables should also be defined in a plugin file S_XXXX.xml
where XXXX is unique to the plugin. They indicated that some of their random crashes are caused when variables are NOT defined in these files. (i.e. following Rex’s ssuggestion).
But I have lots of variables that are not defined in S_XXXXX.xml files. So that I do not have any more finger pointing by MCV I have put ALL of my variable definitions in S_XXXX.xml
files as part of UI7 upgrades.

This has not reduced any of the crashes I have on UI7 but now MCV can not say it’s my fault!

The MCV folks implied to me that all devices variables should also be defined in a plugin file S_XXXX.xml where XXXX is unique to the plugin. They indicated that some of their random crashes are caused when variables are NOT defined in these files.

That is very interesting. In which case, why do they still provide the facility to create new device variables from the Advanced tab of every device in UI7? Variables created this way are just like those instantiated by a luup.variable_set(…) and will not be included in the Service definition file…

I agree with you … but I changed all of my variables so I could get past the impasse and get them to find their BUG!

I understand. Thanks for your efforts to shine light into some of Vera’s darkest corners. :wink:

I took it one step further and sent emails and push notifications using Luup. The message body contains the actual Watt-Hour value and the total KWH value:

local sppm_DEVICE_NO = 33 – the Sump Pump Power Meter device number
local sppm_EM_SID = “urn:micasaverde-com:serviceId:EnergyMetering1” – the Energy Meter service ID
local rlk_sid = “urn:myuniquesid:serviceId:EnergyMetering1” --my own unique sid for saving rlklastKWH to device
local smtp_sid = “urn:upnp-org:serviceId:SmtpNotification1”
local smtp_dev_id = 4
local smtp_Subject = “Sump Pump Ran in Last Hour”
local smtp_Recipient_eMail = “”
local smtp_Recipient_Name = “”
local push_sid = “urn:upnp-org:serviceId:IOSPush1”
local push_dev_id = 15
local push_Title = “Sump Pump”

local lastKWH = luup.variable_get(rlk_sid, “rlklastKWH”, sppm_DEVICE_NO)

local currentKWH = luup.variable_get (sppm_EM_SID, “KWH”, sppm_DEVICE_NO)

local deltaWH = math.floor((tonumber(currentKWH) - tonumber(lastKWH)) * 1000 + 0.5)

local smtp_Message = “Delta energy used since last message is “…deltaWH…” WH. Total energy used is “…currentKWH…” kWH”

if tonumber(currentKWH) > tonumber(lastKWH) then
luup.variable_set(rlk_sid, “rlklastKWH”, currentKWH, sppm_DEVICE_NO)
luup.call_action(smtp_sid, “SendEmail”, { Subject = smtp_Subject, Recipient_eMail = smtp_Recipient_eMail, Recipient_Name = smtp_Recipient_Name, Message = smtp_Message }, smtp_dev_id)
luup.call_action(push_sid, “SendPushOverNotification”,{ Title = push_Title, Message = smtp_Message, Priority = 1, URL=“”, URLTitle=“”, Sound=“Gamelan” }, push_dev_id)
luup.log(“if statement returns true”)
return true
else
luup.log(“if statement returns false”)
return false
end