Auf die Gefahr hin das ich etwas Nerve
Habe das Script weiter ausgebaut und das Mangement von Raumvariablen und Device Groupierungsvariablen getrennt.
Werde es so wahrscheinlich bei mir einbauen.
Jetzt gibt es ein Script um alle Devices in allen Räumen anzulegen:
----------------------------------------------------------- MANAGE ROOMS START
--[[
%% properties
%% events
%% globals
--]]
function globalVar(var,val)
if (fibaro:getGlobal(var) ~= nil) then
fibaro:debug('Benötigte Variable ' ..var.. ' erkannt.')
fibaro:setGlobal(var,'')
fibaro:setGlobal(var,val)
else
local http = net.HTTPClient()
http:request("http://127.0.0.1:11111/api/globalVariables", {
options = {
method = 'POST',
headers = {},
data = '{"name":"'..var..'","value":"'..val..'"}',
timeout = 10000
},
success = function(response)
local result = response.data;
if response.status == 200 or response.status == 201 then
fibaro:debug('Status: ' ..response.status.. ' - Variable wurde angelegt')
else
fibaro:debug('Error: ' ..response.status.. ' - Zugriff verweigert')
end
end,
error = function(err)
fibaro:debug('[ERROR] ' .. err)
end
})
end
end
mySensors = {'room','door','window','motion','light','energy'}
mySensors.room = 'ROOM_WOHNZIMMER'
mySensors.door = {}
mySensors.window = {}
mySensors.motion = {}
mySensors.light = {1,2,3,100,300} -- IDs der Sensoren
mySensors.energy = {}
vOUT = json.encode(mySensors)
globalVar(mySensors.room, vOUT)
mySensors.room = 'ROOM_VORZIMMER'
mySensors.door = {}
mySensors.window = {}
mySensors.motion = {}
mySensors.light = {203,204} -- IDs der Sensoren
mySensors.energy = {}
vOUT = json.encode(mySensors)
globalVar(mySensors.room, vOUT)
mySensors.room = 'ROOM_KUECHE'
mySensors.door = {}
mySensors.window = {}
mySensors.motion = {}
mySensors.light = {300,302,303,304,305} -- IDs der Sensoren
mySensors.energy = {}
vOUT = json.encode(mySensors)
globalVar(mySensors.room, vOUT)
----------------------------------------------------------- MANAGE ROOMS END
Das Script verarbeitet die gewünschten Devices in verschiedenen Räumen und legt Variablen an welche dann in den diversen Scripten abgearbeitet werden können:
----------------------------------------------------------- MANAGE GROUPS START
--[[
%% properties
%% events
%% globals
--]]
function globalVar(var,val)
if (fibaro:getGlobal(var) ~= nil) then
fibaro:debug('Benötigte Variable ' ..var.. ' erkannt.')
fibaro:setGlobal(var,'')
fibaro:setGlobal(var,val)
else
local http = net.HTTPClient()
http:request("http://127.0.0.1:11111/api/globalVariables", {
options = {
method = 'POST',
headers = {},
data = '{"name":"'..var..'","value":"'..val..'"}',
timeout = 10000
},
success = function(response)
local result = response.data;
if response.status == 200 or response.status == 201 then
fibaro:debug('Status: ' ..response.status.. ' - Variable wurde angelegt')
else
fibaro:debug('Error: ' ..response.status.. ' - Zugriff verweigert')
end
end,
error = function(err)
fibaro:debug('[ERROR] ' .. err)
end
})
end
end
function StoreGroup(vType)
for i, Value in pairs (myGroups.room) do
vIN =fibaro:getGlobal(myGroups.room[i])
vRooms = json.decode(vIN)
if vType == 'door' then
for j, Value in pairs (vRooms.door) do
table.insert(myGroups.ID, vRooms.door[j])
end
elseif vType == 'window' then
for j, Value in pairs (vRooms.window) do
table.insert(myGroups.ID, vRooms.window[j])
end
elseif vType == 'motion' then
for j, Value in pairs (vRooms.motion) do
table.insert(myGroups.ID, vRooms.motion[j])
end
elseif vType == 'light' then
for j, Value in pairs (vRooms.light) do
table.insert(myGroups.ID, vRooms.light[j])
end
elseif vType == 'energy' then
for j, Value in pairs (vRooms.energy) do
table.insert(myGroups.ID, vRooms.energy[j])
end
end
end
vOUT = json.encode(myGroups)
--fibaro:debug(vOUT)
globalVar(myGroups.name, vOUT)
end
myGroups = {'name','type','room','ID'}
--########### USER CONFIGURATION START #############
myGroups.name = 'LIGHT_ONOFF_WOHNZIMMER'
myGroups.type = 'light'
myGroups.room = {'ROOM_VORZIMMER','ROOM_WOHNZIMMER'}
myGroups.ID = {}
StoreGroup(myGroups.type)
myGroups.name = 'LIGHT_ONOFF_ALL'
myGroups.type = 'light'
myGroups.room = {'ROOM_VORZIMMER','ROOM_WOHNZIMMER','ROOM_KUECHE'}
myGroups.ID = {}
StoreGroup(myGroups.type)
----------------------------------------------------------- MANAGE GROUPS END
Das letzte Scriptbeispiel benützt die vordefinierten Device ID’s einer oder mehrer Gruppen:
----------------------------------------------------------- USE GROUPS START
----------------------------------------------------------
local DeviceGroups = {'LIGHT_ONOFF_WOHNZIMMER'}
------------------------------------------------------------
for i = 1,#DeviceGroups do
vIN =fibaro:getGlobal(DeviceGroups[i])
IDs = json.decode(vIN)
-- INFORMATION START
fibaro:debug('Group:' ..IDs.name)
fibaro:debug('Type:' ..IDs.type)
for j, Value in pairs (IDs.room) do
fibaro:debug('Room:' ..IDs.room[j])
end
--INFORMATION END
for j, Value in pairs (IDs.ID) do
-- PUT HERE YOUR ACTIONS
fibaro:debug('ID:' ..IDs.ID[j])
end
end
----------------------------------------------------------- USE GROUPS END