Changing from MQTT to HA API

Greetings,

I am begining the move from MQTT to the native HA API. I have been told the change is simple, but I am being challenged with a simple example… which is why I am seeking help.

See the code below… what I am seeking to do, is move away from using MQTT and instead undertake the same function using the HA API.

This is the ESP32 sending the status…

binary_sensor:
  - platform: template
    id: ac_climate_mgmt
    name: "Studio AC Climate"
    on_press:
      then:
          - climate.control:
              id: ac_climate_id
              mode: COOL
              fan_mode: AUTO
              target_temperature: 23°C
          - mqtt.publish:
              topic: HomeAssistant/Studio/Aircon/Status
              payload: "ON"
          - logger.log: "AC is ON"
    on_release:
      then:
          - climate.control:
              id: ac_climate_id
              mode: 'off'
          - mqtt.publish:
              topic: HomeAssistant/Studio/Aircon/Status
              payload: "OFF"
          - logger.log: "AC is OFF"

And below is the ESP32 receiving the status…

# MQTT Interface
mqtt:
#  broker: !secret mqtt_broker
  broker: !secret mqtt_broker_new
  username: !secret mqtt_login
  password: !secret mqtt_passwd
  port: 1883
  reboot_timeout: 0s
  keepalive: 60s
  discovery: false
  on_message:
    - topic: esp5-studio-ac/binary_sensor/studio_ac_climate/state
      payload: "OFF"
      qos: 0
      then:
        - binary_sensor.template.publish:
            id: ac_status
            state: off
        - logger.log: "The AC is OFF"
    - topic: esp5-studio-ac/binary_sensor/studio_ac_climate/state
      payload: "ON"
      qos: 0
      then:
        - binary_sensor.template.publish:
            id: ac_status
            state: on
        - logger.log: "The AC is ON"

Any suggestions on the approach I should take would be welcome. I am confident once I have made one change I should be right for the remainder.

Thanks.

This is what I use in my car battery monitor.

The first is a sensor reading the value of a pin monitoring the battery. That shows up as a sensor in the ESP device that can be read directly in Home assistant.

The second is a helper entity in Home Assistant that I read the value in the ESP device

# This is the sensor which monitors the battery voltage
sensor:
  - platform: adc
    pin: A0
    name: "Car Battery Level"
    device_class: voltage
    update_interval: 10s
    filters:
      - multiply: 14.4217

# This is a binary Helper in Home Assistant
binary_sensor:
  - platform: homeassistant
    id: deep_sleep_mode
    name: Deep Sleep Mode
    entity_id: input_boolean.car_battery_monitor_sleep_mode

Hope this helps.

When you add a node using the API, once it is connected to your network, go to the integrations page and add the integration for that esp. This will create entities for pretty much everything defined in the node.

From there the esp#2 would add the sensor/binary/climate entity created by esp#1. Use the home assistant platform for that. Under those sensors you would put your automations.

Hi @Mikefila and @MarkWalsh thanks for the input… I’d like to use the code below as an example if that were ok.

So taking the code below… which uses MQTT.

# MQTT Interface
mqtt:
#  broker: !secret mqtt_broker
  broker: !secret mqtt_broker_new
  username: !secret mqtt_login
  password: !secret mqtt_passwd
  port: 1883
  reboot_timeout: 0s
  keepalive: 60s
  discovery: false
  on_message:
    - topic: esp5-studio-ac/binary_sensor/studio_ac_climate/state
      payload: "OFF"
      qos: 0
      then:
        - binary_sensor.template.publish:
            id: ac_status
            state: off
        - logger.log: "The AC is OFF"
    - topic: esp5-studio-ac/binary_sensor/studio_ac_climate/state
      payload: "ON"
      qos: 0
      then:
        - binary_sensor.template.publish:
            id: ac_status
            state: on
        - logger.log: "The AC is ON"

How might I remove this code, and would I replace it with the code below…

sensor:
  - platform: homeassistant
    id: studio_aircon_status
    name: "Studio AirCon Status"
    entity_id: binary_sensor.studio_ac_climate

Beyond this I am a little lost? So any tips appreciated.

All sensors defined in esphome (not marked as ‘internal’) should be available as entities

The binary sensor ‘ac status’ created using the above is now

sensor:
  - platform: homeassistant
    id: studio_aircon_status
    name: "Studio AirCon Status"
    entity_id: binary_sensor.studio_ac_climate

So I assume under binary_sensor.ac_status you had some sort of automation defined? It would now go under the above. But this should also be change to a binary sensor.

binary_sensor:
  - platform: homeassistant
    id: studio_aircon_status
    name: "Studio AirCon Status"
    entity_id: binary_sensor.studio_ac_climate
    on_state:
      then:
        ... do something here

@aceindy

I was looking for this and didn’t see it, has it been depreciated? Seems that it was replaced by disabled_by_default. You were also able to omit name or was it friendly_name and that would also set it to internal but I don’t see that either.

@Mikefila it’s there Sensor Component — ESPHome

  • internal (Optional, boolean): Mark this component as internal. Internal components will not be exposed to the frontend (like Home Assistant). Only specifying an id without a namewill implicitly set this to true.

And i still use it, as i use the ID in a lambda call later :wink:

1 Like

I have it set in deployed esp’s. I was about to start searching to make changes.

1 Like

Wow! Thanks for the advice, it is very much appreciated.

@Mikefila and @MarkWalsh you are a legends… really appreciate the examples, no doubt they will help me wrap my head around things. @aceindy thanks for highlighting the internal component info… learnt something new.

I am finalising another project, and once I have it out of the way I will focus on above.

1 Like