I appreciate any help you can provide on this.
These are my first steps integrating ESPHome via MQTT with HA. It’s a device that will be connected to batteries, and I want to save as much energy as possible. It’s a temperature sensor that I’ll be installing outside my house. I want the device to enter deep sleep for 20 minutes, wake up, send the shtcx sensor data via MQTT, and then go back to sleep. For now, I have 2 minutes of sleep configured, but this will be changed later.
After the first tests, I feel very silly with the results. I’m trying to publish the sensor data to the topics cmnd/esp32-pruebas/temperatura
and cmnd/esp32-pruebas/humedad
, but when using MQTT Explorer, I see that the data I need in HA is being also published under a different topic. Now I wonder if it’s necessary to try to publish this data under the topic cmnd/esp32-pruebas/temperatura
.
Then, in HA, I see that the MQTT integration automatically detects 3 entities for this integration. I was only expecting it to detect 2, since I’m not explicitly publishing the “Boton1 dormitorio” binary sensor via MQTT.
I know I’m making some silly mistakes (maybe several), but I don’t know what they are. Also, from what I’ve been reading, I was planning to add a sensor to the configuration.yaml
file to collect MQTT readings, but based on my results, I’ve come to the conclusion that it’s not necessary.
What is the correct way to do this? I’m currently using HAOS v2025.1.4.
My code is:
substitutions:
board_ip: 192.168.77.50
device_name: esp32-pruebas
packages:
wifi: !include wifi.yaml
battery_device: !include battery_device.yaml
esphome:
name: $device_name
friendly_name: ESP32 de pruebas
on_boot:
priority: 600
then:
- script.execute: send_data_and_sleep
esp32:
board: esp32dev
framework:
type: arduino
logger:
level: DEBUG
api:
encryption:
key: "xxx"
ota:
- platform: esphome
password: "xxx"
i2c:
- id: bus_display
frequency: 800kHz
sda: GPIO19
scl: GPIO18
sensor:
- platform: shtcx
i2c_id: bus_display
update_interval: 30s
temperature:
name: "temperatura_pruebas"
id: temperatura_sensor
humidity:
name: "humedad_pruebas"
id: humedad_sensor
address: 0x70
binary_sensor:
- platform: gpio
id: boton1
name: Boton1 dormitorio
pin:
number: 14
mode:
input: True
pulldown: True
deep_sleep:
id: deep_sleep_1
sleep_duration: 2min
script:
- id: send_data_and_sleep
mode: restart
then:
- logger.log: "Despertando y enviando datos..."
- delay: 30s
- mqtt.publish:
topic: "cmnd/$device_name/temperatura"
payload: !lambda |-
return to_string(id(temperatura_sensor).state);
- mqtt.publish:
topic: "cmnd/$device_name/humedad"
payload: !lambda |-
return to_string(id(humedad_sensor).state);
- delay: 30s
- if:
condition:
lambda: return !id(ota_mode);
then:
- logger.log: "Entrando en sueño profundo..."
- deep_sleep.enter: deep_sleep_1
else:
- logger.log: "Modo OTA activo, el ESP permanecerá despierto"
And this is battery_device.yaml
with MQTT config:
globals:
- id: ota_mode
type: bool
initial_value: 'false'
mqtt:
broker: mqtt.broker.com
port: 1883
username: xxx
password: zzz
birth_message:
will_message:
on_message:
- topic: stat/$device_name/ota_mode
payload: 'ON'
then:
- globals.set:
id: ota_mode
value: 'true'
- topic: stat/$device_name/ota_mode
payload: 'OFF'
then:
- globals.set:
id: ota_mode
value: 'false'