Bulk device last update script

Moderator: leecollings

Post Reply
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Bulk device last update script

Post by ben53252642 »

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:

Code: Select all

if (devicechanged["Main Bathroom"] == 'On' and MainBedroomLastUpdate > 5) then
commandArray['Living Room Left Blind'] = 'On'
end
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:

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
Now we can do cool stuff like:

Code: Select all

if (devicechanged["Main Bathroom"] == 'On' and MainBedroomLastUpdate > MainEntranceHallwayLastUpdate ) then
Domoticz event log:

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
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Bulk device last update script

Post by dannybloe »

Or you use dzVents with Lua:

Code: Select all

return  {
	on = {
		devices = { 'bathroom' }
	},
	execute = function(domoticz, bathroom)
	
		local bedroom = domoticz.devices('bedroom')
		local hallway = domoticz.devices('hallway')
		
		if (bedroom.lastUpdate.secondsAgo > hallway.lastUpdate.secondsAgo) then
			-- do something
		end
	end
}
No need to do all that time stuff.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Bulk device last update script

Post by ben53252642 »

DzVents while excellent is almost like a different programming language... there can be benefits starting out with it but for those of us using Lua / familiar with Lua scripting the above could be very useful.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Bulk device last update script

Post by Egregius »

Or use pass2php and have less code :lol:

Code: Select all

if($status=='On'){
	if(past('bedroom')>past('hallway'){
		//do something
	}
}
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Bulk device last update script

Post by ben53252642 »

Egregius wrote: Wednesday 10 January 2018 16:27 Or use pass2php and have less code :lol:

Code: Select all

if($status=='On'){
	if(past('bedroom')>past('hallway'){
		//do something
	}
}
Wow, that is a very simple way to do it!

We all have our favourite programming languages. 8-)
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Bulk device last update script

Post by Egregius »

ben53252642 wrote: Wednesday 10 January 2018 16:34 Wow, that is a very simple way to do it!
That's one of the advantages of PHP.
And the millions of users worldwide of course.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest