Custom Switch


Custom components are deprecated, not recommended for new configurations and will be removed from ESPHome in a future release. Please look at creating a real ESPHome component and “importing” it into your configuration with External Components.

You can find some basic documentation on creating your own components at Contributing to ESPHome.


While we try to keep the ESPHome YAML configuration options as stable as possible, the ESPHome API is less stable. If something in the APIs needs to be changed in order for something else to work, we will do so.

This component can be used to create custom switches in ESPHome using the C++ (Arduino) API.

Please first read Custom Sensor Component guide, the same principles apply here.

The example below is an example of a custom switch; this custom switch is essentially the same as the gpio switch implementation.

#include "esphome.h"

class MyCustomSwitch : public Component, public Switch {
  void setup() override {
    // This will be called by App.setup()
    pinMode(5, INPUT);
  void write_state(bool state) override {
    // This will be called every time the user requests a state change.

    digitalWrite(5, state);

    // Acknowledge new state by publishing it

(Store this file in your configuration directory, for example my_switch.h)

And in YAML:

# Example configuration entry
    - my_switch.h

- platform: custom
  lambda: |-
    auto my_custom_switch = new MyCustomSwitch();
    return {my_custom_switch};

    name: "My Custom Switches"
    id: my_custom_switch

Configuration variables:

  • lambda (Required, lambda): The lambda to run for instantiating the switch(es).

  • switches (Required, list): A list of switches to initialize. The length here must equal the number of items in the return statement of the lambda.

See Switch

See Also