Publish MQTT

Not a HA question. I need to publish the value of a BME280 sensor that uses ESPHome to a third party MQTT broker and I can’t for the life of me work the syntax out!

I know this “on_value” syntax is wrong but could someone suggest what the correct way of doing this is?

esphome:
  name: signalk
  platform: ESP8266
  board: d1_mini_pro

wifi:
  manual_ip:
    static_ip: 192.168.1.21
    gateway: 192.168.1.254
    subnet: 255.255.255.0
    dns1: 8.8.8.8
    dns2: 8.8.4.4
  fast_connect: true
  reboot_timeout: 45min
  networks:
  - ssid: "redacted"
    password: "redacted"

mqtt:
  topic_prefix: boat
  discovery: false
  broker: 192.168.1.41
  port: 1883
  username: admin
  password: redacted
  discovery_prefix: homeassistant

# Enable logging
logger:
  level: VERBOSE

ota:

sensor:
  - platform: bme280
    temperature:
      name: "OutsideTemp"
      oversampling: 16x
      on_value:
        - mqtt.publish_json:
            topic: "boat/temperature"
            payload: {$OutsideTemp}
    pressure:
      name: "Outside Pressure"
    humidity:
      name: "Outside Humidity"
    address: 0x76
    update_interval: 5s

i2c:
  sda: D2
  scl: D1
  scan: true

That’s ok, ESPHome questions are welcome too. You got the right category.

You have specified boat as a topic prefix in your mqtt setup block. You don’t need to include it in the publish topic. Also you need to include an id and your payload should be:

sensor:
  - platform: bme280
    temperature:
      name: "OutsideTemp"
      id: outside_temp
      oversampling: 16x
      on_value:
        - mqtt.publish_json:
            topic: "temperature"
            payload: |-
              root["OutsideTemp"] = id(outside_temp).state;

This should publish something like, {"OutsideTemp": "99"} to the topic boat/temperature

1 Like

That’s magic @tom_l - compiles now and will investigate what gets to my MQTT server later.

Thank you!

1 Like

I highly recommend MQTT explorer for exploring the broker contents:

2 Likes

Thanks @tom_l and I use MQTT Explore already. A great tool! I have another small issue though, when looking at the results of your suggested code in MQTT Explore, you see that because of the mqtt.publish_json you get a key/value pair (e.g. {“Outside Illuminance”:0)} (Please ignore the mixing of temperature, pressure, illuminance etc - that’s just me messing around with different BMP280 sensor values and clearly not relevant to the issue)

For my application, that doesn’t work and I need it to read

temperature = 24

mqtt

I guessed that I should use mqtt.publish (no JSON) instead but again struggling to find the correct syntax as this doesn’t work and gives the above result ("id(outside_pressure).state).

    temperature:
      name: "OutsideTemp"
      id: outside_temp
      oversampling: 16x
      on_value:
        - mqtt.publish:
            topic: "boat/self/environment/outside/temperature"
            payload: |-
              id(outside_pressure).state;

Not sure why it didn’t work, it’s the same as the documented example. If you don’t want to use json you have to do this:

    temperature:
      name: "OutsideTemp"
      id: outside_temp 
      oversampling: 16x
      on_value:
        - mqtt.publish:
            topic: "boat/self/environment/outside/temperature"
            payload: !lambda |-
              return id(outside_temp).state;

Thanks, @tom_l and I did try that but I then end up with:

signalk.yaml:45:28: error: could not convert 'outside_temp->esphome::sensor::Sensor::state' from 'float' to 'std::string {aka std::basic_string<char>}'

And couldn’t find a way to cast a float to a string :frowning:

Yeah I hate converting types in ESPHome. It want’s a string but we gave it a number.

Having said that, Why are you doing this?

Because you have enabled MQTT, this:

sensor:
  - platform: bme280
    temperature:
      name: "OutsideTemp"
      id: outside_temp
      oversampling: 16x

Will publish values to the mqtt broker automatically, every time the sensor updates. No need to automate it.

“Why are you doing this” Great question with a long answer.

A mate of mine owns a boat. The Swiss pocket knife for boat automation is a system called SignalK, which totally a totally amazing tool. Many boaters run ESP8266 sensors (to check their battery banks, bildges etc) and their platform of choice for them is something called SensESP.

Back to my mate… He’s an accountant and there is no way in the world that he can get VisualStudio set up and compile and flash his own ESP sensors to connect to SignalK so I’m looking for an alternative for him to use. Given that SignalK acts as an MQTT broker and ESPHome can publish MQTT events, it seems to make total sense to try to connect them and let ESPHome publish data to SignalK.

This makes sense as ESPHome is about 5000% easier to configure compared to SensESP especially given the recent updates on its deployment features.

That’s what I’m trying to do but, ESPHome needs to deliver the data to SignalK in the correct format and that’s where my questions originate from. It’s very, very close but just not close enough so if you could make some further suggestions I would really appreciate it for all the boaters out there :slight_smile:

I managed to sort it out - it was actually quite simple. Need to tidy up the formatting a bit and then I’ll post a complete yaml file here if people want to do the same.

Thanks again for your help.

  - platform: bme280
    temperature:
      name: "OutsideTemp"
      id: outside_temp
      oversampling: 16x
      on_value:
        - mqtt.publish:
            topic: "vessels/self/environment/outside/temperature"
            payload: !lambda |-
              return to_string(id(outside_temp).state);
4 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?

Great,I have succeed!