Smart switch + smart light setup that works with/without openhab

Smart Switch with smart light bulb setup

Traditionally if you have a smart light bulb and a dumb mechanical switch, you don’t want anyone to turn off that mechanical switch. It must stay “ON”, so you can control the smart bulb via openhab.

Now replace that dumb mechanical switch with a smart switch (based on Tasmota), which can decouple the button that you press from the relay that provides/cuts off electricity to the bulb. This way, the relay can stay on, and the button presses can control the smart bulb.

It has taken me quite some time to come up with the ideal set up involving smart light bulb + smart switch, so that it works with or without OpenHAB running, and that it recovers from black outs “properly” (i.e. not end up with all the smart bulbs turning on when the power is restored in the middle of the night).

Definitions

  • Smart switch = the switch on the wall that has a relay to cut off electricity to the light bulb

  • Smart light bulb = a smart bulb that normally requires to be powered all the time

Requirements

  • The Smart switch has been flashed with Tasmota
  • The smart light bulb can be Tasmota, or a Zigbee, or Tuya, as long as it can be configured to turn on when connected to electricity regardless of its previous state

Functional Description

  • The light switch / light works with or without OpenHAB
  • When OpenHAB/Wifi/Network is down, the light switch acts like a normal dumb switch:
    • Single click toggles the relay, and therefore the light on/off, 100% brightness.
  • When OpenHAB is up, the switch relay will stay on, the light switch button gains enhanced features (adjust to suit your. needs) because openhab controls the smart bulb:
    • Single click: toggle on/off to the last brightness
    • Double click: performs light dimming feature by cycling through brightness levels: 1%, 25%, 50%, 75%, 100%
    • Triple click: set brighness to 100%
    • Quad click: toggle warm/cool
    • Hold: Toggle relay to cut off or provide electricity to the bulb

Implementation Overview:

  • When power is first applied, or when recovering from a blackout:
    • Openhab is down
    • The smart switch relay is OFF (PowerOnState = 0) → no electricity flowing to the smart bulb → the bulb is off
  • The bulb is configured to turn on (light on) when it receives electricity (PowerOnState = 1), and set to 100% brightness and a preset color temp
  • Before Openhab starts, or if openhab is down, pressing the button on the smart switch will toggle the relay.
  • When openhab starts, it sends commands to the smart switch to:
    • decouple button - relay (SetOption73 1)
    • The relay stays in the ON position so the bulb gets electricity all the time
    • The first time the light switch button is pressed, openhab will send a command to turn on the relay, thus sending electricity to the bulb. The initial command to the bulb itself probably won’t be seen by the bulb as it is just booting up. From here on out, openhab will control the bulb, whilst the smart switch / relay stays on.
  • If openhab crashed, its LWT will cause the smart switch to go back to its ‘dumb’ behaviour, cutting off electricity to the bulb to toggle the light
  • The same when openhab shuts down gracefully.

The smart light bulb:

Backlog PowerOnState 1; SetOption65 1
Rule1 on System#boot do dimmer 100 endon
rule1 1

Note: SetOption65 1 disables the Power Cycle Device Recovery feature so its firmware doesn’t get reset when someone’s playing with the switch.

For Tuya / Zigbee light bulbs, check the settings on their corresponding app. The main thing is to make it always turn on its light on power recovery, and not to “save previous state”.

Smart Switch:

rule1 on mqtt#disconnected do backlog SetOption73 0 endon
backlog PowerOnState 0; SetOption13 0; GroupTopic2 smartlightswitches; rule1 1

Openhab

Openhab MQTT Bridge config:

This feature is available since OpenHAB 3.3.0.M1.

birthTopic="cmnd/smartlightswitches/backlog",
birthMessage="SetOption73 1",
lwtTopic="cmnd/smartlightswitches/backlog",
lwtMessage="SetOption73 0",
shutdownTopic="cmnd/smartlightswitches/backlog",
shutdownMessage="SetOption73 0"

OpenHAB Rule:

  • Handle button presses to send the appropriate commands to the smart bulb
  • When smart bulb power item received command ON: ensure that the smart switch relay is ON too

Reserved