Getting Distance value of HC-SR04 into HA

Hi guys

I have browsed the forums/internet and haven’t been able to find the code required to do this simple task. I’m not a coder so for me I’m not exactly sure which is the right path to go.

I have done the above, got it working in HA perfectly but I can’t seem to get the distance value to show. If someone could please check the code for me and help me out that would be most appreciated, thanks!

The .ino file in the project doesn’t send the actual distance. At line 152 it just published a 1 or a 0 for open or closed. This based on the ‘’‘isdooropen()’’’ function I think (I’m not that good at Arduino).

You’ll need to modify this sketch to output the distance instead of the boolean open/closed value.

You could have both the boolean and the actual value publish to seperate Matt topics, or you could just publish the distance and use HA to do the rest.

Alternatviely, you could look at using esphomelib. It supports this sensor, OTA updates, and sensor value smoothing (it’s really nice). There’s even a hassio add-on that integrates nicely with HA if you’re using hassio.

I feel like there’s such an easy modification to this code that someone who knows what they’re doing could help me out with. I will look at esphomelib though thanks.

I think you’re right. The ultrasonic library is supplying a distance value, you just want to publish it to mqtt instead of (or along with) the state boolean being calculated. Investing in learning esphomelib is only useful if you see yourself deploying a bunch of other boards for other uses.

I also think where I’m going wrong is the "value_template: " in the conf.yaml for HA. I’m not sure what to put there other than. value_template: ‘{{ value_json.distance }}’

The simplest change to retrieve the distance in centimetres is to change line 152 to:

		mqtt.publish(MQTT_TOPIC, String(distance));

I haven’t tried that code myself, so can’t guarantee it will compile.

On the HA side try without the value_template - the MQTT message payload should now contain just the value in centimetres.

Either way, I highly recommend taking a look at ESPHome - the ultrasonic sensor just works out of the box, sending a distance value, and no programming required.

check out DrZzs

As written the distance is never published to mqtt, so home assistant is never going to see it.

Contrary to other advice, I think esphome is well worth learning even for this one project.

The only problem with the vid above is I’m using HA on a Windows so not running HASSIO

Home assistant is exactly the same whether or not you are using hassio.

The MQTT broker isn’t though. I did manage to get ESPEasy firmware onto the controller but I can’t communicate with my windows based HA due to the MQTT having no username and password(I think). So going to try with a Pi Setup later and see,

Totally agree.
I have only used an nodeMCU once before today but today a new one arrived along with a HC-SR04 and within about an hour from watching various videos I now have distance sensors into HA via ESPHome

2 Likes

I have subsequently got it to work perfectly :slight_smile: