How to publish DS18B20 to MQTT

Hi, I am a newbe in esphome and try to learn…I have MQTT running for years for my other sensors but I would like to publish my temperatures to MQTT from an esphome sensor. The hardware is running fine and I get temperature results in the webserver of the sensor. Now I would like to publish to MQTT but can’t find an working example. This is the code so far and the editor is complaining about the id.

Couldn't find ID 'sensor_1'. Please check you have defined an ID with that name in your configuration. These IDs look similar: "sensor_6".

mqtt:
  broker: "192.168.1.45"
  username: !secret mqtt_uid
  password: !secret mqtt_password
  topic_prefix: "sensors/buffervat-test"

dallas:
  - pin: D4
    id: sensor_1

sensor:
  - platform: dallas
    address: 0xd50000001e6f7028
    name: "sensor_1"
    id: sensor_1
    on_value:
      - mqtt.publish:
          topic: "sensors/buffervat-test/sensor_1"
          payload: !lambda |-
            return to_string(id(sensor_1).state)

  - platform: dallas
    address: 0xbe0000001c27bf28
    name: "sensor_6"
    id: sensor_6
    on_value:
      - mqtt.publish:
          topic: "sensors/buffervat-test/sensor_6"
          payload: !lambda |-
            return to_string(id(sensor_6).state)

Hi,
Two steps:

  1. Get the 1-wire sensor working as an entity in HASS (e.g. sensor.1wire_temperature_1 below)
  2. Publish the entity state to MQTT to the topic and format you want using a JINJA template

Here’s a working example from my automation.yaml:

- id: '1234567890123456789'
  alias: Publish iButton temperature to MQTT
  description: ''
  trigger:
  - platform: time_pattern
    minutes: '0'
  - platform: time_pattern
    minutes: '30'
  condition: []
  action:
  - service: mqtt.publish
    data:
      topic: stat/1wiretemps/logvalue
      retain: true
      payload_template: '"{{ as_timestamp(now())|timestamp_custom(''%Y-%m-%d %H:%M:%S'')
        + " " + states(''sensor.1wire_temperature_1'') + " " + states(''sensor.1wire_temperature_2'')

I’d also suggest moving the HASS MQTT broker config to the GUI and not in YAML:

If this helps, :heart: this post!

He is asking about esphome.

Once you get mqtt activated in esphome, the data of all sensors will be published to mqtt.

Is this how to activate MQTT in esphome? And than the values should appear in the topic?

It works now. I had to add the broker in my configuration.yaml;

mqtt:
  broker: 192.168.1.45    

Values are now published automatically in MQTT. Now I have to figure out how to omit the automatic topics.

mqtt:
  broker: "192.168.1.45"
  username: !secret mqtt_uid
  password: !secret mqtt_password
  topic_prefix: "sensors/buffervat-test"

dallas:
  - pin: D4

sensor:
  - platform: dallas
    address: 0xd50000001e6f7028
    name: "sensor 1"
    state_topic: "sensors/buffervat-test/s1" # this forces MQTT to put values in this topic
  - platform: dallas
    address: 0xf20000001d0f1c28
    name: "sensor 2"
    state_topic: "sensors/buffervat-test/s2"
  - platform: dallas
    address: 0x9b12f35709646128
    name: "sensor 3"
    state_topic: "sensors/buffervat-test/s3"
  - platform: dallas
    address: 0x8e0000001ea81528
    name: "sensor 4"
    state_topic: "sensors/buffervat-test/s4"
  - platform: dallas
    address: 0xcb0000001e08c328
    name: "sensor 5"
    state_topic: "sensors/buffervat-test/s5"
  - platform: dallas
    address: 0xbe0000001c27bf28
    name: "sensor 6"  
    state_topic: "sensors/buffervat-test/s6"

Hi James, thanks a lot. This was indeed plan B. And if I can’t get rid off the automatically published rubbish I will work out plan B :stuck_out_tongue_winking_eye: