Two MQTT Messages with one simple Switch

Hi there,

I’m trying to control two channels of a sonoff ch4 device with only one button. So in the sitemap I only have one switch for controlling two channels at the same time.

My approaches till now:

1.)

Channel-1 via normal item (I think this does not work because of old syntax?):

Switch sonoffpoolCH1 "SonOff Pool 4CH Ausgang 1"
{ mqtt=">[mosquitto:cmnd/sonoffpool/POWER1:command:*:default],
        <[mosquitto:stat/sonoffpool/POWER1:state:default]" }

only Channel-2 via rule:

    rule "sonoffpool"

when
    Item sonoffpoolCH1 received command
then
val mqttActions=getActions("mqtt","mqtt:broker:mosquitto")

    switch (receivedCommand) {
        case ON:
            mqttActions.publishMQTT("cmnd/sonoffpool/POWER2", "1")
        case OFF :
            mqttActions.publishMQTT("cmnd/sonoffpool/POWER2", "0")
    }
end

Only the rule fired, the switch statements gets ignored, no activity on MQTT Broker.

2.) Both actions in the rule:

    rule "sonoffpool"

when
    Item sonoffpoolCH1 received command
then
val mqttActions=getActions("mqtt","mqtt:broker:mosquitto")

    switch (receivedCommand) {
        case ON:
            mqttActions.publishMQTT("cmnd/sonoffpool/POWER1", "1")
            mqttActions.publishMQTT("cmnd/sonoffpool/POWER2", "1")
        case OFF :
            mqttActions.publishMQTT("cmnd/sonoffpool/POWER1", "0")
            mqttActions.publishMQTT("cmnd/sonoffpool/POWER2", "0")
    }
end

The rule does not load because of syntax errors. How can I separate the two actions in the switchcase so both are executed? Still no luck with simple “when” statements.

3.)

Copy the first rule with the other channel --> Only one Rule fired…

Could someone help me with this?

Thanks a lot

Please state which version of MQTT you are using. The syntax used points to MQTT1, which will not work on MQTT2.

If you have MQTT 2.x installed that is correct, that is incorrect syntax. You need to create a Thing and Channel and Link the Channel to the Item.

Fix the syntax and it will work. The error in the log tells you exactly which line it has a problem with. I suspect it’s looking for curly brackets around the case statements. You can only skip those if the case is only one line.

    switch (receivedCommand) {
        case ON: {
            mqttActions.publishMQTT("cmnd/sonoffpool/POWER1", "1")
            mqttActions.publishMQTT("cmnd/sonoffpool/POWER2", "1")
        }
        case OFF : {
            mqttActions.publishMQTT("cmnd/sonoffpool/POWER1", "0")
            mqttActions.publishMQTT("cmnd/sonoffpool/POWER2", "0")
        }
    }

Though this could be reduced to

    val msg = if(receivedCommand == ON) "1" else "0"
    mqttActions.publishMQTT("cmnd/sonoffpool/POWER1", msg)
    mqttActions.publishMQTT("cmnd/sonoffpool/POWER2", msg)

Until you get your Items defined correctly your Rules will not run.

1 Like

Use curly brackets with your switch cases, just like you would with if-else constructions.

I’d approah this differently. Set up two independent Switch Items, configure them properly, with special attention to which MQTT binding version is in use.
Only after that is working, make a simple rule to link one to the other.

2 Likes

Okay thank you, the curly brackets were the missing chars to end a line. It works also by define a general mqtt thing with switch channels and link two of them to the same item.

Thanks a lot