ESPHome - switch

Hi All,

Finally I have my first ESPHome device done and ready to go. I struggle only on 1 thing.
I have created a “switch” because when I touch the touch button it will toggle this switch.

So HA have a entity of this switch and on that I created a automation to turn on and off a light.

Only thing is when I restart the ESP or it lost signal and restart itself the “switch” always turn off so when the lights are on and this happen then the lights go off all.

How can I change this?

I tried this: restore_mode: RESTORE_DEFAULT_OFF but no success.

esphome:
  name: "meek-bathroom"
  on_boot:
    priority: 600
    then:
      - switch.turn_on: touch_power

esp8266:
  board: nodemcuv2

logger:
  level: ERROR
api:
  encryption:
    key: "HIDDEN"
  reboot_timeout: 0s

ota:
  safe_mode: true
  password: "HIDDEN"

wifi:
  ssid: Oudenes.Photography
  password: HIDDEN
  power_save_mode: light
  reboot_timeout: 0s
  fast_connect: true
  manual_ip:
    static_ip: 192.168.100.202
    gateway: 192.168.100.1
    subnet: 255.255.255.0

mqtt:
  broker: 192.168.100.24
  username: HIDDEN
  password: HIDDEN
  client_id: esphome-bathroom
  topic_prefix: esphome/bathroom
  discovery: true
  reboot_timeout: 0s

light:
  - platform: neopixelbus
    default_transition_length: 0s
    type: GRB
    variant: 800KBPS
    pin: D4 #GPIO2
    num_leds: 1
    name: "ESPHome bathroom - Neopixel"
    restore_mode: ALWAYS_ON
    id: neopixel
          
button:
  - platform: factory_reset
    name: "ESPHome bathroom - reset"

switch:
  - platform: gpio
    pin: D1 #GPIO1
    name: "ESPHome bathroom - Switch 1"
    id: switch1

  - platform: gpio
    pin: D1 #GPIO16
    name: "ESPHome bathroom - Touch 1 power"
    id: touch_power

  - platform: safe_mode
    name: "ESPHome bathroom - safe mode"

text_sensor:
  - platform: wifi_info
    ip_address:
      name: "ESPHome bathroom - IP"
    ssid:
      name: "ESPHome bathroom - SSID"
    bssid:
      name: "ESPHome bathroom - BSSID"
    mac_address:
      name: "ESPHome bathroom - Mac"

  - platform: version
    name: "ESPHome bathroom - Version"
    hide_timestamp: true

binary_sensor:
  - platform: gpio
    pin: D6 #GPIO12
    name: "ESPHome bathroom - Touch 1"
    discovery: false
    on_press:
      - switch.toggle: switch1
      - if:
          condition:
            switch.is_on: switch1
          then:
            - light.turn_on: 
                id: neopixel
                brightness: 100%
                red: 0%
                green: 100%
                blue: 0%
      - if:
          condition:
            switch.is_off: switch1
          then:
            - if:
                condition:
                  lambda: |-
                    return id(goodnight).state > 0;
                then:
                  - light.turn_on: 
                      id: neopixel
                      brightness: 100%
                      red: 10%
                      green: 0%
                      blue: 10%
            - if:
                condition:
                  lambda: |-
                    return id(kodi).state > 0;
                then:
                  - light.turn_on: 
                      id: neopixel
                      brightness: 100%
                      red: 93%
                      green: 0%
                      blue: 93%
            - if:
                condition:
                  lambda: |-
                    return id(daytime).state > 0;
                then:
                  - light.turn_on: 
                      id: neopixel
                      brightness: 100%
                      red: 100%
                      green: 0%
                      blue: 0%
            - if:
                condition:
                  lambda: |-
                    return id(evening).state > 0;
                then:
                  - light.turn_on: 
                      id: neopixel
                      brightness: 100%
                      red: 100%
                      green: 0%
                      blue: 0%
            - if:
                condition:
                  lambda: |-
                    return id(appletv).state > 0;
                then:
                  - light.turn_on: 
                      id: neopixel
                      brightness: 100%
                      red: 55%
                      green: 55%
                      blue: 0%
            - if:
                condition:
                  lambda: |-
                    return id(tv).state > 0;
                then:
                  - light.turn_on: 
                      id: neopixel
                      brightness: 100%
                      red: 0%
                      green: 25%
                      blue: 53%
            - if:
                condition:
                  lambda: |-
                    return id(nintendo).state > 0;
                then:
                  - light.turn_on: 
                      id: neopixel
                      brightness: 100%
                      red: 36%
                      green: 28%
                      blue: 55%

sensor:
  - platform: wifi_signal
    name: "ESPHome bathroom - wifi Signal"
    update_interval: 60s

Because you are using an esp82xx you also need to have esp8266_restore_from_flash: true under esphome: in your yaml to restore states from flash :bulb:

You might also want to avoid using mqtt and api in parallel - or do you have any particular reason to use both? :thinking:

The mqtt is because to get states of my scenes in HA. When a turn on a light manual and turn it off also the led will go back to the scene color.

All wall switches get a color for specific scene. Turn wall switch on its get green when day time or evening scene is on led gets red when wall switch turn off.

I saw the led also in HA as light.
But working with mqtt all my 8 wall switches can het same color simultaneously

Was reading more and you pushed me to.rogjt direction. I can create sensors of HA entities and can use those sensors to let things work. Then the MQTT part is not needed anymore.

Switches all work on ESPeasy. But esphome is so much better!!

1 Like

I now have changed everything and that’s working. Only thing now is it keeps disconnecting from the wifi… with ESPeasy there was no issue about that… :frowning:

power_save_mode: none didn’t work for me.

tried to change 2.4ghz channel from 20/40mhz to only 20mhz didn’t work
chanced channel from 3 to 2 that’s seems to work little bit better.

Anything else I can try?

esphome:
  name: "meek-bathroom"
  on_boot:
    priority: 600
    then:
      - switch.turn_on: touch_power

esp8266:
  board: esp01_1m
  restore_from_flash: true

logger:
  level: ERROR
api:
  encryption:
    key: "HIDDEN"
  reboot_timeout: 15min

ota:
  safe_mode: true
  password: "HIDDEN"

wifi:
  ssid: Oudenes.Photography
  password: HIDDEN
  reboot_timeout: 0s
  fast_connect: true
  manual_ip:
    static_ip: 192.168.100.202
    gateway: 192.168.100.1
    subnet: 255.255.255.0

light:
  - platform: neopixelbus
    default_transition_length: 0s
    type: GRB
    variant: 800KBPS
    pin: GPIO2 #D4
    num_leds: 1
    name: "ESPHome bathroom - Neopixel"
    restore_mode: ALWAYS_ON
    id: neopixel
          
button:
  - platform: factory_reset
    name: "ESPHome bathroom - reset"

switch:
  - platform: gpio
    pin: GPIO1 #D1
    name: "ESPHome bathroom - Touch 1 power"
    id: touch_power

  - platform: safe_mode
    name: "ESPHome bathroom - safe mode"

  - platform: template
    name: "ESPHome bathroom - Switch 1"
    restore_state: true
    id: switch1
    optimistic: true

binary_sensor:
  - platform: gpio
    pin: GPIO12 #D6
    name: "ESPHome bathroom - Touch 1"
    on_press:
      - switch.toggle: switch1
      - if:
          condition:
            switch.is_on: switch1
          then:
            - light.turn_on: 
                id: neopixel
                brightness: 100%
                red: 0%
                green: 100%
                blue: 0%
      - if:
          condition:
            switch.is_off: switch1
          then:
            - if:
                condition:
                  lambda: 'return id(daytime).state == "on";'
                then:
                  - light.turn_on: 
                      id: neopixel
                      brightness: 100%
                      red: 100%
                      green: 0%
                      blue: 0%
            - if:
                condition:
                  lambda: 'return id(goodnight).state == "on";'
                then:
                  - light.turn_on: 
                      id: neopixel
                      brightness: 100%
                      red: 10%
                      green: 0%
                      blue: 10%
            - if:
                condition:
                  lambda: 'return id(kodi).state == "on";'
                then:
                  - light.turn_on: 
                      id: neopixel
                      brightness: 100%
                      red: 93%
                      green: 0%
                      blue: 93%
            - if:
                condition:
                  lambda: 'return id(evening).state == "on";'
                then:
                  - light.turn_on: 
                      id: neopixel
                      brightness: 100%
                      red: 100%
                      green: 0%
                      blue: 0%
            - if:
                condition:
                  lambda: 'return id(appletv).state == "on";'
                then:
                  - light.turn_on: 
                      id: neopixel
                      brightness: 100%
                      red: 55%
                      green: 55%
                      blue: 0%
            - if:
                condition:
                  lambda: 'return id(tv).state == "on";'
                then:
                  - light.turn_on: 
                      id: neopixel
                      brightness: 100%
                      red: 0%
                      green: 25%
                      blue: 53%
            - if:
                condition:
                  lambda: 'return id(nintendo).state == "on";'
                then:
                  - light.turn_on: 
                      id: neopixel
                      brightness: 100%
                      red: 36%
                      green: 28%
                      blue: 55%
sensor:
  - platform: wifi_signal
    name: "ESPHome bathroom - wifi Signal"
    update_interval: 60s

text_sensor:
  - platform: wifi_info
    ip_address:
      name: "ESPHome bathroom - IP"
    ssid:
      name: "ESPHome bathroom - SSID"
    bssid:
      name: "ESPHome bathroom - BSSID"
    mac_address:
      name: "ESPHome bathroom - Mac"

  - platform: version
    name: "ESPHome bathroom - Version"
    hide_timestamp: true
  
  - platform: homeassistant
    name: "Scene Daytime"
    entity_id: switch.scene_daytime
    id: "daytime"

  - platform: homeassistant
    name: "Scene Evening"
    entity_id: switch.scene_evening
    id: "evening"

  - platform: homeassistant
    name: "Scene TV"
    entity_id: switch.scene_tv
    id: "tv"

  - platform: homeassistant
    name: "Scene AppleTV"
    entity_id: switch.scene_appletv
    id: "appletv"

  - platform: homeassistant
    name: "Scene Kodi"
    entity_id: switch.scene_kodi
    id: "kodi"

  - platform: homeassistant
    name: "Scene Nintendo"
    entity_id: switch.scene_nintendo
    id: "nintendo"

  - platform: homeassistant
    name: "Scene Goodnight"
    entity_id: switch.scene_goodnight
    id: "goodnight"

In that case it would be good to raise the log level of your esphome node again to DEBUG (that’s the default) and monitor the serial logs to catch the moment the device disconnects.

Also the HA logs can sometimes give a little clue why a esphome node disconnects.

The FAQ on esphome.io also features some information about that “topic”:

:point_right: My node keeps reconnecting randomly (Frequently asked questions in ESPHome)

I think all people “playing” already longer with esp’s had such a travel… coming from espeasy, espurna, tasmota or other solutions… esphome is just a whole different level. Alone the native api (which should make mqtt obsolete in most cases) is already such a vast improvement which (together with all the others esphome “goodies”) easily allows to deploy like 5 or more (working) esphome nodes in the same time frame we used to just getting one esp flashed with a random binary, configured and and working via mqtt.

1 Like

Yes its a big step forward from espeasy to esphome.
I only can’t understand why espeasy didn’t have such wifi issues and esphome have.

Ill add DEBUG and will check with serial connection what error messages I get.

Thanks anyway for all the info!

Finally my automation is done and prevent to turn on or off when ESP is restart.

- id: "5fd6c4d3-1a8f-4c20-889b-ab377a9216ce"
  alias: "ESPHome - All switches"
  mode: restart
  trigger:
    - platform: state
      entity_id: 
        - switch.esphome_bathroom_switch
        - switch.esphome_hallway_switch
        - switch.esphome_kitchen_switch
        - switch.esphome_livingroom_switch
        - switch.esphome_wall_switch
        - switch.esphome_window_switch
        - switch.esphome_toilet_switch
        - switch.esphome_balcony_switch
        - switch.esphome_diningroom_switch
        - switch.esphome_dressoir_switch

  condition: 
    - "{{ not (to_state == 'on' and from_state == 'unavailable') }}"
    - "{{ not (to_state == 'off' and from_state == 'unavailable') }}"
    - "{{ not (to_state == 'unavailable') }}"

  variables:
    from_state: "{{ trigger.from_state.state }}"
    to_state: "{{ trigger.to_state.state }}"
    name: "{{ trigger.entity_id[15:-7] }}"

  action:
    - if: 
        - "{{ (to_state == 'on') }}"
      then:
        - service: light.turn_on
          target:
            entity_id: light.{{ name }}_all
          data:
            brightness_pct: 100
            kelvin: "{{ states('sensor.adaptive_lighting_color_temperature') }}"

      else:
        - service: light.turn_off
          target:
            entity_id: light.{{ name }}_all
2 Likes