mirror switches unnecessarily complicated :D
Posted: Monday 16 December 2024 14:59
hello everyone.
i've made myself a little script that will make 2 switches mirror each other, no matter which one i push and in what state it is, the other one will mirror. (not like the slave device for a switch). so let say i have a switch that will control some real device. i want to activate that switch from some script after some conditions are met and i found out that the best way for me is to use another virtual switch that after is activated and the conditions are met, will send the command for the real switch.
the mirroring must work in both ways and in any state of the switches.
i've done this in a little script (and because i can make my life more complicated that already is) i wanted to make the script a general one, to be able to mirror any 2 switches and how many pairs of them i want:
the script is working for what i can see so far:
now, because i have mirrored a switch that some times need to be kept Off by a script, for security reasons, and because i can manually activate that switch in this time, before the security script activate and turns back Off the switch, i need a script that check if the state of the first one is not already changed before mirroring.
or a way to block the switch that is kept Off by the script to be accessible for me to change state.
i don't know how to better explain the problem since english is not my primary language.
the real application - maybe i manage to explain better:
a switch "real_1" controls a combi boiler that heats a buffer tank. when the buffer tank reach a max temperature, a script will not allow the switch 'real_1' to be activated. the problem is, i can manually push the switch in domoticz that will turn On, the boiler starte then the security script is activated and the 'real_1' is shut Off and the boiler is turned off.
'dummy_1' mirrors real_1, and also real_1 mirrors dummy_1.
i need that the script, when i activate dummy_1, before sending the command to real_1 for the mirror part, to do the check if dummy_1 was not already been shut Off by the script.
the modification that i made to my initial script were like this: i introduced another dummy switch that turns on immediately after the first switch changes state, and check if the initial state of the first switch is still the same then i mirror it with the second switch. something like this:
the problem i have with this :
is that the check "if helpOnOff.state == 'On'and dummySwitch.state == 'On' then" needs to be made after "helpOnOff.switchOn().afterSec(sec) " but for how domoticz works, if i understand correctly, the state of the switch helpOnOff doesn't have time to change in domoticz (even withow afterseSec() ) before the next line is executed so it will always be Off.
can someone help me get around this problem?
or doese someone know a easier way to do this type of synchronization i want to do?
i've made myself a little script that will make 2 switches mirror each other, no matter which one i push and in what state it is, the other one will mirror. (not like the slave device for a switch). so let say i have a switch that will control some real device. i want to activate that switch from some script after some conditions are met and i found out that the best way for me is to use another virtual switch that after is activated and the conditions are met, will send the command for the real switch.
the mirroring must work in both ways and in any state of the switches.
i've done this in a little script (and because i can make my life more complicated that already is) i wanted to make the script a general one, to be able to mirror any 2 switches and how many pairs of them i want:
the script is working for what i can see so far:
Code: Select all
local sec = 1 -- second to delay the toggle
return {
on = {
devices = {
'Centrala_ON/OFF', 'Centrala_ON/OFF_(rLe1)', -- Pair 1
'Pompa_ON/OFF', 'Pompa_On/OFF_(rLe2)', -- Pair 2
--'Pompa2_ON/OFF', 'Pompa2_On/OFF_(rLe2)', -- Pair 3
--'Pompa4_ON/OFF', 'Pompa4_On/OFF_(rLe4)' -- Pair 4
}
},
logging = {
level = domoticz.LOG_INFO,
marker = "========= Sync Multiple Dummy and Real Switches ========="
},
execute = function(domoticz, device)
-- Defining the switch pairs
local switchPairs = {
{dummy = 'Centrala_ON/OFF', real = 'Centrala_ON/OFF_(rLe1)'},
{dummy = 'Pompa_ON/OFF', real = 'Pompa_On/OFF_(rLe2)'},
--{dummy = 'Pompa3_ON/OFF', real = 'Pompa3_On/OFF_(rLe3)'},
--{dummy = 'Pompa4_ON/OFF', real = 'Pompa4_On/OFF_(rLe4)'}
}
--getting the switch pairs that was activated
for _, pair in ipairs(switchPairs) do
local dummySwitch = domoticz.devices(pair.dummy) -- Getting the dummy switch
local realSwitch = domoticz.devices(pair.real) -- Getting the real switch
-- Synchronizing the dummy with the real switch
if device.name == pair.dummy then
if dummySwitch.state == 'On' and realSwitch.state ~= 'On' then
realSwitch.switchOn().afterSec(sec)
elseif dummySwitch.state == 'Off' and realSwitch.state ~= 'Off' then
realSwitch.switchOff()
end
-- Synchronizing the real with the dummy switch
elseif device.name == pair.real then
if realSwitch.state == 'On' and dummySwitch.state ~= 'On' then
dummySwitch.switchOn()--.afterSec(sec)
elseif realSwitch.state == 'Off' and dummySwitch.state ~= 'Off' then
dummySwitch.switchOff()
end
end
end
end
or a way to block the switch that is kept Off by the script to be accessible for me to change state.
i don't know how to better explain the problem since english is not my primary language.
the real application - maybe i manage to explain better:
a switch "real_1" controls a combi boiler that heats a buffer tank. when the buffer tank reach a max temperature, a script will not allow the switch 'real_1' to be activated. the problem is, i can manually push the switch in domoticz that will turn On, the boiler starte then the security script is activated and the 'real_1' is shut Off and the boiler is turned off.
'dummy_1' mirrors real_1, and also real_1 mirrors dummy_1.
i need that the script, when i activate dummy_1, before sending the command to real_1 for the mirror part, to do the check if dummy_1 was not already been shut Off by the script.
the modification that i made to my initial script were like this: i introduced another dummy switch that turns on immediately after the first switch changes state, and check if the initial state of the first switch is still the same then i mirror it with the second switch. something like this:
Code: Select all
local sec = 1 -- second to delay the toggle
return {
on = {
devices = {
'Centrala_ON/OFF', 'Centrala_ON/OFF_(rLe1)', -- Pair 1
'Pompa_ON/OFF', 'Pompa_On/OFF_(rLe2)', -- Pair 2
--'Pompa2_ON/OFF', 'Pompa2_On/OFF_(rLe2)', -- Pair 3
--'Pompa4_ON/OFF', 'Pompa4_On/OFF_(rLe4)' -- Pair 4
}
},
logging = {
level = domoticz.LOG_INFO,
marker = "========= Sync Multiple Dummy and Real Switches ========="
},
execute = function(domoticz, device)
-- Definim perechile de switch-uri
local switchPairs = {
{dummy = 'Centrala_ON/OFF', real = 'Centrala_ON/OFF_(rLe1)'},
{dummy = 'Pompa_ON/OFF', real = 'Pompa_On/OFF_(rLe2)'},
--{dummy = 'Pompa3_ON/OFF', real = 'Pompa3_On/OFF_(rLe3)'},
--{dummy = 'Pompa4_ON/OFF', real = 'Pompa4_On/OFF_(rLe4)'}
}
local helpOnOff = domoticz.devices('helpOnOff')
helpOnOff.switchOff()
--getting the switch pairs that was activated
for _, pair in ipairs(switchPairs) do
local dummySwitch = domoticz.devices(pair.dummy) -- getting the dummy switch
local realSwitch = domoticz.devices(pair.real) -- getting the real switch
-- synchronizing the dummy switch (if was pushed) with his correspondent
if device.name == pair.dummy then
if dummySwitch.state == 'On' and realSwitch.state ~= 'On' then
helpOnOff.switchOn().afterSec(sec) -- adding a delay before sincronisation
if helpOnOff.state == 'On'and dummySwitch.state == 'On' then
realSwitch.switchOn()
end
elseif dummySwitch.state == 'Off' and realSwitch.state ~= 'Off' then
helpOnOff.switchOn().afterSec(sec) -- adding a delay before sincronisation
if helpOnOff.state == 'On'and dummySwitch.state == 'Off' then
realSwitch.switchOff()
end
end
-- synchronizing the real switch (if was pushed) with his correspondent
elseif device.name == pair.real then
if realSwitch.state == 'On' and dummySwitch.state ~= 'On' then
helpOnOff.switchOn().afterSec(sec) -- adding a delay before sincronisation
if helpOnOff.state == 'On'and realSwitch.state == 'On' then
dummySwitch.switchOn()
end
elseif realSwitch.state == 'Off' and dummySwitch.state ~= 'Off' then
helpOnOff.switchOn().afterSec(sec) -- adding a delay before sincronisation
if helpOnOff.state == 'On'and realSwitch.state == 'Off' then
dummySwitch.switchOff()
end
end
end
end
end
}
the problem i have with this :
Code: Select all
if dummySwitch.state == 'On' and realSwitch.state ~= 'On' then
helpOnOff.switchOn().afterSec(sec) -- adding a delay before sincronisation
if helpOnOff.state == 'On'and dummySwitch.state == 'On' then
realSwitch.switchOn()
end
can someone help me get around this problem?
or doese someone know a easier way to do this type of synchronization i want to do?