Page 1 of 1
save and restore device states
Posted: Thursday 05 December 2019 20:42
by pvklink
Does anyone have a script to save the state of a number of predefined switches (on / off and selectorswitches) to be able to restore them later?
I need this functionality for my alarm system. When an alarm goes off, all kinds of devices switches on, in addition to the fact that there may already be a number turned on. I want to go back to the pre-alarm situation ..
I tried it, but this script is to difficult for me ... especially writing a key with more than one values
[{"devicename":"xxx", "state":"on","type":"on/off"},{...}]
My testscript has to save the state off test2 and lantaarn when device testxx = on and when it is off it has to recover the two devices
a device can be a switch or a selectorswitch...
Code: Select all
return {
on = { devices = { 'testxx' }},
data = {
ondev = { history = true,initial = {}},
},
logging = {level = domoticz.LOG_DEBUG},
execute = function(dz,item,info)
_G.logMarker = _G.moduleLabel -- marker wordt scriptnaam
local checklights = {'test2','lantaarn'}
local lights = dz.devices().filter(checklights)
if item.state == 'On' then -- backup
dz.data.ondev.reset()
lights.forEach(function(device)
if (device.state ~= 'Off' and device.state ~= 'uit' and device.name ~= 'testxx' and device.description == 'backup') then
dz.data.ondev.add(device) -- i need only the device name, state and if the device is a selector do not need the other properties
end
end)
else -- restore
for i = dz.data.ondev.size,1,-1 do
itemx = dz.data.ondev.get(i)
dz.log("data: " .. itemx.data) -- need th device name, state and if the device is a selector and then diferent on/off's for switch or selector
dz.devices(itemx.data).switchOn().checkFirst()
end
end
end
}
Re: save and restore device states
Posted: Thursday 05 December 2019 20:58
by waaren
pvklink wrote: Thursday 05 December 2019 20:42
Does anyone have a script to save the state of a number of predefined switches (on / off and selectorswitches) to be able to restore them later?
If you only want to backup / restore On / Off states, it can be as easy as
Code: Select all
return
{
on =
{
devices = { 'testxx' }
},
data = {
states =
{
initial = {}
},
},
logging = {level = domoticz.LOG_DEBUG},
execute = function(dz, item)
_G.logMarker = _G.moduleLabel -- marker wordt scriptnaam
local checkLights = {'test2','lantaarn'}
if item.state == 'On' then -- backup
for index, deviceName in ipairs(checkLights)
dz.data.states[deviceName] = dz.devices(deviceName).active
end
else -- restore
for index, deviceName in ipairs(checkLights)
if dz.data.states[deviceName] then
dz.devices(deviceName).switchOn()
else
dz.devices(deviceName).switchOff()
end
end
end
end
}
Re: save and restore device states
Posted: Thursday 05 December 2019 22:01
by pvklink
gonna try it

I knew there is a better way...
After hours of struggling......but i first wanted to try it myself
Re: save and restore device states
Posted: Thursday 05 December 2019 22:25
by pvklink
It works!
Had to add two "do" commands at some lines, but that was easy
which property do i have to use when using a selector device? (usb lamp is on, is a selector with different values like Flame, etc.)
or do i have to declare that in ocal checkLights
The data file contains:
-- Persistent Data
local multiRefObjects = {
} -- multiRefObjects
local obj1 = {
["states"] = {
["test2"] = true;
["lantaarn"] = true;
["usb lamp"] = false;
};
}
return obj1
Re: save and restore device states
Posted: Thursday 05 December 2019 22:56
by waaren
pvklink wrote: Thursday 05 December 2019 22:01
gonna try it

I knew there is a better way...
After hours of struggling......but i first wanted to try it myself
This one also include selectors / dimmables
Code: Select all
return
{
on =
{
devices = { 'testxx' }
},
data = {
states =
{
initial = {}
},
levels =
{
initial = {}
},
},
logging = {level = domoticz.LOG_DEBUG},
execute = function(dz, item)
_G.logMarker = _G.moduleLabel -- marker wordt scriptnaam
local checkLights = {'test2','lantaarn'}
local checkDimmable = {'test3' }
if item.state == 'On' then -- backup
for index, deviceName in ipairs(checkLights) do
dz.data.states[deviceName] = dz.devices(deviceName).active
end
for index, deviceName in ipairs(checkDimmable) do
dz.data.levels[deviceName] = dz.devices(deviceName).levelVal or dz.devices(deviceName).level
end
else -- restore
for index, deviceName in ipairs(checkLights) do
if dz.data.states[deviceName] == true then
dz.devices(deviceName).switchOn()
else
dz.devices(deviceName).switchOff()
end
end
for index, deviceName in ipairs(checkDimmable) do
dz.devices(deviceName).dimTo(dz.data.levels[deviceName])
end
end
end
}
Re: save and restore device states
Posted: Thursday 05 December 2019 23:25
by pvklink
WORKS!
Great!
My alarmsystem is almost ready!
When the domoticz app can handle the security panel with a pincode, it is all done
and i have to add some pir's.... still waiting for the boat....
i use the security panel
three scripts
motion software
7 rf kerui devices
zwave indoor alarm
sonof remote key
google home for speak
and lots of devices that go on when an alarm is activated (stereo on , foto's, light on etc.messages pushover etc.)
i implemented the script by splitting it in two and made global functions
Code: Select all
-- nog verwerken in alarm
return {
on = {devices = { 'testxx' }},
logging = {level = domoticz.LOG_ERROR},
execute = function(dz, item)
_G.logMarker = _G.moduleLabel -- marker wordt scriptnaam
if item.state == 'On' then
dz.helpers.backup_devices(dz,{'test2','lantaarn'},1)
dz.helpers.backup_devices(dz,{'usb lamp'},2)
else
dz.helpers.restore_devices(dz,{'test2','lantaarn'},1)
dz.helpers.restore_devices(dz,{'usb lamp'},2)
end
end
}
Code: Select all
data =
{
mystates1 = {initial = {}},
mystates2 = {initial = {}},
},
helpers =
{
backup_devices =
function(dz,bck_devices,bck_type)
if bck_type == 1 then
for index, deviceName in ipairs(bck_devices) do
dz.globalData.mystates1[deviceName] = dz.devices(deviceName).active
end
elseif bck_type == 2 then
for index, deviceName in ipairs(bck_devices) do
dz.globalData.mystates2[deviceName] = dz.devices(deviceName).levelVal or dz.devices(deviceName).level
end
end
end,
restore_devices =
function(dz,bck_devices,bck_type)
if bck_type == 1 then
for index, deviceName in ipairs(bck_devices) do
if dz.globalData.mystates1[deviceName] == true then
dz.devices(deviceName).switchOn()
else
dz.devices(deviceName).switchOff()
end
end
elseif bck_type == 2 then
for index, deviceName in ipairs(bck_devices) do
dz.devices(deviceName).dimTo(dz.globalData.mystates2[deviceName])
end
end
end,
}
}
Re: save and restore device states
Posted: Saturday 07 December 2019 13:29
by pvklink
I made a global function for it, for those who waNt to use it...
Only thing to do is cleaning the VARS states and levels
Code: Select all
data =
{
states = { initial = {}},
levels = { initial = {}},
},
helpers =
{
state_devices =
function(dz,devstate,checkLights,checkDimmable)
if devstate == 'backup' then
for index, deviceName in ipairs(checkLights) do
dz.globalData.states[deviceName] = dz.devices(deviceName).active
end
for index, deviceName in ipairs(checkDimmable) do
dz.globalData.levels[deviceName] = dz.devices(deviceName).levelVal or dz.devices(deviceName).level
end
elseif devstate == 'restore'
for index, deviceName in ipairs(checkLights) do
if dz.globalData.states[deviceName] == true then
dz.devices(deviceName).switchOn()
else
dz.devices(deviceName).switchOff()
end
end
for index, deviceName in ipairs(checkDimmable) do
dz.devices(deviceName).dimTo(dz.globalData.levels[deviceName])
end
elseif devstate == 'clean'
-- how to clean vars levels and states
end
end,
}
}
Re: save and restore device states
Posted: Saturday 07 December 2019 13:39
by waaren
pvklink wrote: Saturday 07 December 2019 13:29
-- how to clean vars levels and states
dz.globalData.states = {}
dz.globalData.levels = {}
Re: save and restore device states
Posted: Saturday 07 December 2019 13:50
by pvklink
yep works !
i forgot the dz.globalData part of the string
Thanks, after an alarm all my devices are restored now... is great!
Re: save and restore device states [Solved]
Posted: Saturday 07 December 2019 18:04
by pvklink
It works GREAT!!!!!!!!!!!!