Page 1 of 1
hotelswitch in loop
Posted: Sunday 17 October 2021 17:45
by Gianni
a long time ago i got help with this script.
Code: Select all
-- Wissel living
local MASTER = 'Licht_Living'
local SLAVE = 'Wissel_Living'
return {
on = {
devices = {
MASTER,
SLAVE
}
},
execute = function(domoticz, device, triggerInfo)
if (domoticz.EVENT_TYPE_TIMER == triggerInfo.type) then
domoticz.log( 'timer event: '..tostring(triggerInfo.trigger)..'.', domoticz.LOG_INFO)
elseif (domoticz.EVENT_TYPE_DEVICE == triggerInfo.type) then
if (device.name == MASTER) then
if (device.state ~= domoticz.devices(SLAVE).state) then
domoticz.devices(SLAVE).setState(device.state).silent()
end
elseif (device.name == SLAVE) then
if (device.state ~= domoticz.devices(MASTER).state) then
domoticz.devices(MASTER).setState(device.state).silent()
end
end
end
end
}
but sometime when i device not react fast enough i got a loop.
meaning the first switch go on the other off then the first one go off but the second one go on and so on.
The only thing to stop it for now is reset domoticz container.
I there a way in the script that i can change to prevent this loop?
Thx,
Re: hotelswitch in loop
Posted: Monday 18 October 2021 21:18
by rrozema
The call to .silent() should make sure that no new trigger gets activated by the call to setState(). I don't know how this can be, but it happens for me sometimes too and I don't know how to fix it in the script. What I do know is that when the switches are in a loop, you can simply manually toggle one of the both switches and the loop will stop.
Re: hotelswitch in loop
Posted: Wednesday 20 October 2021 19:03
by Gianni
rrozema wrote: Monday 18 October 2021 21:18
you can simply manually toggle one of the both switches and the loop will stop.
thats the problem that do not work for me

Re: hotelswitch in loop
Posted: Monday 25 October 2021 21:21
by hestia
Maybe there something you could use here
viewtopic.php?p=245480#p245480
by adding a delay like
so you could stop the loop if there are too quick toggle
And you could add a counter in it
Re: hotelswitch in loop
Posted: Friday 26 November 2021 10:23
by Gianni
thx but i can't see to fix it.
My light are triggerd with motion sensors
Code: Select all
--Motion Overloop
return
{
active = true,
on = { devices = { 'Beweging_Overloop' }},
execute = function(domoticz,motiontrap)
local lichttrap = domoticz.devices('Licht_Overloop')
if motiontrap.active and domoticz.time.matchesRule('between sunset and sunrise')
then
lichttrap.cancelQueuedCommands()
lichttrap.switchOn().checkFirst()
lichttrap.switchOff().afterSec(60)
end
end
}
and my hotelswitch is created with this script.
Code: Select all
-- Wissel trap
local MASTER = 'Licht_Overloop'
local SLAVE = 'Wissel_Overloop'
return {
on = {
devices = {
MASTER,
SLAVE
}
},
execute = function(domoticz, device, triggerInfo)
if (domoticz.EVENT_TYPE_TIMER == triggerInfo.type) then
domoticz.log( 'timer event: '..tostring(triggerInfo.trigger)..'.', domoticz.LOG_INFO)
elseif (domoticz.EVENT_TYPE_DEVICE == triggerInfo.type) then
if (device.name == MASTER) then
if (device.state ~= domoticz.devices(SLAVE).state) then
domoticz.devices(SLAVE).setState(device.state).silent()
end
elseif (device.name == SLAVE) then
if (device.state ~= domoticz.devices(MASTER).state) then
domoticz.devices(MASTER).setState(device.state).silent()
end
end
end
end
}
First thing i thinking of was the response from my motion sensor so i set my blind time higher.
But now it's sometimes happend on my other devices that does not have a sensor.
Problem is that i allready broken 2 switches because of the loop and continues swich on and off.
grts
Re: hotelswitch in loop
Posted: Friday 26 November 2021 12:25
by rrozema
I think the problem is in your motion script, not in the master/slave script. I think the combination .checkFirst() and .afterSec(60) makes your hotel switch script go into a loop, either that or it's the combination steState().silent that doesn't work properly. Not sure exactly how it does though. If you could try this code, and let me know if it still goes into a loop. This does not leave the light on for 60 seconds, but it leaves it on for as long as the motion sensor still sees motion. For many motion sensors you can change the settings in there, so you don't need to explicitly code this into your script: you can just adjust the setting of the motion sensor to your liking. Plus: this way the light doesn't go off while you're still in the room after 60 seconds.
Code: Select all
--Motion Overloop
return
{
active = true,
on = { devices = { 'Beweging_Overloop' }},
execute = function(domoticz,motiontrap)
local lichttrap = domoticz.devices('Licht_Overloop')
if nil == lichttrap then
domoticz.log( 'Device Licht_Overloop not found.', domoticz.LOG_ERROR)
elseif motiontrap.active then
if domoticz.time.matchesRule('between sunset and sunrise') and not lichttrap.active then
lichttrap.switchOn()
end
else
if lichttrap.active then
lichttrap.switchOff()
end
end
end
}
I'm helping you to get your script working to help you learn lua & dzVents. If you just want a working solution without learning I suggest you use my Auto-On, Auto-OnOff and Auto-Off scripts instead to achieve the requested result.
Re: hotelswitch in loop
Posted: Friday 26 November 2021 12:53
by EddyG
Could you try it with these directly after each other.
Code: Select all
<switch>.cancelQueuedCommands()
<switch>.switchOn().checkFirst().forSec(60)
<switch>.switchOff().checkFirst().afterSec(60)
I use the .for<XXX> and .after<XXX> always together. Something I learned from the late @waaren
Re: hotelswitch in loop
Posted: Friday 26 November 2021 13:20
by waltervl
EddyG wrote: Friday 26 November 2021 12:53
Could you try it with these directly after each other.
Code: Select all
<switch>.cancelQueuedCommands()
<switch>.switchOn().checkFirst().forSec(60)
<switch>.switchOff().checkFirst().afterSec(60)
I use the .for<XXX> and .after<XXX> always together. Something I learned from the late @waaren
And also documented in the wiki by @waaren
https://www.domoticz.com/wiki/DzVents:_ ... ng#Options Important note when using forAAA():
Re: hotelswitch in loop
Posted: Friday 26 November 2021 18:28
by rrozema
Yes, i know about the command options. I personally think however they often make things more complicated because you need to decide upfront what's going to happen in the future. And this is also documented in that same wiki. The command options definitely do have their purpose, but I think you should use them with care or they can have unexpected side effects.
Edit: The next part is not correct: The script does check to work only if the sensor switches on.
The problem with that original motion sensor script is that it gets triggered twice each time motion is detected: once when the sensor switches on and another time when it switches off. The script however doesn't distinguish between the both calls and re-fires the same sequence of command options while the other is still active: exactly the scenario described to look out for in the wiki.
This part is true though: dzVents is an event driven system. That doesn't work well with programmatically trying to pre-program what needs to be done.
Re: hotelswitch in loop
Posted: Sunday 28 November 2021 12:47
by Gianni
rrozema wrote: Friday 26 November 2021 12:25
this way the light doesn't go off while you're still in the room after 60 seconds.
Thx this is a much cleaner en better solution
I'm helping you to get your script working to help you learn lua & dzVents. If you just want a working solution without learning I suggest you use my Auto-On, Auto-OnOff and Auto-Off scripts instead to achieve the requested result.
I have no problem with the logic of the script, i'm just having problems with the initialisation of the variables and the excecute function(i just copy it and try to fix it now)
Like here in initialize a master and a slave and add a variable to it so ik can us it later in the script.
but what is the return and the execute.
In the return i have a i think a array devices but on the execute i have device without a S.
I read the wiki serveral times but i just miss this 1 coin to get it.
Code: Select all
local MASTER = 'Licht_Overloop'
local SLAVE = 'Wissel_Overloop'
return {
on = {
devices = {
MASTER,
SLAVE
}
},
execute = function(domoticz, device, triggerInfo)
In the wiki i take this example
Code: Select all
return
{
on =
{
devices = { 'My switch' }
},
execute = function(domoticz, switch)
-- your script logic goes here, something like this:
if (switch.state == 'On') then
domoticz.log('I am on!', domoticz.LOG_INFO)
end
end
}
Simply said, the on-part defines the trigger and the execute part is what should be done if the trigger matches with the current Domoticz event. So all your logic is inside the execute function.
So if i read correct the on defines the trigger.
So any time my switch get a manupliation the excecute will start the logic.
But i never see devices or my switch in the rest of the script again?
I only see a new variable domoticz and switch in the excecute.
Re: hotelswitch in loop
Posted: Sunday 28 November 2021 12:51
by Gianni
rrozema wrote: Friday 26 November 2021 12:25
I think the problem is in your motion script, not in the master/slave script
I think it is the master slave.
I have a master slave in my living without the motion script and this happend there 2.
Is change my motion script in your better solution but i still have loops.
It's the reaction on the zwave i think.
Sometime's when i push the first switch ,there are some sec before the second switch react.
And then i think the loop start.
grts