Page 1 of 1

Bulk device last update script

Posted: Wednesday 10 January 2018 14:45
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

Re: Bulk device last update script

Posted: Wednesday 10 January 2018 16:17
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.

Re: Bulk device last update script

Posted: Wednesday 10 January 2018 16:24
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.

Re: Bulk device last update script

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

Code: Select all

if($status=='On'){
	if(past('bedroom')>past('hallway'){
		//do something
	}
}

Re: Bulk device last update script

Posted: Wednesday 10 January 2018 16:34
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-)

Re: Bulk device last update script

Posted: Wednesday 10 January 2018 16:50
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.