ESPhome api to ESPhome mqtt conversion

Hi Guys,

I am absolutely new to HA and everything related. I just flashed my sonoff with a ESPhome bin and got some basic functionality. but i just realized api is not something good at retaining state and now need to convert my yaml for mqtt.
I know tasmota would be a better option but I am kind of hesitant bcoz ill need a custom bin. Not very sure on compiling that.
I am even open to getting on a zoom session to get this done if required.
i also have a few questions about using the automation on HA and somethings related to mdi:icons

Rishab

all you need to change is to remove api: and put in a section mqtt: (with your broker details) in the yaml file for that device. However what do you mean regarding retain state? what are you trying to retain?

esphome:
  name: ifan02_02
  platform: ESP8266
  board: esp8285
  includes:
    - ifan02.h
  on_boot:
    priority: 225
    # turn off the light as early as possible
    then:
      - light.turn_off: ifan02_light

wifi:
  ssid:
  password:
  manual_ip:
    static_ip:
    gateway:
    subnet:
    dns1:
    dns2: 1.1.1.1

api:

#mqtt:
#  broker:

logger:

ota:

binary_sensor:
  - platform: gpio
    id: vbutton_light
    pin:
      number: GPIO0
      inverted: True
    on_press:
      then:
        - light.toggle: ifan02_light

  - platform: gpio
    id: vbutton_relay_1
    pin:
      number: GPIO9
      inverted: True
    on_press:
      then:
        - switch.toggle: fan_relay1
        - switch.turn_on: update_fan_speed

  - platform: gpio
    id: vbutton_relay_2
    pin:
      number: GPIO10
      inverted: True
    on_press:
      then:
        - switch.toggle: fan_relay2
        - switch.turn_on: update_fan_speed

  - platform: gpio
    id: vbutton_relay_3
    pin:
      number: GPIO14
      inverted: True
    on_press:
      then:
        - switch.toggle: fan_relay3
        - switch.turn_on: update_fan_speed
        
  - platform: gpio
    id: light_button
    pin:
      number: GPIO1
      inverted: True
    on_press:
      then:
        - light.toggle: ifan02_light
        
  - platform: gpio
    id: fan_button
    pin:
      number: GPIO3
      inverted: true
    on_multi_click:
      - timing:
        - ON for 0.5s to 2s
        then:
          - fan.turn_off: ifan02_fan
      - timing:
        - ON for 050ms to 350ms
        then:
          - fan.turn_on:
              id: ifan02_fan
              speed: LOW
      - timing:
        - ON for 050ms to 350ms
        - OFF for 050ms to 200ms
        - ON for 050ms to 350ms
        then:
          - fan.turn_on:
              id: ifan02_fan
              speed: MEDIUM
      - timing:
        - ON for 050ms to 350ms
        - OFF for 050ms to 200ms
        - ON for 050ms to 350ms
        - OFF for 050ms to 200ms
        - ON for 050ms to 350ms
        then:
          - fan.turn_on:
              id: ifan02_fan
              speed: HIGH

output:
  - platform: custom
    type: float
    outputs:
      id: fanoutput
    lambda: |-
      auto ifan02_fan = new IFan02Output();
      App.register_component(ifan02_fan);
      return {ifan02_fan};
  - platform: gpio
    pin: GPIO12
    id: light_output

light:
  - platform: binary
    name: "iFan02 Light"
    output: light_output
    id: ifan02_light

switch:
  - platform: template
    id: update_fan_speed
    optimistic: True
    turn_on_action:
      then:
        - delay: 200ms
        - if:
            condition:
              and:
                - switch.is_off: fan_relay1
                - switch.is_off: fan_relay2
                - switch.is_off: fan_relay3
            then:
              - fan.turn_off: ifan02_fan
        - if:
            condition:
              and:
                - switch.is_on: fan_relay1
                - switch.is_off: fan_relay2
                - switch.is_off: fan_relay3
            then:
              - fan.turn_on:
                  id: ifan02_fan
                  speed: LOW
        - if:
            condition:
              and:
                - switch.is_on: fan_relay1
                - switch.is_on: fan_relay2
                - switch.is_off: fan_relay3
            then:
              - fan.turn_on:
                  id: ifan02_fan
                  speed: MEDIUM
        - if:
            condition:
              and:
                - switch.is_on: fan_relay1
                - switch.is_off: fan_relay2
                - switch.is_on: fan_relay3
            then:
              - fan.turn_on:
                  id: ifan02_fan
                  speed: HIGH
        - switch.turn_off: update_fan_speed

  - platform: gpio
    pin: GPIO5
    id: fan_relay1

  - platform: gpio
    pin: GPIO4
    id: fan_relay2

  - platform: gpio
    pin: GPIO15
    id: fan_relay3

fan:
  - platform: speed
    output: fanoutput
    id: ifan02_fan
    name: "iFan02 Fan"

This is my yaml, I am using a Sonoff iFan02 and have added touch buttons on Rx and Tx pin to be used for light and fan control. I need the speed and the state of light and fan retained after a power failure.

i tried commenting the api and adding the mqtt server details (I have a Pi4 where i run Hassio with mqtt) but the device never got detected.

PS the initial yaml is from @quazzie

How often are you expecting power failures? There are actually settings in ESPhome for restore_mode

Also, the fan component should restore after reboot

Do you have mqtt discovery turned on in HA?

We see a lot of power failures. That will also reboot the Pi. I am not sure if that will then allow a power state restore.
Any clue on how mqtt discovery has to be enabled?

So does it mean, if i comment the api and add the mqtt details the yaml works as is?

It’s an option in your mqtt integration settings:

It should do

Yes its enabled.
Let me experiment with the restore state and get back.

thanks :slight_smile:

Hey so this is wat i see on the logs

I rebooted the HA and my MQTT discovery is on, I still cant find the device. :frowning:
Not sure if i am missing a step maybe…

Rishab

In my point view you should try to eliminate these pi power failure by adding an UPS to your pi. It could help avoiding a lot of future issues.

I could do that. Thanks :slight_smile:

@sparkydave any clue on the device discovery?

Rishab

Sorry, no. I don’t use MQTT much, only for an alarm interface. All of my ESPhome devices use the API. Is there not a way to manually add the device by MQTT? Perhaps try asking on the ESPhome Discord channel

So reinstalling hassio and then enabling the mqtt add on worked :slight_smile: