What could be difficult to implement another switch Item? Actually when we find solution or when we purchase buttons it is easy. However this kind of button can work in 2 modes similar like previously described Remote 433MHz Switch . As reminder we may want to configure it as:
- one push ON, second push OFF – toggle
- push button and keep state ON until release
I need first option. I would like to install button switch for turning on/off my desk lamp which has installed Sonoff Basic installed instead of standard switch.
Hardware
OpenHAB configuration
As my button works using 433 MHz wireless I combined it with Sonoff RF Bridge to capture signal and send MQTT notification to OpenHAB.
I have clicked button multiple times and let Sonoff RF Bridge capture signal in console. In my case “Data” values were static. I wrote down to use it in my existing OpenHAB thing configuration.
.things
I add thing element to my existing configuration. We use specific code value to be captured.
1 |
Type string : button1rf "Bakey Button 1" [ stateTopic="tele/sonoff-bridge/RESULT", transformationPattern="JSONPATH:$.RfReceived.Data", allowedStates="851201", autoupdate="false" ] |
.items
We have to create virtual Item that will track state of the button switch. It is String type as we just wants to collect Data value. (HINT: Do not use “-” in Item names. It is forbidden)
1 |
String button1_rf "Decoding RF code [%s]" { channel="mqtt:topic:sonoffbridge:button1rf" } |
.rules
Yes, magic happens in rule. Unfortunately we can’t make it using standard settings. In short once Item button1_rf receives an update we check for the code and if matches perform opposite action to the current state.
I also implemented Sonoff T0 Wall switch (sonoffwall3_p3) that uses for the same purpose – control Desktop Lamp so button switch should control the same.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
rule "BUTTON TOGGLE" when Item button1_rf received update then var rfData = button1_rf.state.toString Thread::sleep(100) logInfo("rule SONOFF BRIDGE", "Incoming RF code: " + rfData) switch (rfData) { case "851201": { if (sonoffwall3_p3.state == ON) { logInfo("rule SONOFF BRIDGE", "-> sonoffwall3_p3.sendCommand(OFF)") sonoffwall3_p3.sendCommand(OFF) } else { logInfo("rule SONOFF BRIDGE", "-> sonoffwall3_p3.sendCommand(ON)") sonoffwall3_p3.sendCommand(ON) } // Reset Last Received Code rfData="" button1_rf.postUpdate(rfData) } } end |
Code can be re-used for multi button switch which generates separate codes for any. You have to add additional “case <code>:” section. I hope you find this helpful.