Light switch - change to detached mode when connection to HA is lost

Hi,

How can i configure esp32 light switch to work in detached mode when it has connection t home assistant and when it lose the connection to switch into relay control mode?

It’s easy by just doing something like this:

binary_sensor:
  - platform: gpio
    # ...
    on_state:
      then:
        - if:
            condition:
              api.connected:
            then:
              - logger.log: "We are going full detached now because the api is connected"
              - homeassistant.event: esphome.button_pressed # action when api is connected
            else:
              - logger.log: "No api connection - let's solve things locally"
              - light.toggle: younameit
4 Likes

ooooo that looks way to easy, btw do you use orangePi for the home devices?

1 Like

:joy: not anymore… but my first ha server was running on a $10 $9.99 orange pi one back in 2017:

That DHT22 Sensor was quite expensive compared to the ha server actually :stuck_out_tongue_winking_eye:

1 Like

hi, do you mind sharing your config for detached mode with the relay?

That depends on the devices, gpio etc. will be different

Something like

maybe?

while changing the last line from light.toggle to switch.toggle should have you covered with a relay (configured as switch) :raised_hands:

Sorry, probably should’ve rephrased my question

Let’s say it reconnects back to wifi, but the last actual relay status was off. How do i have the relay turn back on? Do i just toggle it back on in HA

Also, if i toggle the relay in HA, can I have it send on/off commands to the lights instead of toggling the actual relay?

1 Like

As for relay I do not think so, in esphome it is just GPIO output, but you can create template switch. You can do “interval” in esphome and do some action when there is no connection to api. Just an idea, don’t know exactly how that would work.

Basically:
binary sensor switch - ?can connect to api? - just send to HA switch was pressed (do nothing, let HA handle on press action), if no api connection toggle gpio relay (ssr, triac).

I do not know about better way to it.

1 Like

This indeed includes some more logic and depends on your personal needs.

For me I want a action the second I press a switch - even in case the device I want toggle is “not available” for some reason - I prefer a light/sound feedback which indicates the failure of the action rather then postpone it to a (unknown) time (minutes, hours, days?) the device might back come online.

But you can surely figure out something in HA which “queues” commands that are not successfully executed (because a device is offline) and fires them again when a device comes back online.

Sure. I probably do something like this. Having 3-gang wall switches with 3 buttons that actually toggle (remote esphome) lights and not the internal relays.

1 Like

I wonder if it would be possi le to do espnow to control the bulbs while still keeping connection to HA?

I have a template number on my Shellys running esphome. Flicking the switch cycles the input number. Each bulb is also esphome and has it’s own input number. When the input number on the switch changes, it updates all the bulbs in it’s settings. If the network is down, it just acts like a normal switch and cuts or restores power.

Here’s some code. It works for my stuff but probably won’t work for you unless you have the exact same stuff. Just some example code.

Switch physical switch:

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO13
    name: "${switch_1_friendly_name} Switch Now"
    on_state:
      then:
      - number.increment:
          id: "${switch_1_name}_scene"
          cycle: true         

Switch template number:

number:
  - platform: template
    id: "${switch_1_name}_scene"
    name: "${switch_1_friendly_name} Scene"
    optimistic: true
    min_value: ${light_automation_1_input_number_min}
    max_value: ${light_automation_1_input_number_max}
    restore_value: true
    step: 1
    on_value:
      then:
      - if:
          condition:
            - wifi.connected:
          then:
            - if:
                 condition:
                   - lambda: return (${energy_saving_1} && (${off_scene_1}.0 == x));
                 then:
                   - switch.turn_off: shelly_relay_1
                 else:
                   - switch.turn_on: shelly_relay_1
            - lambda: id(scene_light_index_1) = 0;
            - while:
                 condition:
                   - lambda: return (id(scene_light_index_1) < id(scene_light_host_string_1).size());
                 then:
                   - http_request.post: !lambda |- 
                               std::string sceneName = "http://" + id(scene_light_host_string_1).at(id(scene_light_index_1)) + "/number/scene/set?value=" + std::to_string(int(x));
                               ESP_LOGI("scene", "Setting vanity right: [%s]", sceneName); 
                               id(scene_light_index_1) += 1;
                               return sceneName;  
          else:
            - if:
                 condition:
                   - lambda: return (${off_scene_1}.0 == x);
                 then:
                   - switch.turn_off: shelly_relay_1
            - if:
                 condition:
                   - lambda: return (${on_scene_1}.0 == x);
                 then:
                   - switch.turn_on: shelly_relay_1
            - if:
                 condition:
                   - lambda: return ((${off_scene_1}.0 == x) && (${on_scene_1}.0 == x));
                 then:
                   - number.set:
                       id: "${switch_1_name}_scene"
                       value: ${off_scene_1}
      - if:
           condition:
             - lambda: return (${auto_off_boolean_1} && ${off_scene_1}.0 != x);
           then:
             - delay: ${delay_time_1}
             - number.set:
                 id: "${switch_1_name}_scene"
                 value: ${off_scene_1}

bulb template number:

number:
  - platform: template
    id: "scene"
    name: "scene"
    optimistic: true
    min_value: ${light_automation_1_input_number_min}
    max_value: ${light_automation_1_input_number_max}
    restore_value: false
    step: 1
    on_value:
      then:
        - if:
            condition:
              - lambda: return (0.0 == x);
            then:
              - light.turn_off:
                  id: "${id}_rgbct_light"
                  transition_length: 1s
        - if:
            condition:
              - lambda: return (1.0 == x);
            then:
              - light.turn_on:
                  id: "${id}_rgbct_light"
                  color_mode: "COLOR_TEMPERATURE"
                  brightness: 32%
                  color_temperature: 2600 K
        - if:
            condition:
              - lambda: return (2.0 == x);
            then:
              - light.turn_on:
                  id: "${id}_rgbct_light"
                  color_mode: "COLOR_TEMPERATURE"
                  brightness: 100%
                  color_temperature: 3000 K
        - if:
            condition:
              - lambda: return (3.0 == x);
            then:
              - light.turn_on:
                  id: "${id}_rgbct_light"
                  color_mode: "COLOR_TEMPERATURE"
                  brightness: 100%
                  color_temperature: 6500 K

1 Like

Hi, this looks like another way of syncing your esphome lights similar to tasmota group
Does this also work for adjusting brightness & colors?

Should be yes. But the custom component your are linking regarding esp-now is abandoned since a long time. Instead check out the more recent work from @tomrusteze :point_down:

1 Like

That does not look so convoluted, to me, now, maybe i’m wrong, but wouldn’t using this kind of setup make more sense (if stable enough) then the ssr/triac failsafe?

HA would know the status of the lights even when controlled by esphome switch using “esp now” link.

This kinda seems like, at least at the first glance, as ideal of all the non-wired-bus-using alternatives for the real world to me, it gets away with zigbee pairing, does not need coordinator, or wifi,
so even better then ble proxy as that only solves zigbee pairing issues.
It does not work for other things that lights, but still, for the lights, i don’t see disadvantage. I mean, when something is so broken then only thing that works is to severe electrical connection you might as well pull the breaker.

Update: Guess it is not finished Improvements: Add support for more types of lights, such as RGBCT.
But it should still work for on/off even for those.

Guess time for the bench cleanup and some testing :slight_smile:

It does if you program it that way.

In my example, the use case was wanting the lights to start out dim then be able to cycle them to bright. Also I was wanting them to have a redder temp (I always get confused whether red is cooler or warmer than blue. it’s colder on the spectrum but people consider it warm) because it looks nicer, but also wanting to be able to cycle a whiter temp for task lighting.

However it be easy enough to set the color. You can set brightness, color, temp, etc. in the light.turn_on service. You’ll need to set a compatible color_mode. In my example “COLOR_TEMPERATURE” wouldn’t support an RGB setting. However RGB is a setting that supports RGB.

This explains it in better detail. Light Component — ESPHome

You can also set your esphome switches to sync. For example I have a switch that does nothing except turn on or off all the switches in my house. I installed it right by my bed, saves a lot of stubbed toes in the middle of the night. The code pattern is the same used in the example. Except updating a switch instead of a bulb.