Page 1 of 1

two switches controlling one device

Posted: Wednesday 06 September 2023 11:14
by TonB
I'm having trouble finding a solution in Lua to accomplish the following:

I've got a Sonoff zigbee mini smart switch (ZBMINI) which controls some lights in the kitchen. ("Keuken verlichting")
I've made a switch in domoticz, using the Hue bridge.
All works fine, but now, I want to use an extra switch (Using RF, KlikAanKlikUit) to also control the sonoff switch. ("Wandschakelaar keuken")

Not very hard to accomplish, by using this code :

Code: Select all

commandArray = {}

for deviceName,deviceValue in pairs(devicechanged) do
      if (deviceName=='Wandschakelaar keuken')  then
          if deviceValue == "On" then
              commandArray['Keuken verlichting'] = "On"
          elseif deviceValue == "Off" then
              commandArray['Keuken verlichting'] = "Off"
          end
      
      elseif (deviceName=='Keuken verlichting') then
          if deviceValue == "On" then
              commandArray['Wandschakelaar keuken'] = "On"
          elseif deviceValue == "Off" then
              commandArray['Wandschakelaar keuken'] = "Off"

      end
    
  end
  
end
The only problem is, that it creates a trigger loop, and the lights wil go on and off repeately.
So, I figured I had to make an ajustment in the code using a variable to determine which code must fire.
Therefore I've created a variabele "TriggeredBy"

Code: Select all

commandArray = {}


for deviceName,deviceValue in pairs(devicechanged) do
      if (deviceName=='Wandschakelaar keuken') and (uservariables['TriggeredBy'] ~= 'Keuken verlichting') then
          commandArray['Variable:TriggeredBy']=  'Wandschakelaar keuken'
          if deviceValue == "On" then
              commandArray['Keuken verlichting'] = "On"
          elseif deviceValue == "Off" then
              commandArray['Keuken verlichting'] = "Off"
          end
           commandArray['Variable:TriggeredBy'] = ' '
      
      elseif (deviceName=='Keuken verlichting') and (uservariables['TriggeredBy'] ~= 'Wandschakelaar keuken') then
          commandArray['Variable:TriggeredBy'] = 'Keuken verlichting'
          if deviceValue == "On" then
              commandArray['Wandschakelaar keuken'] = "On"
          elseif deviceValue == "Off" then
              commandArray['Wandschakelaar keuken'] = "Off"

      end
      commandArray['Variable:TriggeredBy'] = ' '
  end
  
end
But somehow I can't get it to work.
As you can see I try to clear the variable at the end, but that does not work.

Help is appreaciated.

Re: two switches controlling one device

Posted: Monday 11 September 2023 7:32
by psubiaco
You have to keep the variable content, because devices are activated when the script exits, and after activation domoticz triggers the script again.
So, you have to save in TriggeredBy who turned on/off the light, and maybe the last command (on or off).

Re: two switches controlling one device

Posted: Monday 11 September 2023 9:43
by lost
TonB wrote: Wednesday 06 September 2023 11:14 So, I figured I had to make an ajustment in the code using a variable to determine which code must fire.
Therefore I've created a variabele "TriggeredBy"...
If not having to command in chain (master1->master2->slave), one of both "master" devices may directly change slave switch if it's current state (can be checked from other_devices table) differs from the master that triggered.

IMO, this would be more straightforward+no need for a double script call scheme needing a user variable to store your last "TriggeredBy" between executions.

Drawback is if master1 changed slave, master2 state may not match slave state. But is masterX state really important? They may be hidden (name prefixed by $) to just keep slave state visible if this is confusing.