Script for faster, more efficient bulk control of devices (eg everything off)

Moderator: leecollings

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

Script for faster, more efficient bulk control of devices (eg everything off)

Post by ben53252642 »

This Lua script can dramatically improve performance as an alternative to a scene to bulk control devices.

Example, a home has 20 devices of various technologies (z-wave, phillips hue, xiaomi etc...). Half the devices are turned on and the other half are not.

Let's say you want to turn off all the devices, the script will look at which devices Domoticz thinks are turned on and turn them off FIRST, then tell the other half of the devices that Domoticz thinks are turned off to turn off.

So we tell the 20 devices in the home to turn off but it's done in a way that delivers a more rapid responsiveness for the user by prioritising which devices to control first.

This is what output in the Domoticz event log looks like:

Code: Select all

2018-01-10 18:39:26.478 LUA: Priority Off:Main Bathroom
2018-01-10 18:39:26.478 LUA: Priority Off:Main Entrance Hallway
2018-01-10 18:39:26.478 LUA: Priority Off:Kitchen
2018-01-10 18:39:26.478 LUA: Priority Off:Living Room Middle
2018-01-10 18:39:26.478 LUA: Priority Off:Fish Tank
2018-01-10 18:39:26.478 LUA: Remaining Off:Main Bedroom
2018-01-10 18:39:26.478 LUA: Remaining Off:Main Bedroom Hallway
2018-01-10 18:39:26.478 LUA: Remaining Off:Second Bathroom
2018-01-10 18:39:26.478 LUA: Remaining Off:Living Room Window
2018-01-10 18:39:26.478 LUA: Remaining Off:Office
2018-01-10 18:39:26.478 LUA: Remaining Off:Mirror LightStrip Main Bathroom
2018-01-10 18:39:26.478 LUA: Remaining Off:Kitty Litter Lightstrip
2018-01-10 18:39:26.478 LUA: Remaining Off:Mirror LightStrip 2nd Bathroom
2018-01-10 18:39:26.478 LUA: Remaining Off:Kitchen R Lightstrip
Devices to be controlled are processed in the order top to bottom (see the script below). Look at the script then look at the Domoticz event log above you can see how the algorithm processed devices thought to be "On" first, then moved on to turning off the remaining devices (look how Main Bedroom was the first remaining device to be turned off).

I use a virtual on / off device called "Everything" to trigger the Lua device script and then have an event which controls the virtual on / off device (Optional).

Code:

Code: Select all

commandArray = {}

-- Devices to be controlled, last entry should not have a comma at the end
devices = {
"Main Bedroom",
"Main Bedroom Hallway",
"Main Bathroom",
"Main Entrance Hallway",
"Second Bathroom",
"Kitchen",
"Living Room Middle",
"Living Room Window",
"Office",
"Fish Tank",
"Mirror LightStrip Main Bathroom",
"Kitty Litter Lightstrip",
"Mirror LightStrip 2nd Bathroom",
"Kitchen R Lightstrip"
}

if (devicechanged["Everything"] == 'On') then
    for i = 1, #devices do
        if (otherdevices[devices[i]] == 'Off') then -- Priority control
        commandArray[devices[i]] = 'On'
        print("Priority On:" .. devices[i])
        end
    end
    for i = 1, #devices do
        if (otherdevices[devices[i]] == 'On') then -- Remaining control
        commandArray[devices[i]] = 'On'
        print("Remaining On:" .. devices[i])
        end
    end

elseif (devicechanged["Everything"] == 'Off') then
    for i = 1, #devices do
        if (otherdevices[devices[i]] ~= 'Off') then  -- Priority control
        commandArray[devices[i]] = 'Off'
        print("Priority Off:" .. devices[i])
        end
    end
    for i = 1, #devices do
        if (otherdevices[devices[i]] == 'Off') then  -- Remaining control
        commandArray[devices[i]] = 'Off'
        print("Remaining Off:" .. devices[i])
        end
    end

end

return commandArray
Updated 11/06/2018 (improved code efficiency)
Last edited by ben53252642 on Monday 11 June 2018 9:16, edited 10 times in total.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
mayyam
Posts: 47
Joined: Saturday 14 January 2017 11:29
Target OS: Linux
Domoticz version: 4.11333
Location: Poland
Contact:

Re: Script for faster, more efficient bulk control of devices (eg everything off)

Post by mayyam »

Great script @ben53252642!
Its working just fine. Thank You Very Much.

But there is one, little error - first two lines are doubled by c&p.
_______________
- Dell FX-160 / Ubuntu 16.04
- RFLink 433Mhz / NRF 2.4GHz
- 2x Xiaomi Gateway
- different species of ESP8266
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Script for faster, more efficient bulk control of devices (eg everything off)

Post by ben53252642 »

mayyam wrote: Wednesday 10 January 2018 7:08 Great script @ben53252642!
Its working just fine. Thank You Very Much.

But there is one, little error - first two lines are doubled by c&p.
Fixed it, thanks!
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
Derik
Posts: 1602
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Script for faster, more efficient bulk control of devices (eg everything off)

Post by Derik »

Thanks great work!!1
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Script for faster, more efficient bulk control of devices (eg everything off)

Post by ben53252642 »

I've also implemented this in my motion sensor device scripts (More compact version for bulk turning on devices triggered by motion).

Code: Select all

commandArray = {}
--- Living Room and Kitchen + Main Entrance Hallway
if (devicechanged["Living Room Motion"] == 'On') then
    devices = {"Main Entrance Hallway","Living Room Middle","Kitchen","Fish Tank"}
    for i = 1, #devices do
        if (otherdevices[devices[i]] == 'Off') then -- Priority control
        commandArray[devices[i]] = 'On'
        print("Priority On:" .. devices[i])
        end
    end
    for i = 1, #devices do
        if (otherdevices[devices[i]] == 'On') then -- Remaining control
        commandArray[devices[i]] = 'On'
        print("Remaining On:" .. devices[i])
        end
    end
end
return commandArray

Code: Select all

2018-01-10 22:34:26.104 LUA: Priority On:Living Room Middle
2018-01-10 22:34:26.104 LUA: Remaining On:Main Entrance Hallway
2018-01-10 22:34:26.104 LUA: Remaining On:Kitchen
2018-01-10 22:34:26.104 LUA: Remaining On:Fish Tank
Last edited by ben53252642 on Monday 11 June 2018 9:17, edited 1 time in total.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: Script for faster, more efficient bulk control of devices (eg everything off)

Post by MikeF »

Excellent - works really well! Have now got rid of scenes. :D
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Script for faster, more efficient bulk control of devices (eg everything off)

Post by dannybloe »

dzVents Lua script:

Code: Select all

local SWITCHES = {
    'Main Bedroom', 'Main Bedroom Hallway', 'Main Bathroom', 'Main Entrance Hallway', 'Second Bathroom'
}

return {
    on = {
        devices = {
            'Everything'
        }
    },
    execute = function(domoticz, device)
        -- prios
        for i, switch in pairs(SWITCHES) do
            if (domoticz.devices(switch).state ~= device.state) then
                domoticz.devices(switch).toggleSwitch()
            end
        end

        -- low prios
        for i, switch in pairs(SWITCHES) do
            if (domoticz.devices(switch).state == device.state) then
                domoticz.devices(switch).setState(device.state) -- enforce current state
            end
        end
    end
}
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Script for faster, more efficient bulk control of devices (eg everything off)

Post by jvdz »

Just an FYI: You do not need that initial loop to count the number of devices in the table as you can just use #devices.
To demonstrate what I mean:

Code: Select all

commandArray = {}

-- Devices to be controlled, last entry should not have a comma at the end
devices = {
"Main Bedroom",
"Main Bedroom Hallway",
"Main Bathroom",
}

-- Count number of devices in the array
numdevices = 0
for index in pairs(devices) do
    numdevices = numdevices + 1
end

print("numdevices=" .. numdevices)
print("#devices=" .. #devices)

return commandArray
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Script for faster, more efficient bulk control of devices (eg everything off)

Post by ben53252642 »

Thanks jvdz, I've updated my posts in this thread to include your code optimization.

Anyone using a previous version before 11/06/2018 should update.

There's also another example use of this priority based logic here: viewtopic.php?f=61&t=23780#p183185

Cheers
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: Script for faster, more efficient bulk control of devices (eg everything off)

Post by Egregius »

Damn, I always like how much code is needed in lua to achieve such simple things :lol:
In pass2php that would just be:

Code: Select all

sw(array('Main Bedroom','Main Bedroom Hallway','Main Bathroom','Main Entrance Hallway','Second Bathroom'),'Off',true);
Post Reply

Who is online

Users browsing this forum: hjzwiers and 1 guest