two switches controlling one device

Moderator: leecollings

Post Reply
TonB
Posts: 11
Joined: Thursday 18 February 2021 21:32
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

two switches controlling one device

Post 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.
User avatar
psubiaco
Posts: 204
Joined: Monday 20 August 2018 9:38
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Italy
Contact:

Re: two switches controlling one device

Post 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).
Paolo
--
I use DomBus modules to charge EV car, get a full alarm system, control heat pump, fire alarm detection, lights and much more. Video
Facebook page - Youtube channel
lost
Posts: 643
Joined: Thursday 10 November 2016 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: two switches controlling one device

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests