Switch two lights from different sources

Hello together,

i want to switch two lights (connected via a gpio relay card) with different methods:
-two switches (lamp1, lamp2) on the sitemap to control each lamp individually: already working
-mqtt binding to switch both lamps: kinda works
-one switch to switch on and off both lamps: i have a virtual switch but no rule

Problem: when the lamps are switched on or off via the mqtt binding, die states of the lamp1, lamp2 switches on the sitemap don’t change. How can i achieve that?
Which rule should i use for the virtual switch to control both lamps?

my rule for the mqtt binding:
when
Item esp_sw1 changed from OFF to ON
then
logInfo(“myLog”, “received ESPswitch ON”)
sendCommand(lamp1, ON)
sendCommand(lamp2, ON)
end
[…]

Thanks!

Why don’t you just put both lights in a group? Then you can just toggle the group on or off and both lights will receive the same command.

Ok, what do you mean by “kind of works”
Can you post your item definitions, please?

Your rule:

rule "2 lamps"
when
    Item esp_sw1 changed from OFF to ON
then
    logInfo(“myLog”, “received ESPswitch ON”)
    lamp1.sendCommand(ON)
    lamp2.sendCommand(ON)
end

Not efficient
Put your 2 lamps in a group:

Group gLamps
Switch lamp1 (gLamps) { your binding }
Switch lamp2 (gLamps) { your binding }

Your rule:

rule "gLamps"
when
    Item esp_sw1 received command
then
    logInfo(“myLog”, “received command”)
    gLamps.sendCommand(ON)
end

my item.items:
//Relays
Switch lamp1 “Channel 1” { gpio=“pin:6 activelow:yes initialValue:high” }
Switch lamp2 “Channel 2” { gpio=“pin:13 activelow:yes initialValue:high” }

//virtual
Switch lamp_both “Hoflicht”

my sitemap:
sitemap home label=“Home” {
Switch item=lamp1
Switch item=lamp2
Switch item=lamp_both
}

The mqtt binding does work so far. But when it toggles the lamps, the corresponding switches in the basic UI don’t change their state.

What do you mean the mqtt binding works so far?
None of the items has an mqtt binding.
Where is your physical switch? And how do you receive the value in OH? Is it esp_sw1?
What is esp_sw1 item definition?

I’m sorry. I forgot to add my mqtt.items file:

Switch esp_sw1 “esp_sw1” { mqtt=">[broker:switch:command:*:default], <[broker:switch:state:default" }

OK, from what I gather, you are using an esp as a remote switch

Items

Group gLamps

Switch esp_sw1 “esp_sw1” { mqtt="<[broker:switch:command:default]" }

//Relays
Switch lamp1 “Channel 1” (gLamps) { gpio=“pin:6 activelow:yes initialValue:high”, autoupdate="false" }
Switch lamp2 “Channel 2” (gLamps) { gpio=“pin:13 activelow:yes initialValue:high”, autoupdate="false" }

//virtual
Switch lamp_both “Hoflicht”

Rules

rule "Both Lamps"
when
    Item esp_sw1 received command
then
    logInfo(“myLog”, “received command from esp_sw1:” + receivedCommand.toString)
    gLamps.sendCommand(receivedCommand)
    lamp_both.postUpdate(receivedCommand)
end

rule "Virtual switch"
when
    Item lamp_both received command
then
    esp_sw1.sendCommand(receivedCommand)
end

Thanks for your very fast and helpful answer. After fixing some quotation mark errors, it works.

But i’m still not really satisfied by how i works: If i switch on the lights using my mqtt device or the virtual switch, the switches for ‘lamp1’ und ‘lamp2’ still seem to be OFF.
Isn’t it possible to let the UI show the actual state of the switch?

What browser are you using?

I tested chrome(60) and firefox (59). Both show the same behaviour in the basic UI

Odd, try removing the autoupdate=“false” from the switches

Unfortunately it doesn’t make a difference.

What happens if you switch your esp and then refresh the browser?

Ahh. refreshing shows the current state in the red/green symbol on the left. Is it possible to update the checkBox/slider on the right too?

You need to adjust the auto refresh settings of your browser