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,
}
}