How can I convert this API code to MQTT?

The binary_sensor here uses the new API, but I’m not sure what I need to do in order to have this work with MQTT. I’d like to be able to set this binary_sensor to on/off from within an HA automation.

I can’t find any MQTT examples for either. Much appreciated.

esphome:
  name: car_honda
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: "MyWiFi"
  password: "0123456789"
  fast_connect: true
  
  manual_ip:
    static_ip: 172.16.68.153
    gateway: 172.16.68.1
    subnet: 255.255.255.0

    
# Enable logging
logger:

# Enable Home Assistant API
api:
  password: 'password'
  
ota:
  password: 'password'
  
binary_sensor:
  - platform: homeassistant
    name: "Garage Opened HONDA"
    entity_id: input_boolean.hyundai_opened_garage
    id: garage_opened_honda
    
sensor:
  - platform: template
    name: "HONDA"
    lambda: |-
      static int prevCount = 0;
      static int num_executions = 0;
      if (!(id(garage_opened_honda).state)) {
        num_executions += 1;
      }
      else {
        num_executions = 1;
      }
      if (prevCount != num_executions) {
        prevCount = num_executions;
        return num_executions;
      }
    update_interval: 1s

What is it you wish to accomplish overall?

With the ESPHome API you can link the binary_sensor to a HA input boolean, so you can set it from HA, as well as have an automation trigger when it changes in the device.

I want to be able to do them same thing with MQTT, that is set the binary_sensor in the ESPHome device to on/off and respond to on/off state changes in HA.

ETA: My original code “works”, however it is too slow upon startup. Otto said using MQTT would be faster.

I’m not sure I can help since I still consider myself a novice with Home Assistant. Before going further, what do you understand about the mqtt protocol? In my home, I have some sensors that publish an mqtt message that home assistant can respond to:

  - platform: mqtt
    name: "Light-Attic"
    state_topic: "attic/light_status"
    force_update: true
    
  - platform: mqtt
    name: "Temp-Room"
    state_topic: "tele/room/SENSOR"
    value_template: "{{ value_json['DHT11'].Temperature }}"
    force_update: true 
    unit_of_measurement: "°F"   

You need to know what the topic and format of the data are that the sensor sends.

My yaml for controlling lights looks like this:

  - platform: mqtt
    name: "Master Bedroom Light"  
    command_topic: "cmnd/mbrLights/POWER"
    state_topic: "stat/mbrLights/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true
    
  - platform: mqtt
    name: "Desk Light"  
    command_topic: "cmnd/DeskLight/power"
    state_topic: "stat/DeskLight/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true

In this direction you need to know the command and state topics the device is listening for and what the format of the data that the device works from.

If I am completely misunderstanding your question, then I’ll stop trying to help here.

In your ESP config file replace this:

api:
  password: 'password'

With this:

mqtt:
  broker: [your mqtt broker ip address]
  username: [your mqtt username]
  password: [your mqtt password]

The binary sensor will be discovered automatically by HA.