New ESPHome user- can't see a switch in the entity list

I’ve been using Tasmota for a couple of years on more than a dozen devices around the house. ESP Home looks like a better integration with Home Assistant, so I wanted to give it a try.
My first test was with a NodeMCU. Home Assistant found the switch I defined in the YAML file, but no state change was detected. [Is it pull down or pull up? I tried both.]
Having no success I compiled ESP Home for a Wemos D1 Mini and flashed it. This time, the switch doesn’t show up in the entities.

Any tips of what I may have done wrong?

esphome:
  name: wemos_test
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: "Kaywinnet"
  password: "806194xxxxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Wemos Test Fallback Hotspot"
    password: "Oi136ZHGIzON"

captive_portal:

# Enable logging
logger:
  level: DEBUG

# Enable Home Assistant API
api:

ota:
  safe_mode: True
  
# Switch on D4, GPIO2  
switch:
  - platform: gpio
    name: "Wemos_Test"
    pin: D4  

But no logs, no issue :slight_smile:

Thanks, but that wasn’t the question.

I should be seeing an entity: switch.wemos_test, but I am not. That is the question.

UPDATE: I didn’t realize that I had to run configuration again, but now the switch is in the list of entities. Now- How do I get the switch to respond. I pull the GPIO pin to ground or to 3V3, but the entity switch.wemos_test remains “off”.
I will start a new thread since my YAML and log is substantially different, and my issue has changed.

switch:
  - platform: gpio
    name: "Wemos_Switch"
    pin:
      number: D1
      inverted: False
      mode: INPUT_PULLUP

And in the log it shows as an input.
But, still I see no switch activity.

INFO Reading configuration /config/esphome/wemos_test.yaml...
INFO Starting log output from wemos_test.local using esphome API
INFO Connecting to wemos_test.local:6053 (192.168.1.59)
INFO Successfully connected to wemos_test.local
[15:23:51][I][app:105]: ESPHome version 1.16.2 compiled on Mar 11 2021, 13:03:42
[15:23:51][C][wifi:443]: WiFi:
[15:23:51][C][wifi:303]:   SSID: [redacted]
[15:23:51][C][wifi:304]:   IP Address: 192.168.1.59
[15:23:51][C][wifi:306]:   BSSID: [redacted]
[15:23:51][C][wifi:307]:   Hostname: 'wemos_test'
[15:23:51][C][wifi:311]:   Signal strength: -64 dB ▂▄▆█
[15:23:51][C][wifi:315]:   Channel: 6
[15:23:51][C][wifi:316]:   Subnet: 255.255.255.0
[15:23:51][C][wifi:317]:   Gateway: 192.168.1.1
[15:23:51][C][wifi:318]:   DNS1: 192.168.1.1
[15:23:51][C][wifi:319]:   DNS2: (IP unset)
[15:23:51][C][switch.gpio:042]: GPIO Switch 'Wemos_Test'
[15:23:51][C][switch.gpio:043]:   Pin: GPIO2 (Mode: OUTPUT)
[15:23:51][C][switch.gpio:059]:   Restore Mode: Restore (Defaults to OFF)
[15:23:51][C][logger:185]: Logger:
[15:23:51][C][logger:186]:   Level: DEBUG
[15:23:51][C][logger:187]:   Log Baud Rate: 115200
[15:23:51][C][logger:188]:   Hardware UART: UART0
[15:23:51][C][captive_portal:169]: Captive Portal:
[15:23:51][C][ota:029]: Over-The-Air Updates:
[15:23:51][C][ota:030]:   Address: wemos_test.local:8266
[15:23:51][C][api:095]: API Server:
[15:23:51][C][api:096]:   Address: wemos_test.local:6053

The first version should work as is. There is nothing obviously incorrect, so the switch should appear in HA after setting it up via integrations. You should debug here and not trying by accident :smiley: :smiley: :smiley:

The second config is not that wrong, you would use that for example with a binary_sensor to use a physical button on your chip (like with the Sonoffs).

Here is a very simple example from one of my Sonoffs:

esphome:
  name: sonoff_rocket
  platform: ESP8266
  board: esp01_1m

#snip wifi, ap, ota and all that stuff

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    name: "Sonoff Rocket Button"
    id: sonoff_basic_rocket_button
    internal: true
    on_press:
      - switch.toggle: relay

switch:
  - platform: gpio
    name: "Sonoff Rocket Relay"
    pin: GPIO12
    id: relay

As you can see, i have just a switch to call by HA, and for the physical button there is a binary_sensor that is set internal aka not-shown-in-HA. If you push the button, you toggle the relay aka the switch inside your board (ESPx-NodeMCU, D1).

So it seems, something else is wrong that needs to be debuged, not changed. :slight_smile:

Thanks for the example- it’s starting to sink in.

It does work now, simulating a Sonoff Basic on a Wemos MD1 Mini. I think that I understand what I have. (I am almost a complete doofus when it comes to YAML code).

Do I even need a name on the binary sensor?

# Physical Button on D1 (GPIO5)  
binary_sensor:
  - platform: gpio
    pin: 
      number: D1
      inverted: False
      mode: INPUT_PULLUP
    name: Wemos_Switch"
    internal: true
    on_press:
      - switch.toggle: led

# Virtual switch D0, GPIO16
# This is what I want the physical switch to control.
switch:
  - platform: gpio
    name: "Wemos_Switch"
    pin:
      number: D0
      mode: output
    id: led

In Tasmota I can say the physical switch is a button (toggles) or a switch (on/off).
This YAML is acting like a Sonoff Basic where the button toggles the output. How can I configure this so that the LED on D0 (my “virtual” switch) only toggles when the state of the physical switch changes?

You assign actions to the binary sensor (please don’t call it a switch for clarity sake).
On_press, on_release etc

The esphome docs are really comprehensive. I’d recommend reading the guide section then binary sensor, switch and output sections to start with.

I have several pages of the docs open at a time, but it never occurred to me that a switch is an output.

But I really do appreciate you getting me this far.

1 Like

Some of the examples are very helpful. There are a few places to get them:

The cookbook ESPHome — ESPHome

The DIY examples DIY Examples — ESPHome

And here https://www.esphome-devices.com/

The LED is another thing in ESPHome. Like so:

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    name: "Sonoff Rocket Button"
    id: sonoff_basic_rocket_button
    internal: true
    on_press:
      - switch.toggle: relay

switch:
  - platform: gpio
    name: "Sonoff Rocket Relay"
    pin: GPIO12
    id: relay

status_led:
  pin:
    number: GPIO13
    inverted: yes

In ESPHome everything is handled “on it’s own” and will afterwards be combined. In my example, the binary_sensor is the physical button to press. You might expect that this button does something physical on the board, but that’s not the case. See it that simple: everything that can do something, is one part in ESPHome. The button, the relay, the LED.
And these need to be combined, that’s why you call an id with your binary_sensor and that id is the relay. If you step back a little and take a wider look at ESPHome, it is surprisingly that simple. :smiley: :slight_smile:

That’s the beauty of ESPHome, because everything is one “component” and can be controlled on it’s own, you have the possibility to make a simple relay in your wall a light in HA. :slight_smile: Here is one, as well very simple, example for a Shelly device. In principle a relay that is behind my light switch. It can be controlled via HA or with the physical wall switch. :slight_smile:

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO5
    id: shelly1_binary
    internal: true
    on_state:
      then:
        - light.toggle: shelly1_light

output:
  - platform: gpio
    pin: GPIO4
    id: shelly1_output

switch:
  - platform: gpio
    name: "Shelly1 Relay"
    pin: GPIO4
    id: shelly1_relay
    restore_mode: RESTORE_DEFAULT_OFF

light:
- platform: binary
  name: "Shelly1"
  output: shelly1_output
  id: shelly1_light

Hope that clears it up a little, the help pages can be overwhelming with ESPHome :smiley: :smiley: :smiley:

Yes, thank you. Your Shelly example does clear things up a bit, (and adds more questions). I’ve spent the evening reading the cookbook and DIY examples and gleaned some ideas - and many more questions.

Where do I find documentation of “on_state” and “on_press” and other conditions? Is this unique to ESPHome or is it generic to YAML? Where are the various platforms described?

I am sure that once it clicks I will be converting many of my devices with Tasmota to ESPHome.

Under the component, in this case it is a binary_sensor, so here:

The way you have to write it is generic YAML. The things you write in YAML language is ESPHome specific. :slight_smile:

The start page from ESPHome. :slight_smile: The left side navigation is a little more usable. :wink:

Look at the binary_sensor component. Every component has a core part, and some extensions to the specific component, for example a touch input for a binary_sensor.

Written in ESPHome this looks something like this:

esp32_touch:
  setup_mode: false

binary_sensor:
  - platform: esp32_touch
    name: "ESP32 Touch Pad GPIO27"
    pin: GPIO27
    threshold: 700
    on_press:
      then:
        - light.toggle: ledstrip

light:
  - platform: fastled_clockless
    chipset: WS2812B
    pin: GPIO5
    num_leds: 12
    rgb_order: GRB
    id: ledstrip
    name: "ledstrip"
    effects:
      - addressable_rainbow:
      - addressable_rainbow:
          name: Rainbow Effect With Custom Values
          speed: 10
          width: 12

A button to control a light. :wink: You have a button (as before, but touch) and a light (as before, but a ledstripe). You would use here:

  • binary_sensorcore
  • binary_sensortouch
  • lightcore
  • lightledstripe (in this case WS2812B LEDs)

You get the idea? :wink: :slight_smile:

I’m getting somewhere, but I think my lack of experience with YAML is getting in my way.

For example, I want the LED on my Wemos to follow the button state. As long as the button is down, I want the LED on. Release the button and the LED should be off.

I can do it with an automation in Home Assistant because the state of binary_sensor.wemos_test_button is what I want the LED to follow. But shouldn’t I be able to do this completely within ESPHome?

Yes you can. Have you read the instructions?

Read every page in the first panel.

Partial example here: this is from my mains meter reader, which has a sensor watching the light on the front of the meter that pulses for every watt-hour consumed. The meter is outside, and I wanted to “copy” the flashing light to my box, which is inside.

box_led is in my unit, driven by pin D1 of my ESP device (D1 Mini). It is inverted because I’ve wired the ESP as a current sink, so HIGH is off. The light sensor from the meter is on pin D2:

switch:
  - platform: gpio
    pin:
      number: D1
      inverted: yes
    name: "Garage sensor box LED"
    id: "box_led"
    internal: true

binary_sensor:
  - platform: gpio
    pin: D2
    name: "Meter pulse mirror"
    internal: true
    on_press:
      then:
        - switch.turn_off: box_led
    on_release:
      then:
        - switch.turn_on: box_led

There’s more to the code than this, but it shows what you can do. I agree with @nickrout though — do work your way through the excellent and comprehensive documentation.

Thanks (also NickRout) I am learning a lot. I’m reading the components pages now, doing some experiments and making notes in my lab notebook.

I am finding some duplicates in the documentation. For example:
//esphome.io/components/switch/index.html#switch-is-on-switch-is-off-condition
is virtually identical to:
//esphome.io/components/switch/index.html#switch-is-on-off-condition

I appreciate the hand-holding that’s gotten me this far. Thanks to your guidance I can replicate a Sonoff Basic on a NodeMCU and on a Wemos D1 Mini. (I will mark the thread as solved.)

This morning, one of my devices was “unavailable” on Lovelace.
Here’s what I found.
In Developer Tools/States, I see duplicates of the entities for my switch:
switch.wemos_switch (off)
and
switch.wemos_switch_2 (off)

So, where did the _2 come from?

Either you’ve set two devices up with the same name, or you’ve reloaded the existing one over the new config. HA is avoiding a clash of names by adding the _2. You should be able to delete one and rename the other if needed from Configuration / Entities.