Adding a new variable and value to an existing device

Hi

I have a 3in1 sensor (EZmotion) that is not always updating all the child sensors every wake up (either by design or other factors) but either way - its becuase of this that i cannot use the parent device’s ‘Last Update’ value, as it is no guarantee that was when the child sensors were last updated.

So to help with this, i would thinking about having a piece of code to constantly look out for any updates (even if the update value is the same) to a particular variable on a child device and then when it occurs it just adds the current OS.time to a new variable field ‘LastUpdate’ on that child device as one does not exist currently ?

Here is my thinking so far, and I would appreciate some help on the logic needed in the middle

[code]local Light_ID = 120
local HA_SERV = “urn:micasaverde-com:serviceId:HaDevice1”
local LIGHT_SERV = “urn:micasaverde-com:serviceId:LightSensor1”

– what would go in here to check the logs for any update ?–

luup.variable_set(HA_SERV, “LastUpdate”, os.time(), Light_ID)
[/code]

Many thanks for looking…

Click on the toolbar of the device you want to track, and click on the notification tab. You can add a notification here that will later be sent to the log.

Or if it’s something more complicated than the MiCasaVerde system allows, then in your LUUP code add a conditional statement to whatever scenario you are looking for and add a log that you can check through SSH:

if CHECKTHISSTATEMENT then luup.log("whatever you want to say") end

Hi @suhmantha1

Thanks for responding, although I’m not sure we are on the page? Let me set the scenarios I’m in in a different way.

The 3in1 EZmotion i have creates 4 devices, one parent and 3 children and all updates to any of them are already captured in the log.

My Issue is - As the 2 of the 3 child devices do not have a LastUpdate variable, there is no way for me to know when they specifically were last updated.

So the goal is to write a bit of code that looks for a specific update to a variable on the child device (e.g LightSensor1 CurrenLevel) and then when it sees one, it adds the current date/time (os.time) to a new 'LastUpdate. variable on that same child device.

The code I’ve written so far will add the new variable and value, it’s the code that will act as a trigger for when the aforementioned light sensor value is updated I need.

Hope that helps.

parkerc, there’s a Luup function luup.variable_watch() which can call any function you designate when a variable changes.

[code]function updateLastUpdate(device, service, variable, value_old, value_new)
– your existing luup.variable_set() code
end

local device_to_watch = 123 – change this
luup.variable_watch(“updateLastUpdated”, “urn:micasaverde-com:serviceId:LightSensor1”, “CurrentLevel”, device_to_watch)[/code]

The above code (put it in your Vera’s Startup Luup) will watch a device’s CurrentLevel variable, and if it changes, calls your updateLastUpdate function. This is the same mechanism that drives my Combination Switch plugin.

Notice how you aren’t actually coding the explicit check yourself, as in your incomplete example code. Doing so would be inefficient. Instead you are asking Vera to call your supplied function on demand.

Thanks @futzle

I’ve had a go at what you’ve suggested - it make perfect sense when you put it that way…

As I will be looking to do a number of these, if you have time I would appreciate it if you could cast your eye over the following

[code]function updateLightLastUpdate
luup.variable_set(urn:micasaverde-com:serviceId:HaDevice1, “LastUpdate”, os.time(), light_device_to_watch)

function updateTempLastUpdate
luup.variable_set(urn:micasaverde-com:serviceId:HaDevice1, “LastUpdate”, os.time(), temp_device_to_watch)

end

local light_device_to_watch = 136
luup.variable_watch(“updateLightLastUpdated”, “urn:micasaverde-com:serviceId:LightSensor1”, “CurrentLevel”, light_device_to_watch)

local temp_device_to_watch = 137
luup.variable_watch(“updateTempLastUpdated”, “urn:micasaverde-com:serviceId:TemperatureSensor1”, “CurrentTemperature”, temp_device_to_watch)[/code]

Also as I already have some code in my Lua Start Up (see below) how do I add the above so it does not interfere with it ?

[code]-- list of { temperature_sensor_device_id, { list of Danfoss_LC_device_id } }

devices_to_synchronize = { { 63, { 17 } } } – B

function synchronize_temp( lul_device, lul_service, lul_variable, lul_value_old, lul_value_new )

for i = 1,#devices_to_synchronize
do

if devices_to_synchronize[ i ][ 1 ] == tonumber( lul_device )
then

 for j = 1,#devices_to_synchronize[ i ][ 2 ]
   do
    luup.variable_set( 'urn:upnp-org:serviceId:TemperatureSensor1', 'CurrentTemperature', lul_value_new, devices_to_synchronize[ i ][ 2 ][ j ] )
   end

end

end

end – synchronize_temp

for i = 1,#devices_to_synchronize
do
luup.variable_watch( ‘synchronize_temp’, ‘urn:upnp-org:serviceId:TemperatureSensor1’, ‘CurrentTemperature’, devices_to_synchronize[ i ][ 1 ] )
end[/code]

I think this is key in your analysis to determine if the sensor value was in fact requested and received, but stayed the same (which would be correct operation), or was either not requested or received, due to a bug in Vera or time-out/routing issues with the device.

Exactly… ;D

Then with the use of DataMine I can then (hopefully) plot the true update frequency of Light and Temp updates, along with the existing BatteryDate and then compare them all to the Last WakeUp time/frequency. All 4 should plot out nicely on one of Chris’s graphs.

@oTi do you know how I add this new bit of code to the startup so it won’t effect the code I already have there (see above)

Sorry to repost, but I’d like to get this up and running .

How do I add this new bit of code to the existing startup I have so it won’t effect the the existing (see above) - is it an obvious thing? Do I just place it at the bottom of the existing Startup code?

You do.

Make sure your code is valid. I think I saw one function was missing an “end”.

Thanks @futzle

Do you mean I need an

end

At the very end?

Or is one needed to

end

Every Luup.variable.set command?

Sorry for what’s probably a stupid question…

Every “function” needs a matching “end”. One of your functions does, the other does not.

Thanks futzle

I’ve only really done single functions to date, although I have done scripts which use ‘else’ so have required two ‘ends’ . I take it I have to put them together, or can they go at the end of each function?

[code]function updateLightLastUpdate
luup.variable_set(urn:micasaverde-com:serviceId:HaDevice1, “LastUpdate”, os.time(), light_device_to_watch)

function updateTempLastUpdate
luup.variable_set(urn:micasaverde-com:serviceId:HaDevice1, “LastUpdate”, os.time(), temp_device_to_watch)

end
end

local light_device_to_watch = 136
luup.variable_watch(“updateLightLastUpdated”, “urn:micasaverde-com:serviceId:LightSensor1”, “CurrentLevel”, light_device_to_watch)

local temp_device_to_watch = 137
luup.variable_watch(“updateTempLastUpdated”, “urn:micasaverde-com:serviceId:TemperatureSensor1”, “CurrentTemperature”, temp_device_to_watch[/code]

Again forgive me if I am asking stupid questions.

[code]function updateLightLastUpdate()
luup.variable_set(urn:micasaverde-com:serviceId:HaDevice1, “LastUpdate”, os.time(), light_device_to_watch)
end

– here is not inside any function.

function updateTempLastUpdate()
luup.variable_set(urn:micasaverde-com:serviceId:HaDevice1, “LastUpdate”, os.time(), temp_device_to_watch)
end
[/code]

Now I understand…

Thanks @futzle, you have the patients of a saint :wink: