Page 1 of 1
Two question
Posted: Wednesday 28 February 2018 22:31
by madrian
I have a virtual switch (Lights) with which I switch 3 lights in Domoticz (Kankun, LED, BEDLED).
I wrote the following dzvents script:
Code: Select all
return {
active = true,
on = {
devices = {
'Lights'
}
},
execute = function(domoticz, mySwitch)
if (mySwitch.state == 'On') then
domoticz.devices('Kankun').switchOn()
domoticz.devices('LED').dimTo(90)
domoticz.devices('BEDLED').dimTo(90)
else
domoticz.devices('Kankun').switchOff()
domoticz.devices('LED').switchOff()
domoticz.devices('BEDLED').switchOff()
end
end
}
This works fine. I wanted to fine tune this script: If I turn on any lights separately-manually it should turn the status of the virtual light switch (Lights) to ON. And vice versa: if all switch status is OFF then turn virtual light switch to OFF.
So I wrote another script:
Code: Select all
return {
active = true,
on = {
devices = {
'LED',
'BEDLED',
'Kankun'
}
},
execute = function(domoticz, mySwitch)
if (domoticz.devices('Kankun').state == 'On') or
(domoticz.devices('LED').state == 'On') or
(domoticz.devices('BEDLED').state == 'On')
then
domoticz.devices('Lights').switchOn()
elseif
(domoticz.devices('Kankun').state == 'Off' and domoticz.devices('LED').state == 'Off' and domoticz.devices('BEDLED').state == 'Off')
then
domoticz.devices('Lights').switchOff()
end
end
}
And these two script are not working together, this last is constantly triggering (I see this in the log) and just not working?
Re: Two question
Posted: Wednesday 28 February 2018 22:55
by waaren
As far as I understand the situation is as follows
if you switch Kankun,LED or BEDLED the second script switches Lights. This will trigger the first script that will switch Kankun,LED and BEDLED. This will trigger the second script 1,2 or 3 times. This will continue until you stop domoticz or the event system.
For these cases dzVents 2.3 introduced the method silent()
Re: Two question
Posted: Wednesday 28 February 2018 23:08
by madrian
Great thank you very much, I added .switchXY().silent(). It's working now.
If there is a room for improvement in these 2 scripts, let me know.
Re: Two question
Posted: Wednesday 28 February 2018 23:24
by waaren
@madrian, I am not sure what your desired control is.
Do you want to be able to control the 3 devices individual or do you want to switch all off if one is switched off and switch all on if one is switched on ?
What is the function of the virtual switch "Lights" ? . Is it only used in the context of these two scripts or does it have a funtion elsewhere in your home automation environment
Re: Two question
Posted: Wednesday 28 February 2018 23:32
by madrian
3 devices individually + one master switch "Lights".
I can turn on 3 devices (Kankun, LED, BEDLED) individually or turn on/off at once with master switch "Lights".
The purpose of the second script, without it:
I turn on lamps BEDLED, LED individually. Master switch stays in state OFF. I can't turn off lamps at once, because master switch was not turned on.
With the second script:
I turn on lamps BEDLED, LED individually. Master switch switched ON, because of the second script. I can turn off lamps at once, because master switch now has state ON.
I hope it's clear now.

Re: Two question
Posted: Thursday 01 March 2018 0:22
by waaren
Hi, do you have a specific reason why you don't use two scenes "LightsOn" and "LightsOff" to switch the 3 devices independent of the current state of 1 or more of them ?
If I understand your requirement correctly, you could do without any script with that setup or if you still want to show if all devices are on or all devices are off, using a virtual switch , only use the second script.
Re: Two question
Posted: Thursday 01 March 2018 0:25
by madrian
Yes, reason is simple: Scenes/groups are currently not supported in Homebridge-edomoticz.

Re: Two question
Posted: Thursday 01 March 2018 1:32
by waaren
OK understand. My preference would be to put all this related logic in one script (easier to maintain) That would look like
Code: Select all
--[[
LEDtest
]]--
return {
on = { devices = { 'LED', 'BEDLED', 'Kankun' , 'Lights' } },
execute = function(domoticz, mySwitch)
if mySwitch.name ~= 'Lights' then
if domoticz.devices('Kankun').state == 'On' and domoticz.devices('LED').state == 'On' and domoticz.devices('BEDLED').state == 'On' then
domoticz.devices('Lights').switchOn().silent()
elseif domoticz.devices('Kankun').state == 'Off' and domoticz.devices('LED').state == 'Off' and domoticz.devices('BEDLED').state == 'Off' then
domoticz.devices('Lights').switchOff().silent()
end
elseif mySwitch.state == 'On' then
domoticz.devices('Kankun').switchOn().silent()
domoticz.devices('LED').dimTo(90).silent()
domoticz.devices('BEDLED').dimTo(90).silent()
else
domoticz.devices('Kankun').switchOff().silent()
domoticz.devices('LED').switchOff().silent()
domoticz.devices('BEDLED').switchOff().silent()
end
end
}
Re: Two question
Posted: Thursday 01 March 2018 13:27
by madrian
Thanks for the easier/combined script. It's almost what I needed, the end result is this:
Code: Select all
--[[
LEDtest
]]--
return {
on = { devices = { 'LED', 'BEDLED', 'Kankun' , 'Lights' } },
execute = function(domoticz, mySwitch)
if mySwitch.name ~= 'Lights' then
if domoticz.devices('Kankun').state == 'On' or
domoticz.devices('LED').state == 'On' or
domoticz.devices('BEDLED').state == 'On'
then
domoticz.devices('Lights').switchOn().silent()
elseif domoticz.devices('Kankun').state == 'Off' or
domoticz.devices('LED').state == 'Off' or
domoticz.devices('BEDLED').state == 'Off'
then
domoticz.devices('Lights').switchOff().silent()
end
elseif mySwitch.state == 'On' then
domoticz.devices('Kankun').switchOn()
domoticz.devices('LED').dimTo(90)
domoticz.devices('BEDLED').dimTo(90)
else
domoticz.devices('Kankun').switchOff()
domoticz.devices('LED').switchOff()
domoticz.devices('BEDLED').switchOff()
end
end
}
The difference:
- I need the OR statement in the first part
- At the end I am physically turning on/off the light if Lights button is pressed.