Control Smart Bulbs Unless HASS Down Then Control Relay

I thought I read that someone else had done this at some point, but I’m not having any luck finding it unfortunately and am hoping someone can help me out.

Basically, my light switches trigger a binary sensor that HASS then sees the state change on and then HASS controls my smart bulbs.

Every now and then HASS goes down for some reason or I lose wireless, in which case my switches can’t communicate with HASS and don’t work at all since all they know to do is change a binary sensor while the button is pressed. Ideally, I’d like the switches to always act as a dumb light switches even if the automation platform is down and I’d ideally like the switch to trigger the relay instead of just its binary sensor.

So, I believe I’d need some logic to know if HASS is available and then some logic to say:


If HASS==online THEN
     If relay==offline THEN turn it on
     --AND --
     Trigger the binary sensor //in case HASS is supposed to adjust colors/brightness
Else 
     Toggle the relay

No longer available, but these are the light switches themselves too.

My existing YAML below in case anyone has any ideas, but my description wasn’t clear. As you can see, the relay is configured and exposed to HASS in case I ever need to power cycle the smart bulbs, but other than that, basically doesn’t do anything w/in ESPHOME itself.

Thanks in advance all!

esphome:
  name: hallway02_switch
  platform: ESP8266
  board: esp01_1m
  on_boot:
    priority: 700
    # ...
    then:
      - switch.turn_on: relay
      
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: true
  domain: .MYinternalDOMAIN.com
  manual_ip:
    static_ip: 10.1.100.161
    gateway: 10.1.100.1
    subnet: 255.255.255.0

logger:

# Enable Home Assistant API
api:

ota:
  safe_mode: true

web_server:
  port: 80

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: true
    name: "Hallway Lightswitch (Bedrooms)"
    on_click:
      then:
        - logger.log: "Hallway Lightswitch (Bedrooms) Clicked"

switch:
  - platform: gpio
    name: "Hallway Lightswitch Power"
    pin: GPIO12
    id: relay

status_led:
  pin:
    number: GPIO13
    inverted: no

api.connected Condition

This Condition checks if at least one client is connected to the ESPHome native API

Could properly use something like this and extend/modify to your likes

    on_click:
      then:
        if:
          condition:
            api.connected:
          then:
            - homeassistant.service:
                service: homeassistant.toggle
                data:
                  entity_id: light.smart_bulb # HA connected light
          else:
            - switch.toggle: relay # local relay

You’re Awesome! THANK YOU!

I don’t control the bulbs directly through ESPHOME->HASS like that because it triggers an automation that does different things based on the time (light goes red at night, different brightnesses, etc…), but I was able to take that logic and plop it right in there and tested it successfully.

If anyone is interested:

#api connected via indeeed - https://community.home-assistant.io/t/control-smart-bulbs-unless-hass-down-then-control-relay/704984/2
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: true
    name: "Hallway Lightswitch (Bedrooms)"
    on_click:
      then:
        if: 
          condition:
            api.connected:
          then:
            - switch.turn_on: relay #May try to add another IF here, depending on how it works out
            - logger.log: "Hallway Lightswitch (Bedrooms) Clicked - API CONNECTED"
          else:
            - switch.toggle: relay
            - logger.log: "Hallway Lightswitch (Bedrooms) Clicked - OFFLINE"

Now I just need to add similar logic to all my lights that are connected in this way!

Thanks Again!

1 Like

For smart lights toggle, dimming i just use device group so this doesn’t rely on HA

If it also should withstand wifi outages the choice would be properly something esp-now based

1 Like

Yep, espnow would be the better choice
But I couldn’t figure out how to use it with bthome because the lights would act as transmitter so it won’t receive any packets (eg button clicks from switch).

Or can you configure multiple receivers (lights) with 1 transmitter (switch)?
I prefer tasmesh for now