Vera watch variable / startup LUA how ?
-
I want to set something up natively if possible on Vera, so a virtual motion sensor device is tripped / not tripped by following the tripped variable state of a real device.
This LUA code works if I run it manually but I don't know how to have this running all the time and watching for that "tripped" variable to change ?
local istripped = luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", 100) --Real Device luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", istripped,101) --Virtual Motion Sensor Device return true
How do you add something like this in to Vera's startup LUA feature ?
Thanks
-
I've already posted in the old place, so I'll post here again and save to my GitHub, for future reference:
devices = { [61] = {sensorID = 89}, [65] = {sensorID = 94}, [63] = {sensorID = 95} } function copyVariable(dev_id, service, variable, oldValue, newValue) if tonumber(oldValue) ~= tonumber(newValue) then luup.log(string.format("Setting %s - %s for #%s to %s", service, variable, tostring(deviceID), tostring(newValue))) local deviceID = devices[dev_id].sensorID luup.variable_set(service, variable, newValue, deviceID) end end for deviceID, _ in next, devices do luup.variable_watch("copyVariable", "urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", deviceID) end
Where 61, 65 and 63 are the master and 89, 94 and 95 are the children. I've just included a version that could work with more then one device, but you could just remove the other two and it'll work anyway.
-
I've already posted in the old place, so I'll post here again and save to my GitHub, for future reference:
devices = { [61] = {sensorID = 89}, [65] = {sensorID = 94}, [63] = {sensorID = 95} } function copyVariable(dev_id, service, variable, oldValue, newValue) if tonumber(oldValue) ~= tonumber(newValue) then luup.log(string.format("Setting %s - %s for #%s to %s", service, variable, tostring(deviceID), tostring(newValue))) local deviceID = devices[dev_id].sensorID luup.variable_set(service, variable, newValue, deviceID) end end for deviceID, _ in next, devices do luup.variable_watch("copyVariable", "urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", deviceID) end
Where 61, 65 and 63 are the master and 89, 94 and 95 are the children. I've just included a version that could work with more then one device, but you could just remove the other two and it'll work anyway.
@therealdb Thanks, so you just copy the code in to the Vera Startup Lua area ?
I never understood properly how the Startup Lua thing worked? Presumably you can't just keep pasting code snippets in to it for various different things ?
This is what I have in there currently:
-
If I run this in the test code area in Vera, it does not trip the virtual motion sensor (290) the real device (696) is currently tripped.
devices = { [696] = {sensorID = 290} } function copyVariable(dev_id, service, variable, oldValue, newValue) if tonumber(oldValue) ~= tonumber(newValue) then luup.log(string.format("Setting %s - %s for #%s to %s", service, variable, tostring(deviceID), tostring(newValue))) local deviceID = devices[dev_id].sensorID luup.variable_set(service, variable, newValue, deviceID) end end for deviceID, _ in next, devices do luup.variable_watch("copyVariable", "urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", deviceID) end
-
yes, but you could just add to VeraScenes as a function, and call it. remember that in this case, copyVariables should be initialized in startup code as follows
VeraScenes = require("VeraScenes") copyVariable = VeraScenes.copyVariable VeraScenes.startWatches() ... other code...
and in your VeraScenes, add
devices = { [61] = {sensorID = 89}, [65] = {sensorID = 94}, [63] = {sensorID = 95} } function copyVariable(dev_id, service, variable, oldValue, newValue) if tonumber(oldValue) ~= tonumber(newValue) then luup.log(string.format("Setting %s - %s for #%s to %s", service, variable, tostring(deviceID), tostring(newValue))) local deviceID = devices[dev_id].sensorID luup.variable_set(service, variable, newValue, deviceID) end end function startWatches() for deviceID, _ in next, devices do luup.variable_watch("copyVariable", "urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", deviceID) end end
-
yes, but you could just add to VeraScenes as a function, and call it. remember that in this case, copyVariables should be initialized in startup code as follows
VeraScenes = require("VeraScenes") copyVariable = VeraScenes.copyVariable VeraScenes.startWatches() ... other code...
and in your VeraScenes, add
devices = { [61] = {sensorID = 89}, [65] = {sensorID = 94}, [63] = {sensorID = 95} } function copyVariable(dev_id, service, variable, oldValue, newValue) if tonumber(oldValue) ~= tonumber(newValue) then luup.log(string.format("Setting %s - %s for #%s to %s", service, variable, tostring(deviceID), tostring(newValue))) local deviceID = devices[dev_id].sensorID luup.variable_set(service, variable, newValue, deviceID) end end function startWatches() for deviceID, _ in next, devices do luup.variable_watch("copyVariable", "urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", deviceID) end end
@therealdb said in Vera watch variable / startup LUA how ?:
yes, but you could just add to VeraScenes as a function, and call it.
I will try and do that for me.
However I am writing a user guide for a particular Z-Wave device for others to follow on the forum and I am stuck on this part of the setup.
So I need to make this as generic as possible.
So I can just post the code directly in to the Startup Lua window ?
Thanks
-
@therealdb said in Vera watch variable / startup LUA how ?:
yes, but you could just add to VeraScenes as a function, and call it.
I will try and do that for me.
However I am writing a user guide for a particular Z-Wave device for others to follow on the forum and I am stuck on this part of the setup.
So I need to make this as generic as possible.
So I can just post the code directly in to the Startup Lua window ?
Thanks
-
@therealdb OK its working ! I just pasted the raw code in to the startup LUA area and the virtual motion sensor now follows the real devices tripped status.
I am writing a user guide for the Everspring EH403 Floodlight device, as it doesn't configure correctly after pairing with Vera.
I will credit you for your code in the guide when I publish it on the forum.
Thanks
-
yes, but you could just add to VeraScenes as a function, and call it. remember that in this case, copyVariables should be initialized in startup code as follows
VeraScenes = require("VeraScenes") copyVariable = VeraScenes.copyVariable VeraScenes.startWatches() ... other code...
and in your VeraScenes, add
devices = { [61] = {sensorID = 89}, [65] = {sensorID = 94}, [63] = {sensorID = 95} } function copyVariable(dev_id, service, variable, oldValue, newValue) if tonumber(oldValue) ~= tonumber(newValue) then luup.log(string.format("Setting %s - %s for #%s to %s", service, variable, tostring(deviceID), tostring(newValue))) local deviceID = devices[dev_id].sensorID luup.variable_set(service, variable, newValue, deviceID) end end function startWatches() for deviceID, _ in next, devices do luup.variable_watch("copyVariable", "urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", deviceID) end end
@therealdb said in Vera watch variable / startup LUA how ?:
copyVariable = VeraScenes.copyVariable
VeraScenes.startWatches()OK I am trying to add this code in to the VeraScenes.lua file now for myself.
This is what my startup looks like:
I have just pasted the LUA code in to the end of my VeraScenes.lua file and uploaded it again to Vera
However I am getting an error in the blue banner in the Vera gui.
"ERROR : Error in lua for scenes and events"
-
@therealdb said in Vera watch variable / startup LUA how ?:
copyVariable = VeraScenes.copyVariable
VeraScenes.startWatches()OK I am trying to add this code in to the VeraScenes.lua file now for myself.
This is what my startup looks like:
I have just pasted the LUA code in to the end of my VeraScenes.lua file and uploaded it again to Vera
However I am getting an error in the blue banner in the Vera gui.
"ERROR : Error in lua for scenes and events"
-
@cw-kid Looks like you forgot to declare the function in VeraScenes
function startWatches()