Create a light and a switch based on the same gpio

I’m trying to expose a single gpio both as a light and a switch, with synchronised states and surviving states after reboot. The doc and ChatGPT make me run in circles… Any ideas?

In esphome yaml create light

Create a seperate switch in esphome yaml.
Have switch state change with light state and vice versa.

The switch would basically be a script that runs when light state change or when switch state changes

I don’t think it needs to be that complicated. Try this (untested):

output:
  - platform: gpio
    pin: GPIOXX # choose a GPIO pin
    id: gpio_light_and_sw

light:
  - platform: binary
    name: "Light"
    output: gpio_light_and_sw

switch:
  - platform: gpio
    pin:
      number: GPIOXX # use the same GPIO pin here
      allow_other_uses: true
    name: "Switch"
    restore_mode: RESTORE_DEFAULT_OFF  # restore state or if not possible turn off, could aslo be RESTORE_DEFAULT_ON

Thanks, the “allow_other_uses: true” looks very promising. This should be somehow mentioned in the error message during compilation.

The documentation doesn’t mention any kind of unwanted side effect or risk, as this is not enabled by default I do wonder if there is any potential drawback?

Not really.
It’s disabled by default because it can be confusing to someone and can lead to some result that user didn’t expect.

Light can be on, switch can be off, gpio can have just one state…

OK, I tested and it doesn’t really work. It does compile, but it does not automatically synchronize the states between the output-based light and the gpio-based switch.

I will need to go back to the not-very-elegant solution of an output using the gpio, amd then a switch and a light bosed based on the output that use “on_turn_on” and “on_turn_off” to sync each other state. It does work, but it feels clumsy.

Ah ok. I wasn’t 100% sure it would

Thanks for the feedback. I won’t recommend it in future.

Yeah as suggested by tmjpugh, the automation will be the way to do it. You may also need an on_boot trigger to synchronise them after a restart.

However it still seems a very odd thing to do.

Why exactly do you need both a light and a switch?

1 Like

The on_boot trigger doesn’t seem necessary, states are restored without it.

I need this because I will use a Kincony F24 with a bunch of relays. Some of them will be switches to create cover objects with interlocks, others will be used for heating, and others for lights. Besides the ‘high-level’ objects such as the covers, assigned to areas, I want to have access directly to the switches themselves so that I can turn them on and off independently of what they’re used for.

So…helpers can make a switch a light

It basically hides the switch and makes an associated light

This would be another solution, but I want to keep as much as possible in ESPHome.

Yes but none need to be a light and a switch at the same time.

You can do that with a light. There is no need to duplicate it with a switch.

You already got two switches on same gpio. Do you have some practical purpose to get one of the switches to be presented as light?