Is it possible to make a switch. appear as a light. from esphome?

I have a sonoff mini that is in the ceiling for a light. At the moment it appears as switch. in home assistant but I’d prefer it to be a light. since it is a light in this instance. Is this possible?

I believe this can be done in Home Assistant itself but I like to keep all the changes together so not to get confused if and when I improve or update stuff later on.

This is my current config if it helps

substitutions:
  ip_address: 192.168.1.XX
  gateway_address: 192.168.1.1
  subnet_address: 255.255.255.0
  dns1_address: 192.168.1.1
  dns2_address: 192.168.1.1
  timeout_reboot: 5min
  devicename: bedroom2_light_ceiling_sonoff_mini
  upper_devicename: Bedroom2 Light Ceiling
  ap_name: bdrm2_ceil_lght
  

esphome:
  name: $devicename
  platform: ESP8266
  board: esp01_1m

# WiFi connection
wifi:
  networks:
    - ssid: !secret wifi_ssid_NoT
      password: !secret wifi_password_NoT
      priority: 10

  #enable acess point access if no wifi
  ap:
    ssid: $ap_name
    password: !secret ota_password

  manual_ip:
    static_ip: $ip_address
    gateway: $gateway_address
    subnet: $subnet_address
    dns1: $dns1_address
    dns2: $dns2_address
  reboot_timeout: $timeout_reboot

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: !secret api_password
  reboot_timeout: $timeout_reboot

# Enable over-the-air updates
ota:
  password: !secret ota_password

# Enable Web server
web_server:
  port: 80

# Sync time with Home Assistant
time:
  - platform: homeassistant
    id: homeassistant_time

# Text sensors with general information
text_sensor:
  - platform: version
    name: $upper_devicename version
  - platform: wifi_info
    ip_address:
      name: $upper_devicename ip
    ssid:
      name: $upper_devicename ssid
    bssid:
      name: $upper_devicename bssid

sensor:
  # Uptime sensor
  - platform: uptime
    name: $upper_devicename uptime
  # WiFi Signal sensor
  - platform: wifi_signal
    name: $upper_devicename wifi signal
    update_interval: 10s

status_led:
  pin:
    number: GPIO13
    inverted: true

binary_sensor:
  - platform: gpio
    name: $upper_devicename button
    pin: GPIO00
    id: reset
    internal: true
    filters:
      - invert:
      - delayed_off: 10ms
    on_press:
      - switch.toggle: 
          id: relay_1

  - platform: gpio
    #name: $upper_devicename switch
    pin: GPIO04
    id: switch_1
    on_press:
      then:
        - switch.toggle:
            id: relay_1
   # on_release:
   #   then:
   #     - switch.toggle:
    #        id: relay_1

switch:
  - platform: gpio
    name: $upper_devicename switch
    icon: "mdi:lightbulb"
    pin: GPIO12
    id: relay_1
    restore_mode: ALWAYS_OFF

Thank you.

Yes, use the binary light component

You can define light as following template:

- platform: template
    lights:
      # Moes switch ured
      moes_wall_switch_03_01:
        friendly_name: "Ured 1/1"
        unique_id: moes_wall_switch_03_01
        level_template: >-
          {% if is_state('switch.moes_wall_switch_03_01','on') %}255{% else %}0{% endif %}
        value_template: >-
          {{ is_state('switch.moes_wall_switch_03_01','on') }}
        turn_on:
          service: switch.turn_on
          entity_id: switch.moes_wall_switch_03_01
        turn_off:
          service: switch.turn_off
          entity_id: switch.moes_wall_switch_03_01
        set_level:
          service_template: >-
            {% if brightness > 0 %}switch.turn_on{% else %}switch.turn_off{% endif %}
          data:
            entity_id: 'switch.moes_wall_switch_03_01'

I did this for all my light switches.

Thank you for your help, however my knowledge is not great on this as you can probably tell.
Would I need to change my switch to a output and then add the binary light code?

Thank you for you help however this is exactly the complexity I’m trying to avoid.

Replace the above with:

output:
  - platform: gpio
    pin: GPIO12
    id: relay_1

light:
  - platform: binary
    name:  $upper_devicename switch
    output: relay_1

You’ll alswo need to change - switch.toggle: to - light.toggle:

Thank you again for your help with this.

So I changed this as you have described

substitutions:
  ip_address: 192.168.1.XX
  gateway_address: 192.168.1.1
  subnet_address: 255.255.255.0
  dns1_address: 192.168.1.1
  dns2_address: 192.168.1.1
  timeout_reboot: 5min
  devicename: bedroom2_light_ceiling_sonoff_mini
  upper_devicename: Bedroom2 Light Ceiling
  ap_name: bdrm2_ceil_lght
  

esphome:
  name: $devicename
  platform: ESP8266
  board: esp01_1m

# WiFi connection
wifi:
  networks:
    - ssid: !secret wifi_ssid_NoT
      password: !secret wifi_password_NoT
      priority: 10

  #enable acess point access if no wifi
  ap:
    ssid: $ap_name
    password: !secret ota_password

  manual_ip:
    static_ip: $ip_address
    gateway: $gateway_address
    subnet: $subnet_address
    dns1: $dns1_address
    dns2: $dns2_address
  reboot_timeout: $timeout_reboot



# Enable logging
logger:

# Enable Home Assistant API
api:
  password: !secret api_password
  reboot_timeout: $timeout_reboot

# Enable over-the-air updates
ota:
  password: !secret ota_password

# Enable Web server
web_server:
  port: 80

# Sync time with Home Assistant
time:
  - platform: homeassistant
    id: homeassistant_time

# Text sensors with general information
text_sensor:
  - platform: version
    name: $upper_devicename version
  - platform: wifi_info
    ip_address:
      name: $upper_devicename ip
    ssid:
      name: $upper_devicename ssid
    bssid:
      name: $upper_devicename bssid

sensor:
  # Uptime sensor
  - platform: uptime
    name: $upper_devicename uptime
  # WiFi Signal sensor
  - platform: wifi_signal
    name: $upper_devicename wifi signal
    update_interval: 10s

status_led:
  pin:
    number: GPIO13
    inverted: true

binary_sensor:
  - platform: gpio
    name: $upper_devicename button
    pin: GPIO00
    id: reset
    internal: true
    filters:
      - invert:
      - delayed_off: 10ms
    on_press:
      - light.toggle: 
          id: relay_1

  - platform: gpio
    #name: $upper_devicename switch
    pin: GPIO04
    id: switch_1
    on_press:
      then:
        - light.toggle:
            id: relay_1
   # on_release:
   #   then:
   #     - light.toggle:
    #        id: relay_1

output:
  - platform: gpio
    pin: GPIO12
    id: relay_1

light:
  - platform: binary
    name:  $upper_devicename light
    output: relay_1

but I’m getting this error on the change from - switch.toggle: to - light.toggle:

ID ‘relay_1’ of type gpio::GPIOBinaryOutput doesn’t inherit from light::LightState. Please double check your ID is pointing to the correct value.

I think I have it working now - I added an id to the light and the error are gone. Just trying now. I’ll report back once I’m sure. Thanks

So this is the final esphome code that I used on my sonoff mini to display in home assistant as a light instead of a switch.

substitutions:
  ip_address: 192.168.1.XX
  gateway_address: 192.168.1.1
  subnet_address: 255.255.255.0
  dns1_address: 192.168.1.1
  dns2_address: 192.168.1.1
  timeout_reboot: 5min
  devicename: bedroom2_light_ceiling_sonoff_mini
  upper_devicename: Bedroom2 Light Ceiling
  ap_name: bdrm2_ceil_lght
  

esphome:
  name: $devicename
  platform: ESP8266
  board: esp01_1m

# WiFi connection
wifi:
  networks:
    - ssid: !secret wifi_ssid_NoT
      password: !secret wifi_password_NoT
      priority: 10
    - ssid: !secret wifi_ssid_gif
      password: !secret wifi_password_gif
      priority: 20
    - ssid: !secret wifi_ssid_bc
      password: !secret wifi_password_bc
      priority: 30
  #enable acess point access if no wifi
  ap:
    ssid: $ap_name
    password: !secret ota_password

  manual_ip:
    static_ip: $ip_address
    gateway: $gateway_address
    subnet: $subnet_address
    dns1: $dns1_address
    dns2: $dns2_address
  reboot_timeout: $timeout_reboot



# Enable logging
logger:

# Enable Home Assistant API
api:
  password: !secret api_password
  reboot_timeout: $timeout_reboot

# Enable over-the-air updates
ota:
  password: !secret ota_password

# Enable Web server
web_server:
  port: 80

# Sync time with Home Assistant
time:
  - platform: homeassistant
    id: homeassistant_time

# Text sensors with general information
text_sensor:
  - platform: version
    name: $upper_devicename version
  - platform: wifi_info
    ip_address:
      name: $upper_devicename ip
    ssid:
      name: $upper_devicename ssid
    bssid:
      name: $upper_devicename bssid

sensor:
  # Uptime sensor
  - platform: uptime
    name: $upper_devicename uptime
  # WiFi Signal sensor
  - platform: wifi_signal
    name: $upper_devicename wifi signal
    update_interval: 10s

status_led:
  pin:
    number: GPIO13
    inverted: true

binary_sensor:
  - platform: gpio
    name: $upper_devicename button
    pin: GPIO00
    id: reset
    internal: true
    filters:
      - invert:
      - delayed_off: 10ms
    on_press:
      - light.toggle: light_1

  - platform: gpio
    #name: $upper_devicename switch
    pin: GPIO04
    id: switch_1
    on_press:
      then:
        - light.toggle: light_1

output:
  - platform: gpio
    pin: GPIO12
    id: relay_1

light:
  - platform: binary
    name:  $upper_devicename light
    output: relay_1
    restore_mode: ALWAYS_OFF
    id: light_1

Thank you Holdesmade for your help as I wouldn’t of been able to do this without it.

Yes sorry, you’re right. Glad its working

this is my modified code and compiled for sonoff TX switches
from two users of this forum
I’m sorry I don’t know who the other one was

if it helps anyone

substitutions:
  devicename: yours_name
  upper_devicename: yours-name

esphome:
  name: $devicename

  # Automation turning on the blue status LED once T1 is booted up
  on_boot:
    priority: -10
    then:
      - light.turn_on: blue_led_light

esp8266:
  board: esp01_1m

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

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

web_server:
  port: 80

text_sensor:
  - platform: version
    name: Date
  - platform: wifi_info
    ip_address:
      name: $upper_devicename ip
    ssid:
      name: $upper_devicename ssid

sensor:
  # Uptime sensor
  - platform: uptime
    name: $upper_devicename uptime
  # WiFi Signal sensor
  - platform: wifi_signal
    id: wifi_signal_db
    name: RSSI
    entity_category: "diagnostic"
    update_interval: 30s
  # Reports the WiFi signal strength in %
  - platform: copy
    source_id: wifi_signal_db
    name: "WiFi Signal Percent"
    filters:
      - lambda: return min(max(2 * (x + 100.0), 0.0), 100.0);
    unit_of_measurement: "%"
    entity_category: "diagnostic"

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode:
        input: true
        pullup: true
      inverted: true
    name: "Light Touchpad 1"
    on_press:
      - switch.toggle: relay_1
  - platform: gpio
    pin:
      number: GPIO9
      mode:
        input: true
        pullup: true
      inverted: true
    name: "Light Touchpad 2"
    on_press:
      - switch.toggle: relay_2
- platform: gpio
    pin:
      number: GPIO10
      mode:
        input: true
        pullup: true
      inverted: true
    name: "Light Touchpad 3"
    on_press:
      - switch.toggle: relay_3
  - platform: status
    name: "Status"

switch:
  - platform: gpio
    name: "Light Relay 1"
    pin: GPIO12
    id: relay_1
  - platform: gpio
    name: "Light Relay 2"
    pin: GPIO5
    id: relay_2
 - platform: gpio
    name: "Light Relay 3"
    pin: GPIO4
    id: relay_3

output:
  # Register the blue LED as a dimmable output ....
  - platform: esp8266_pwm
    id: blue_led
    pin: GPIO13
    inverted: true

light:
  # ... and then make a light out of it.
  - platform: monochromatic
    name: "Light Blue LED"
    output: blue_led
    internal: true
    id: blue_led_light