Switch (Incoming)

Hello,

I’m trying the following:
A ESP8266 with a capacitive touch sensor sends a MQTT message, for example a “1” when Button Number 1 is pressed.

How can i define the item? When the switch is already on, it should turn off when it’s pressed.

Do I understand right, the switch shall change amongON and OFF at every input?

Yes

The question is: which Item? Given two Items

Switch myMQTTswitch1 "Switch 1 [%s]" {mqtt="<[mosquitto:topic/of/button/1:state:MAP(mqtt.map)]"}
Switch myToggleLight "Toggle Light [%s]" {...}

and the mqtt.map:

NULL=NULL
1=ON
0=OFF
ON=1
OFF=0

you would need a rule to do the toggle:

rule "toggle light"
when
    Item myMQTTswitch1 received command
then
    myToggleLight.sendCommand(if(myToggleLight.state == ON) OFF else ON)
end

Thank you, i’ll try this.

Where can i find the mqtt.map file?

You will have to create it by yourself. You will need to install the MAP Transformation service. The file has to be in ./transformations/

In fact, I think it would be easier to define the first Item as a string:

String myMQTTswitch1 "Switch 1 [%s]" {mqtt="<[mosquitto:topic/of/button/1:state:default]"}

and to use another rule instead:

rule "toggle light"
when
    Item myMQTTswitch1 received command
then
    if (myMQTTswitch1.state == "1")
        myToggleLight.sendCommand(if(myToggleLight.state == ON) OFF else ON)
end

Thank you.

But it’s not working. The log says myMQTTswitch1 changed from NULL to 1, but then the switch does not change the state anymore. (Sending a 2 with a other button changes the state of this item from 1 to 2…)

Also the light is not turned on/off…

Item File:

String myMQTTswitch1 "Switch 1 [%s]" {mqtt="<[broker:/max/touch/one:state:default]"}
Switch myToggleLight "Toggle Light [%s]" {mqtt=">[broker:/max/light/bed:command:ON:1],>[broker:/max/light/bed:command:OFF:0]"}

Rule File:

rule "toggle light"
when
    Item myMQTTswitch1 received command
then
    if (myMQTTswitch1.state == "1")
        myToggleLight.sendCommand(if(myToggleLight.state == ON) OFF else ON)
end

Ideas?

Ah. Maybe received command is not the right trigger. Please try
Item myMQTTswitch1 received update
instead.

Works! Thank you!

As you get different messages from the different buttons, you could use the same rule to use the other buttons as well:

rule "toggle lights"
when
    Item myMQTTswitch1 received update
then
    if (myMQTTswitch1.state == "1")
        myToggleLight.sendCommand(if(myToggleLight.state == ON) OFF else ON)
    else if (myMQTTswitch1.state == "2")
        myToggleLight2.sendCommand(if(myToggleLight.state == ON) OFF else ON)
    else if (myMQTTswitch1.state == "3")
        myToggleLight3.sendCommand(if(myToggleLight.state == ON) OFF else ON)
...
end

1 Like

@max2 what capacitive switch are you using?

I’m using TTP223’s
(https://www.ebay.de/itm/10Stks-TTP223-Touch-Key-Module-Capacitive-Settable-Self-lock-No-lock-Switch-NEU/123142780725)

Thx