Oil tank level measuring MQTT message from arduino

I have built and set up an oil tank level sensor that’s working but my last piece of the puzzle is getting the data into HA.

I’m publishing the MQTT message OK but not parsing it correctly in HA, I think.

Here’s what I have in the arduino:

client.publish("oil_level/litres", String(Litres));

And in HA my sensor is defined as:

- platform: mqtt
  name: "Oil Tank Level"
  state_topic: "oil_level/litres/sensor"
  value_template: "{{ value_json['sensor'].litres }}"
  qos: 1
  unit_of_measurement: "L"
  icon: mdi:gauge

What do I need to change, in either or both, to have this work?

Does this mean you are publishing a numeric text string to oil_level/litres?

client.publish("oil_level/litres", String(Litres));

If it does, then you need to use this (no value_template required):

  state_topic: "oil_level/litres"

Or does your code somehow append /sensor to oil_level/litres and String(Litres) means the payload will be published as JSON?

Yes, that’s it exactly. Thank you.

All I see when subscribing using MQTT lens is a value, like 160 for example.

I’ll give that a try.

May i ask how you built that sensor?
Is it inside the tank or?

It uses an ultrasonic sensor, like fitted to your car, and a NodeMCU.

Once I have the MQTT part buttoned down I’ll happily share the code if you’re interested.

It’s not all my own work. I’ve cobbled it together from a couple of other projects but it’s working well.

you can also try my version of the oil measurement sensor with zero config in homeassistant (MQTT-auto-discovery).