ESPHome Unable to read mqtt topic on_boot

Hi guys. I want to create a counter based on esp and PZEM-04. I want to read the indicators of my counter from the mqtt topic when starting esp. For this, I use the on_boot automation and the following code:

esphome:
  name: esp_name
  platform: ESP8266
  board: nodemcuv2

  on_boot:
    priority: 600
    then:
      - mqtt.subscribe:
          topic: "mqtt_topic"

mqtt:
  broker: 'mqtt_server_ip'
  username: 'mqtt_username'
  password: 'mqtt_password'

sensor:
  - platform: mqtt_subscribe
    name: "Your Sensor"
    state_topic: "mqtt_topic"
    unit_of_measurement: "Your Unit of Measurement"
    value_template: "{{ value }}"

but I get the error Unable to find action with the name ‘mqtt.subscribe’ Help to find a solution please

That’s because there is no such action. The mqtt subscription is done automatically by the sensor.

1 Like

I understand that there is no such action, but what does it mean that the subscription of the sensor is automatic?

Once you create a mqtt sensor and define a mqtt server, ESPHome takes care of the rest. Same with publishing values. If you have defined a mqtt server and say a temperature sensor, you don’t use mqtt.publish. ESPHome just publishes the sensor readings to the server.

1 Like