dzvents performance big script or multiple small ones

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
Gravityz
Posts: 587
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

dzvents performance big script or multiple small ones

Post by Gravityz »

Hello,

what is the best practice for dzvents regarding performance.

1 big script with less triggers/timers but with multiple if then tests inside

or

multiple smaller scripts each with their own trigger/timer

for example i have a script which is called every minute to update energy meter devices

inside this script i can do a test if it is the first of the month and if it is call os.execute routine to send telegrams
in this case every minute there is a if then comparison made

i can also let the meter script updating the energy devices every minute and make a seperate script which is only called once a month to os.execute routine to send telegrams

my gut feeling would be the second(seperate) script choice is better.

also if i could choose between
1 script with 1 trigger and 10x if then choices
or
10 scripts with 1 trigger with no if then choices

i do not know which uses more resources, checking the triggers/timers or doing if then's in a script
poudenes
Posts: 667
Joined: Wednesday 08 March 2017 9:42
Target OS: Linux
Domoticz version: 3.8993
Location: Amsterdam
Contact:

Re: dzvents performance big script or multiple small ones

Post by poudenes »

I created all kind small scripts. Easier to manage :)


Verzonden vanaf mijn iPhone met Tapatalk Pro
RPi3 B+, Debain Stretch, Domoticz, Homebridge, Dashticz, RFLink, Milight, Z-Wave, Fibaro, Nanoleaf, Nest, Harmony Hub, Now try to understand pass2php
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzvents performance big script or multiple small ones

Post by waaren »

poudenes wrote: Wednesday 08 May 2019 22:24 I created all kind small scripts. Easier to manage :)
+1
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Melotron
Posts: 62
Joined: Tuesday 22 November 2016 21:04
Target OS: Linux
Domoticz version: 2020.2
Location: Gothenburg
Contact:

Re: dzvents performance big script or multiple small ones

Post by Melotron »

I'm using several smal scripts.
One for each room controlled with different lux. And for leaving the apartment.
And a few other minor one, 14 scripts totally.
Its easy to manage individual and if I mess up a script it's only a smal part off the apartment that gets effected.
Also no big change on performance on my rasp 3b+
Had a rasp b+ up to a year aggo and it was slow as hell.
With alot of blocky scripts too.

I had everything in one long an big script but it was to en cumbersome to work with.
One error and the script was not running.

I even have the living room environment on 1 min timer an going and coming home on different scripts.
But I'm thinking about combining them again.

Try both ways and see how its working for you.
What works for me will probably not work for you LOL
I'm in the middle of a overhaul of my scripts and what was working good for me a yeas aggo looks stupid now lol
Cleaning, organising and adding comments on what they are doing all over the place now.
I'm adding comments on what I should think about the next time I'm going over my scripts too ^_^

//Magnus Svensson.
Gravityz
Posts: 587
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: dzvents performance big script or multiple small ones

Post by Gravityz »

ok thanks. that is what i am doing now.
indeed easy to pinpoint problems also but i was worried about the performance
poudenes
Posts: 667
Joined: Wednesday 08 March 2017 9:42
Target OS: Linux
Domoticz version: 3.8993
Location: Amsterdam
Contact:

Re: dzvents performance big script or multiple small ones

Post by poudenes »

I agree with @Melotron. I use 58 scripts to manage my house. Every script have is own function. I can never mess up the rest. Also add sometimes comments so I have some reminders when a script is using something thats also using in a different other script.

Saying that. I also make use of global_data file. Add all my device and give them a nice name so I have always the same device name. When a device is changing (sometimes it will) and get a new IDX. You only have to change that in global_data and all your scripts are working again.

Here a example maybe you love this way, I did and very happy now because of all the 58 scripts. No change everywhere etc:

First is a script so you can see how its build
Second the global_data where all the "helpers" are stored as well

Scene_AppleTV

Code: Select all

local Version = '2019.05.11'

return {
    active = true,
	on = {
	    timer = {'at nighttime'},
	    devices = {[LogitechAppleTV] = { 'at nighttime' }
	               },
	},
	-- logging = {marker = 'APPLETV Scene ' ..Version..'..........'},
	execute = function(domoticz, device)
	    
    if (domoticz.devices(LogitechAppleTV).active
        and not domoticz.devices(SceneAppleTV).active 
        and not domoticz.devices(SceneGoodnight).active
        and (domoticz.devices(iPhonePeter).active or domoticz.devices(iPhoneKay).active))
        then
            domoticz.helpers.TurnOnScene(domoticz,SceneAppleTV)
            domoticz.helpers.TurnOffScene(domoticz,SceneEvening)
            domoticz.helpers.TurnOffScene(domoticz,SceneDayTime)
            domoticz.helpers.TurnOffScene(domoticz,SceneMovie)
            domoticz.helpers.TurnOffScene(domoticz,SceneTV)
            domoticz.helpers.TurnOffScene(domoticz,SceneSexy)
            domoticz.helpers.TurnOffScene(domoticz,SceneMorning)
            domoticz.helpers.TurnOffScene(domoticz,SceneGoodnight)
            domoticz.helpers.Disco(domoticz,BulbBalcony1,nil,nil,nil)

        -- HALLOWEEN SCENE
        if domoticz.helpers.halloween(domoticz) then
            domoticz.helpers.RGB(domoticz,BulbLivingroomWall,nil,'Orange',nil)
            domoticz.helpers.RGB(domoticz,BulbLivingroomWindow,70,'Orange',nil)
            os.execute('/home/pi/domoticz/scripts/Peter/Nanoleaf/nano.sh -e "Halloween" &')
        
        -- X-MAS SCENE
        elseif domoticz.helpers.xmas(domoticz) then
            domoticz.helpers.RGB(domoticz,BulbLivingroom1,nil,'Red',nil)
            domoticz.helpers.RGB(domoticz,BulbLivingroom2,nil,'Green',nil)
            domoticz.helpers.RGB(domoticz,BulbLivingroom3,nil,'Green',nil)
            domoticz.helpers.RGB(domoticz,BulbLivingroom4,nil,'Red',nil)
            os.execute('/home/pi/domoticz/scripts/Peter/Nanoleaf/nano.sh -e "Christmas" &')
        
        -- NORMAL SCENE
        else
            domoticz.helpers.RGB(domoticz,BulbLivingroomWall,nil,nil,nil)
            domoticz.helpers.RGB(domoticz,BulbLivingroomWindow,70,nil,nil)
            os.execute('/home/pi/domoticz/scripts/Peter/Nanoleaf/nano.sh -e "Forest" &')
        end
        
        -- SENSOR DINING ROOM ON
        if (domoticz.devices(SensorDiningRoomDoor).active) then
                domoticz.helpers.Off(domoticz,BulbDressoirAll)
            
            -- SWITCH KAY LEREN
            if (domoticz.devices(SwitchKayLearn).active) then
                domoticz.helpers.TurnOn(domoticz,SwitchDiningroom,nil,nil)
            elseif (not domoticz.devices(SwitchKayLearn).active) then
                domoticz.helpers.TurnOn(domoticz,SwitchDiningroom,50,nil)
            end
    
        --SENSOR DINING ROOM OFF
        elseif (not domoticz.devices(SensorDiningRoomDoor).active) then
            
            -- HALLOWEEN SCENE
            if domoticz.helpers.halloween(domoticz) then
                domoticz.helpers.RGB(domoticz,BulbDressoirAll,50,'Blue',nil)
                domoticz.helpers.RGB(domoticz,BulbDressoir2,50,'Purple',nil)
            
            -- X-MAS SCENE
            elseif domoticz.helpers.xmas(domoticz) then
                domoticz.helpers.RGB(domoticz,BulbDressoirAll,75,'Blue',nil)
                domoticz.helpers.RGB(domoticz,BulbDressoir2,75,'White',nil)
                
            -- KINGSDAY    
            elseif domoticz.helpers.kingsday(domoticz) then
                domoticz.helpers.RGB(domoticz,BulbDressoir1,nil,'Red',nil)
                domoticz.helpers.RGB(domoticz,BulbDressoir2,nil,'White',nil)
                domoticz.helpers.RGB(domoticz,BulbDressoir3,nil,'Blue',nil)
                
            -- BDAY    
            elseif domoticz.helpers.bday(domoticz) then
                domoticz.helpers.Disco(domoticz,BulbHall1,nil,2,1)
                domoticz.helpers.Disco(domoticz,BulbDressoir1,nil,1,2) 
                domoticz.helpers.Disco(domoticz,BulbDressoir2,nil,1,3)
                domoticz.helpers.Disco(domoticz,BulbDressoir3,nil,1,4)
            
            -- NORMAL SCENE
            else
                domoticz.helpers.Kelvin(domoticz,BulbDressoirAll,50,nil,nil)
            end
            
            -- SWITCH KAY LEREN OFF
            if (domoticz.devices(SwitchKayLearn).active) then
                domoticz.helpers.TurnOn(domoticz,SwitchDiningroom,nil,nil)
            elseif (not domoticz.devices(SwitchKayLearn).active) then
                domoticz.helpers.TurnOn(domoticz,SwitchDiningroom,50,nil)
            end
        end
        os.execute('/home/pi/domoticz/scripts/Peter/Nanoleaf/nano.sh -b 50')
        os.execute('/home/pi/domoticz/scripts/Peter/speech.sh "The scene AppleTV is activated"&')
        -- domoticz.log('------------------------==<[ Scene AppleTV ]>==-----------------', domoticz.LOG_FORCE)
	end
end
}
global_data
-

Code: Select all

- VERSION 2019.01.25

-- RGB          (domoticz,idx,dim,color,sec)    (domoticz,idx,100,'white',0)
-- Kelvin       (domoticz,idx,dim,kelvin,sec)   (domoticz,idx,100,5000,0)
-- Disco        (domoticz,idx,dim,disco,sec)    (domoticz,idx,100,6,0)
-- TurnOn       (domoticz,idx,dim,sec)          (domoticz,idx,100,0)
-- TurnOff      (domoticz,idx,dim,sec)          (domoticz,idx,5,1)
-- TurnOnScene  (domoticz,idx)
-- TurnOffScene (domoticz,idx)
-- Effect       (domoticz,idx,level,sec)        (domoticz,idx,30,0)
-- OnDelay      (domoticz,idx,sec)              (domoticz,idx,15) 
-- OffDelay     (domoticz,idx,sec)              (domoticz,idx,15)
-- QuietOn      (domoticz,idx)
-- QuietOff     (domoticz,idx)


-- https://snipplr.com/view/40782/color-to-rgb-value-table-in-lua/
-- https://professorcook.org/lua/colorchart.gif
------------------------------ SLAAPKAMER
BulbMasterBedroomAll    = 578 
BulbMasterBedroom1      = 540 
BulbMasterBedroom2      = 541 
BulbMasterBedroom3      = 542 
BulbMasterBedroom4      = 543 
BulbMasterBedroom5      = 551 -- LEFT for sleeping scene
BulbMasterBedroom6      = 552 -- RIGHT for sleeping scene
------------------------------ BALCONY
BulbBalcony1            = 589 
------------------------------ DRESSOIR
BulbDressoirAll         = 601 
BulbDressoir1           = 602 
BulbDressoir2           = 603 
BulbDressoir3           = 604 
------------------------------ HALL
BulbHall1               = 484 
------------------------------ LIVINGROOM
BulbLivingroomAll       = 477 
BulbLivingroom1         = 478 
BulbLivingroom2         = 479 
BulbLivingroom3         = 481 
BulbLivingroom4         = 480 
BulbLivingroomWall      = 614 
BulbLivingroomWindow    = 615 
------------------------------ BATHROOM
BulbBathroomAll         = 528 
BulbBathroom1           = 529 
BulbBathroom2           = 530 
BulbBathroom3           = 531 
BulbBathroom4           = 532 
------------------------------ KITCHEN
BulbKitchenAll          = 506 
BulbKitchen1            = 507 
BulbKitchen2            = 508 
BulbKitchen3            = 509 
------------------------------ KITCHEN SINK
BulbKitchenSinkAll      = 517 
BulbKitchenSink1        = 518 
BulbKitchenSink2        = 519 
BulbKitchenSink3        = 520 
------------------------------ TOILET
BulbToiletAll           = 561 
BulbToilet1             = 562 
BulbToilet2             = 563 
BulbToilet3             = 564 
------------------------------ LOGITECH DEVICES
LogitechOff             = 26
LogitechAppleTV         = 27
LogitechTV              = 28
LogitechKodi            = 30    
LogitechIPTV            = 622
------------------------------ IPHONE DEVICES
iPhonePeter             = 414
iPhoneKay               = 415 
iBeaconPeter            = 748
iBeaconKay              = 741
------------------------------ SCENES
SceneDayTime            = 36
SceneEvening            = 37
SceneTV                 = 38
SceneMovie              = 39
SceneAppleTV            = 40
SceneSexy               = 41
SceneMorning            = 42
SceneGoodnight          = 43  
SceneDisco              = 639
SceneSleeping           = 245
------------------------------ SWITCHES
SwitchKayLearn          = 35
SwitchBalcony           = 59
SwitchDressoir          = 60
SwitchWall              = 61
SwitchWindows           = 62
SwitchHall              = 63
SwitchToilet            = 567
SwitchKitchen           = 65
SwitchKitchenSink       = 66
SwitchCooker            = 249
SwitchDiningroom        = 70
SwitchBedroom           = 71
SwitchBathroom          = 74
SwitchHoliday           = 680
SwitchNightLight        = 679
SwitchEnablePresence    = 467
SwitchTEST              = 193
------------------------------ SENSORS
SensorMasterBedroom     = 709
SensorMasterBedroomDoor = 730
SensorDiningDoor        = 76
SensorDiningRoomDoor    = 719
SensorKitchen           = 77
SensorHall              = 469
SensorWC                = 701
------------------------------ NEST
Nest                    = 689
------------------------------ OTHER
LuxDiv                  = 392
OutsideTemp             = 394
IPTVStatus              = 708
KodiStatus              = 99
------------------------------ COMBINATION ACTIONS
local COLORS = {
                ['White']       = {255,225,255},
                ['Red']         = {255,50,1},
                ['Blue']        = {12,0,179},
                ['Orange']      = {255,127,1},
                ['Yellow']      = {255,255,1},
                ['Green']       = {150,255,142},
                ['Purple']      = {146,66,244},
                ['LightGreen']  = {191,255,114},
                ['Romantic']    = {128,0,1}
            }
            
return {
    helpers = {
                
        xmas        = function(domoticz) return domoticz.time.matchesRule('on 01/12-05/01') end,
        halloween   = function(domoticz) return domoticz.time.matchesRule('on 23/10-4/11')  end,
        bday        = function(domoticz) return domoticz.time.matchesRule('on 11/05,19/08') end,
        kingsday    = function(domoticz) return domoticz.time.matchesRule('on 27/04') end,        
        restyear    = function(domoticz) end,

        RGB = function(domoticz,idx,dim,color,sec)
            if (dim == nil)     then dim = 100 end
        	if (color == nil)   then rgb = COLORS['White']  else rgb = COLORS[color] end
            if (sec == nil)     then sec = 0 end
            domoticz.devices(idx).setRGB(rgb[1], rgb[2], rgb[3]).afterSec(sec)
            domoticz.devices(idx).dimTo(dim).afterSec(sec)
            status = 'aangezet'
            movement = 'beweging gedecteerd'
        end,

        RGBON = function(domoticz,idx,dim,color,sec)
            if (dim == nil)     then dim = 100 end
        	if (color == nil)   then rgb = COLORS['White']  else rgb = COLORS[color] end
            if (sec == nil)     then sec = 0 end
            domoticz.devices(idx).setRGB(rgb[1], rgb[2], rgb[3]).afterSec(sec)
            status = 'aangezet'
            movement = 'beweging gedecteerd'
        end,

        Kelvin = function(domoticz,idx,dim,kelvin,sec)
            if (dim == nil)     then dim = 100 end
            if (kelvin == nil)  then kelvin = 4000 end
            if (sec == nil)     then sec = 0 end
            domoticz.devices(idx).setKelvin(kelvin).afterSec(sec)
            domoticz.devices(idx).dimTo(dim).afterSec(sec)
            status = 'aangezet'
            movement = 'beweging gedecteerd'
        end,
        
        Disco = function(domoticz,idx,dim,disco,sec)
            if (dim == nil)     then dim = 100 end
            if (disco == nil)   then disco = 1 end
            if (sec == nil)     then sec = 0 end
            domoticz.devices(idx).setDiscoMode(disco).afterSec(sec)
            domoticz.devices(idx).dimTo(dim).afterSec(sec)
            status = 'aangezet'
        end,

        TurnOn = function(domoticz,idx,dim,sec)
            if (dim == nil)     then dim = 99 end
            if (sec == nil)     then sec = 0 end
            domoticz.devices(idx).dimTo(dim).afterSec(sec)
            status = 'aangezet'
            movement = 'beweging gedecteerd'
        end,
        
        TurnOff = function(domoticz,idx,dim,sec)
            if (dim == nil)     then dim = 5 end
            if (sec == nil)     then sec = 1 end
            domoticz.devices(idx).dimTo(dim)
            domoticz.devices(idx).switchOff().afterSec(sec)
            status = 'uitgezet'
            movement = 'geen beweging gedecteerd'
        end,

        TurnOnScene = function(domoticz,idx)
            domoticz.devices(idx).switchOn().checkFirst()
            status = 'aangezet'
        end,

        TurnOffScene = function(domoticz,idx)
            domoticz.devices(idx).switchOff().checkFirst() 
            status = 'uitgezet'
            movement = 'geen beweging gedecteerd'
        end,
        
        OnDelay = function(domoticz,idx,sec)
            domoticz.devices(idx).switchOn().afterSec(sec)
            status = 'aangezet'
        end,
        
        OffDelay = function(domoticz,idx,sec)
            domoticz.devices(idx).switchOff().afterSec(sec)
            status = 'uitgezet'
        end, 
        
        On = function(domoticz,idx)
            domoticz.devices(idx).switchOn()
            status = 'aangezet'
        end,
        
        Off = function(domoticz,idx)
            domoticz.devices(idx).switchOff()
            status = 'uitgezet'
        end,
        
        QuietOn = function(domoticz,idx)
            domoticz.devices(idx).switchOn().quietOn()
            status = 'aangezet'
        end,
        
        QuietOff = function(domoticz,idx)
            domoticz.devices(idx).switchOff().quietOn()
            status = 'uitgezet'
        end,
    }
}
RPi3 B+, Debain Stretch, Domoticz, Homebridge, Dashticz, RFLink, Milight, Z-Wave, Fibaro, Nanoleaf, Nest, Harmony Hub, Now try to understand pass2php
Gravityz
Posts: 587
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: dzvents performance big script or multiple small ones

Post by Gravityz »

Very impressive.

if that is working then the few routines i have i global_data will fly.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest