Best practice OTA & MQTT?

I want to run some D1 Mini with sensors on battery and therefore I am looking for a very economical solution for the operation. In the meantime I have been able to realize all the optimizations I know (WiFi, deep sleep) under esphome.

Now I still want to replace the HA-API with MQTT and I am not sure how to proceed:

The MQTT broker is active, the values already end up in the topic homeassistant/sensor…
But: for this transfer it probably needs the HA-API in esphome after all, which I actually want to disable.

My question: Can I do without the HA-API in esphome and still perform OTA via ESPHome? Or should I do without esphome and build an independent solution with mqtt, for example with ESPEasy?

Yes, ota does not depends on api.

1 Like

Thank you very much! That was my mistake, I had not read an error message correctly when I disabled the api:. MQTT is now active, however I do not get any values (not even via the MQTT Explorer). I had tried it as follows:

## bme680_bsec
  - platform: bme680_bsec
    temperature:
      name: $devicename Temperatur
      id: temp
      sample_rate: lp
      filters:
        - median
      on_value:
        - mqtt.publish:
            topic: "sensord1/temperatur"
            payload: id(temp).state

Did I forget something somewhere else?

1 Like

Did you do all this MQTT Client Component — ESPHome

Thank You! I added the following to configuration.yaml:

mqtt_statestream:
  base_topic: homeassistant
  publish_attributes: true
  publish_timestamps: true

mqtt:
  broker: 192.168.178.80
  discovery: true

But the log of the sensor is:

[12:13:29][I][mqtt:176]: Connecting to MQTT... 
[12:13:29][W][mqtt:264]: MQTT Disconnected: TCP disconnected.

This is the yaml of the simple sensor, i tested with:

substitutions:
  devicename: esp-dht11-01
  friendly_devicename: ESP mit DHT

esphome:
  name: $devicename

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:

# Mqtt
mqtt:
  broker: 192.168.178.80
  username: !secret mqtt_user
  password: !secret mqtt_pass
  birth_message:
    topic: sensor/$devicename/status
    payload: online
  will_message:
    topic: sensor/$devicename/status
    payload: offline

ota:
  password: "6ed48a5bec1fe58310ea2f5ad36afaef"

wifi:
  networks:
    - ssid: !secret wifi_ssid
      password: !secret wifi_pwd
    - ssid: !secret wifi_ssid_2
      password: !secret wifi_pwd_2
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esensor-01 Fallback Hotspot"
    password: "1SKvzs7idzYy"
  manual_ip:
    static_ip: 192.168.178.26
    gateway: !secret gateway
    subnet: !secret subnet
    dns1: !secret dns01
    dns2: !secret dns02

captive_portal:

sensor:
  - platform: dht
    pin: GPIO12
    update_interval: 30s
    temperature:
      name: "Temperatur"
      id: temp
      on_value:
        - mqtt.publish:
            topic: "sensord2/feuchte"
            payload: id(temp).state
    humidity:
      name: "rel. Luftfeuchtigkeit"
      id: feuchte
      on_value:
        - mqtt.publish:
            topic: "sensord2/feuchte"
            payload: id(feuchte).state

Doesn’t seem to publish anything :frowning:

okay, sorry, it’s working now. I don’t know why, I only deactivated api: and rebooted HA. Thank you for your help!
ESPHome takes care of transferring the values itself via MQTT, which is convenient. OTA works fine, API is switched off. The values appear in my case under esp-dht11-01/sensor/....
Now only deepsleep is missing, controlled via MQTT, then I am satisfied. If it is any help, here is a simple script for the DHT11 on ESP8266:

substitutions:
  devicename: esp-dht11-01
  friendly_devicename: ESP mit DHT

esphome:
  name: $devicename

esp8266:
  board: esp01_1m

# Enable logging
logger:
  level: ERROR
  logs:
    mqtt.component: ERROR
    mqtt.client: ERROR

# Enable Home Assistant API
#api:

# Mqtt
mqtt:
  broker: 192.168.178.80
  username: !secret mqtt_user
  password: !secret mqtt_pass
  discovery: true
  birth_message:
    topic: $devicename/status
    payload: online
  will_message:
    topic: $devicename/status
    payload: offline

ota:
  password: "6ed48a5bec1fe58310ea2f5ad36afaef"

wifi:
  fast_connect: true
  ssid: !secret wifi_ssid_2
  password: !secret wifi_pwd_2
  manual_ip:
    static_ip: 192.168.178.26
    gateway: !secret gateway
    subnet: !secret subnet
    dns1: !secret dns01
    dns2: !secret dns02
# Enable fallback hotspot (captive portal) in case wifi connection fails
#  ap:
#    ssid: $devicename Fallback Hotspot
#    password: !secret hotspot_pass

captive_portal:

sensor:
  - platform: dht
    pin: GPIO12
    update_interval: 30s
    temperature:
      name: Temperatur
      id: temp
    humidity:
      name: Luftfeuchtigkeit relativ
      id: feuchte
  - platform: template
    name: Luftfeuchtigkeit absolut
    lambda: |-
      const float mw = 18.01534;    // molar mass of water g/mol
      const float r = 8.31447215;   // Universal gas constant J/mol/K
      return (6.112 * powf(2.718281828, (17.67 * id(temp).state) /
        (id(temp).state + 243.5)) * id(feuchte).state * mw) /
        ((273.15 + id(temp).state) * r); // in grams/m^3
    accuracy_decimals: 2
    update_interval: 30s
3 Likes

Hi
I use your codes in my Lilygo Board through EspHome.
Would you please help me to edit the HA configuration file based on your esphome codes?