I wonder if someone can help me with a project I am working on, I am in the process of trying to organise my storage room, I have limited space so trying be a efficient as possible with it. This has led me to purchase several 5L water tanks which I will be using to store cleaning and laundry liquids in. I want to take the opportunity to make the tanks “smart” by adding a ultrasonic sensor to each of them which will report on the stock level and trigger an automation to add the item to my shopping list when my supply runs low.
I have 3D printed a basket to hold the electrics in place, which is working well.
My sensor is up and running in ESPhome and Home assistant however, the sensor is as expected is reporting in distance and I would like a way to report on the percentage of available liquid in each tank.
After spending all afternoon researching and bastardising other peoples code only to encounter error after error I am now willing to admit defeat and ask for some help.
So here goes, I am looking to achieve the following:
When the distance between the sensor and liquid is 4cm or less it reports 100%, when it is 17cm or more it reports 0% and based on those measurements it works out everything else in between.
I did something similar with an analog sensor connected to a wemos D1 mini. i did it in two parts, with the raw value coming into home assistant from the programmed sensor, and then creating another sensor in home assistant with a “value template” to convert the raw value i.e. do the math. I think that ultrasonic sensor will be connected to an analog pin also, so it may be very similar. my code on the home assistant side copy-pasted below
you’ll need to come upwith a formula to convert the voltage to your max and minimum level. my formula below multiplies the raw value by 2 and subtracts 3.
I glossed over this part. if you already have a sensor value, you can probably plug the sensor name directly it into the value template above, and the math should be pretty easy.
Not a math guy either, but it goes something like this (I think).
4 is 100%, 17 is 0%
If you subtract the actual reading, say 4 from the 17, you get 13.
13/17 is 100%
Half full would read 10.5/17.
empty would be 17/17.
The bottom 17’s all cancel out, so if you take the reading and subtract it from 17, then divide it by the 13 (because it’s the biggest it can get) you have your ratio. Multiply that by 100 to get percentage.
((17 - reading) / 13) * 100
Test it with some readings, but I think that works.
More understandable long term would be to assign variables to the numbers and assign names using the raw formula, Then in 6 months when it breaks, you can see what the math is.
Also if you use a different tank, you can just change the values.
Compiling .pioenvs/pantry-fabric-softener-monitor/src/main.cpp.o
<unicode string>: In lambda function:
<unicode string>:53:7: error: 'zeroValue' was not declared in this scope
<unicode string>:54:7: error: 'fullValue' was not declared in this scope
<unicode string>:55:59: error: could not convert '{<expression error>}' from '<brace-enclosed initializer list>' to 'esphome::optional<float>'
Compiling .pioenvs/pantry-fabric-softener-monitor/lib057/noise-c/backend/ref/dh-curve25519.c.o
Compiling .pioenvs/pantry-fabric-softener-monitor/lib057/noise-c/backend/ref/hash-blake2b.c.o
*** [.pioenvs/pantry-fabric-softener-monitor/src/main.cpp.o] Error 1
Compiling .pioenvs/pantry-fabric-softener-monitor/lib057/noise-c/backend/ref/hash-blake2s.c.o
========================== [FAILED] Took 3.01 seconds ==========================
i believe you have to declare those variables differently. i wasn’t using variables in mine, just plugging in numbers directly. i think you declare global variables in esphome like this: