Lua script for tracking time at and away from home
Posted: Sunday 10 June 2018 9:34
This is a couple of Lua scripts I wrote for tracking time spent inside and outside my apartment each day.
1) Create a couple of custom sensors and name the "Axis Label" "Hours".
2) Create two seperate Lua TIME scripts in Setup > More Options > Events
Edit below scripts as needed, in particular the method you use to determine if you are home or away from home (mine is called "Ben is Home").
Time Inside Apartment
Time Outside Apartment
I've been running this for about a couple of months now and collected some interesting data.
Please note this is not a thread for discussing presence detection methods, if you don't already have a way of detecting presence there are many methods available on the Domoticz wiki.
https://www.domoticz.com/wiki/Presence_detection
1) Create a couple of custom sensors and name the "Axis Label" "Hours".
2) Create two seperate Lua TIME scripts in Setup > More Options > Events
Edit below scripts as needed, in particular the method you use to determine if you are home or away from home (mine is called "Ben is Home").
Time Inside Apartment
Code: Select all
commandArray = {}
time = os.date("*t")
counter = otherdevices_svalues['Time Inside Apartment']
counter = tonumber(counter)
-- Reset hours 1 minute before mid-night (for charting reasons)
if (time.hour == 0) and (time.min == 00) then
counter = 0
commandArray['UpdateDevice'] = otherdevices_idx['Time Inside Apartment'] .. '|0|' .. tonumber(counter)
end
if (otherdevices["Ben is Home"] == 'On') then
counter = counter + 0.016666666666667
commandArray['UpdateDevice'] = otherdevices_idx['Time Inside Apartment'] .. '|0|' .. tonumber(counter)
print ('Added one minute to time inside apartment counter. Hours: ' .. counter .. '')
end
return commandArray
Code: Select all
commandArray = {}
time = os.date("*t")
counter = otherdevices_svalues['Time Outside Apartment']
counter = tonumber(counter)
-- Reset hours 1 minute before mid-night (for charting reasons)
if (time.hour == 0) and (time.min == 00) then
counter = 0
commandArray['UpdateDevice'] = otherdevices_idx['Time Outside Apartment'] .. '|0|' .. tonumber(counter)
end
if (otherdevices["Ben is Home"] == 'Off') then
counter = counter + 0.016666666666667
commandArray['UpdateDevice'] = otherdevices_idx['Time Outside Apartment'] .. '|0|' .. tonumber(counter)
print ('Added one minute to time outside apartment counter. Hours: ' .. counter .. '')
end
return commandArray

Please note this is not a thread for discussing presence detection methods, if you don't already have a way of detecting presence there are many methods available on the Domoticz wiki.
https://www.domoticz.com/wiki/Presence_detection