actually I wanted to create a whole plugin but as it actually is just a temperature sensor with some luup code so I did this. This will be used as implementation file for a normal temperature sensor which is a virtual device created with Vera. In the sensor I added two variables, one CurrentTemperature with value 0 and a variable devNum with value 31. devNum should point to the device ID of the real ZP3102 sensor. It is a Motion sensor which meassures temperature as well but it is not displayed in Vera. It is stored as a value/variable in the actual sensor. So the new virtual sensor should show this value and read it out of the motion sensor.
Until now I used a scene with local my_temp2 = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",31)
luup.variable_set("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", my_temp2,37)
return true which ran every 5 minutes to solve this issue. As I want to learn more about my vera I thought about a plugin to do all for me. I learnt that I actually only need a implementation-file as all the other stuff is pretty standrad.
My Vera is miles away and I am a bit afraid I ll put it in a loop or something bad happens if i point the virtual temp sensor to this implementation file.[code]<?xml version="1.0"?>
local SID_TS1 = "urn:upnp-org:serviceId:TemperatureSensor1"
local devNum = luup.variable_get( SID_TS1, "devNum", lul_device )
--devNumber of real ZP3102 Sensor
function StartUp (lul_device)
luup.variable_watch( "UpdateLevel", SID_TS1, "CurrentTemperature", devNum )
end
function UpdateLevel (lul_device)
local my_temp1 = luup.variable_get( SID_TS1, "CurrentTemperature", devNum )
luup.variable_set( SID_TS1, "CurrentTemperature", my_temp1, lul_device )
end
StartUp
[/code]
Maybe some of you foks can say sth about it? Is it safe to just upload it and point the virtual device to it? As it is my first Luup Code i wrote I am sure it is full of errors? I let inspire myself by multiple examples and code snippets.
Note that lul_device returned by the callback is for the device where the variable was changed. You will have to keep a local copy of the virtual device ID.
<?xml version="1.0"?>
<implementation>
<functions>
function StartUp (lul_device)
local virDev = lul_device
--devNumber of virtual Sensor
local devNum = luup.variable_get( "urn:upnp-org:serviceId:TemperatureSensor1", "devNum", virDev )
--devNumber of real ZP3102 Sensor
luup.variable_watch( "UpdateLevel", "urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", devNum )
end
function UpdateLevel (lul_device, lul_service, lul_variable, lul_value_old, lul_value_new)
luup.variable_set( "urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", lul_value_new, virDev )
end
</functions>
<startup>
StartUp
</startup>
</implementation>
This doesn’t work either. Error in logs:01 04/04/14 10:48:46.963 LuaInterface::CallFunction_Startup-1 device 42 function
StartUp
failed attempt to call a nil value <0x2be4d680>
01 04/04/14 10:48:46.963 LuImplementation::StartLua running startup code for 42 I_ZP3102virtual2.xml failed <0x2be4d680>
As this is my very first Luup-Code I would appreciate help. If I understood you correctly RexBeckett, I tried to fix the issue with the lul_device changing its value in/after “watch”…
There are a couple of small issues: You need to define virDev outside the StartUp function so it will be visible to UpdateLevel. You also need to ensure that your devNum variable gets created and handle the situation that it is empty.
[code]<?xml version="1.0"?>
virDev = “”
function StartUp (lul_device)
virDev = lul_device
--devNumber of virtual Sensor
local devNum = luup.variable_get( "urn:upnp-org:serviceId:TemperatureSensor1", "devNum", lul_device )
--devNumber of real ZP3102 Sensor
if devNum == nil then
luup.variable_set( "urn:upnp-org:serviceId:TemperatureSensor1", "devNum","", lul_device )
end
if tonumber(devNum) > 1 then
luup.variable_watch( "UpdateLevel", "urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", devNum )
else
luup.task("devNum not set!",1,"VTemp",-1)
end
end
function UpdateLevel (lul_device, lul_service, lul_variable, lul_value_old, lul_value_new)
luup.variable_set( "urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", lul_value_new, virDev )
end
Tried it, didn’t work. The Luup-Code RexBeckett “helped” me with (actually its more like he wrote it :-)) doesn’t work either. I gave up on it. I keep to the scene-stuff…
OK try this. I used this code to get the Temperature off a Monoprice motion sensor, and it worked. It’s similar to @RexBeckett code suggestion. devID variable in the device should be filled with the target device number.
<?xml version="1.0"?>
<implementation>
<functions>
local THIS_DEVICE
function UpdateLevel (lul_device, lul_service, lul_variable, lul_value_old, lul_value_new)
luup.variable_set("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", lul_value_new, THIS_DEVICE)
end
function startup (lul_device)
luup.log ("J: Ext temperature " .. lul_device .. " running..")
THIS_DEVICE = lul_device
local devid = luup.variable_get( "urn:upnp-org:serviceId:TemperatureSensor1", "devID", lul_device )
if devid == nil then
luup.variable_set( "urn:upnp-org:serviceId:TemperatureSensor1", "devID","", lul_device )
end
if devid and tonumber(devid) > 1 then
local t = luup.variable_get( "urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", tonumber(devid) )
if t then
luup.variable_set("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", t, lul_device)
end
luup.variable_watch( "UpdateLevel", "urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", tonumber(devid))
else
luup.log ("J: Ext temperature " .. lul_device .. " devID invalid!")
end
end
</functions>
<startup>startup</startup>
</implementation>
Best Home Automation shopping experience. Shop at Ezlo!