Bulk device last update script
Posted: Wednesday 10 January 2018 14:45
This Lua script gets the last update times for large numbers of devices and makes the time available as a variable name:
eg: MainBedroomLastUpdate
Then it's possible to do simple math as part of if statements, example:
In the above scenario, if Main Bathroom is turned on and it's been more than 5 seconds since Main Bedroom was last updated then it runs the command.
You enter device names to get last update times in the top of the script.
The last update variable name is the device name without spaces with "LastUpdate" on the end, eg: MainBedroomLastUpdate
The below example shows 3 devices in my script and printing their last update time in the Domoticz event log:
Now we can do cool stuff like:
Domoticz event log:
eg: MainBedroomLastUpdate
Then it's possible to do simple math as part of if statements, example:
Code: Select all
if (devicechanged["Main Bathroom"] == 'On' and MainBedroomLastUpdate > 5) then
commandArray['Living Room Left Blind'] = 'On'
end
You enter device names to get last update times in the top of the script.
The last update variable name is the device name without spaces with "LastUpdate" on the end, eg: MainBedroomLastUpdate
The below example shows 3 devices in my script and printing their last update time in the Domoticz event log:
Code: Select all
-- Device Last Updates
t1 = os.time()
devices = {
"Main Entrance Hallway",
"Main Bathroom",
"Main Bedroom"
}
numdevices = 0 -- Count number of devices in the array
for index in pairs(devices) do
numdevices = numdevices + 1
end
for i = 1, numdevices do
s = otherdevices_lastupdate[devices[i]]
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
str = (devices[i] .. "LastUpdate")
str = str:gsub("%s+", "")
str = string.gsub(str, "%s+", "")
_G[str] = (os.difftime (t1, t2))
end
commandArray = {}
print(MainBedroomLastUpdate)
print(MainBathroomLastUpdate)
print(MainBedroomLastUpdate)
return commandArray
Code: Select all
if (devicechanged["Main Bathroom"] == 'On' and MainBedroomLastUpdate > MainEntranceHallwayLastUpdate ) then
Code: Select all
2018-01-11 00:43:25.946 LUA: 539
2018-01-11 00:43:25.946 LUA: 1488
2018-01-11 00:43:25.946 LUA: 539