Wall switch to toggle state and show in HA frontend as a switch

Hi struggling to get this code correct, so any pointers would be gratefully received.

Im trying to get standard household toggle wall switches to appear in home assistant as a switch so the can be controlled in automations switch scenes, or just be standard light switches etc. The switches do not have any lights attached physically and are wired to d1 mini pin and back to ground pin.

i don’t mind which what the toggle is so happy that state change toggles the HA switch.

I have it set up as a binary sensor but the doesn’t let me switch it in HA frontend. So i’m trying to get it to switch a template switch and make the binary sensor internal.

This is where i have got to;

esphome:
  name: lsc2
  on_boot:
    priority: 600
    then:
      - binary_sensor.template.publish:
          id: template_sw1
          state: ON
      - binary_sensor.template.publish:
          id: template_sw2
          state: OFF

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "t################="

ota:
  password: "#####################"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

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

captive_portal:

  # Physical wall switch sensor automation
binary_sensor:
  - platform: gpio
    pin:
      number: 5
      mode: INPUT_PULLUP
    name: Wall Switch 1
    id: wallswitch1
    internal: true
    filters:
      - delayed_on: 10ms
      - delayed_off: 10ms

#Automation
    on_state:
      then:
        - lambda: |-
            lambda: 'return id(template_sw1).state;'
            then:
              - switch.template.publish:
                  id: template_sw1
                  state: OFF
            else:
              - switch.template.publish:
                  id: template_sw1
                  state: ON

# Virtual switches
  - platform: template
    name: "left bed switch 1"
    id: template_sw1
  - platform: template
    name: "left bed switch 2"
    id: template_sw2

You had some YAML mixed in with the on_state lambda. The lambda would need to be all c code. Using the on_state trigger requires extra logic and processing. The on_press and on_release triggers should execute once whenever the gpio state changes and the switch toggle action can be used to flip the switch state.

binary_sensor:
  - platform: gpio
    pin:
      number: 5
      mode: INPUT_PULLUP
    name: Wall Switch 1
    id: wallswitch1
    internal: true
    filters:
      - delayed_on: 10ms
      - delayed_off: 10ms
    #when gpio state changes, toggle the switch state
    on_press:
      then:
        - switch.toggle: template_sw1
    on_release:
      then:
        - switch.toggle: template_sw1        

# Virtual switches
switch:  ### added
  - platform: template
    name: "left bed switch 1"
    id: template_sw1
    optimistic: true  ### added
  - platform: template
    name: "left bed switch 2"
    id: template_sw2
    optimistic: true  ### added

Why not use a switch component?

Thank you that works perfectly, looks so much simpler also. i’m going to read about the optimistic line.

When i was trying it it seamed to be an output, i needed inputs

Yeah, silly me!