Tasmota firmware and SR04 ultrasonic sensor

So I have finally managed to complete my ultrasonic sensor which monitors the level of our heating oil.
I have enable MQTT support and using teleperiod I’m uploading sensor reading every one hour. It works great and it is very accurate.

Here is how my graphs looks:

εικόνα

The problem I want to solve is although mqtt is updating every hour, sensor is working constantly.
Is it possible to switch off the sensor for an hour too? Then switch it on again, get new value, update mqtt server and wait for another hour ?

I know you can do it via commands but I can’t figure out which are these commands?
Maybe a rule which will be triggered by time?

Would apreciate any help.

Also apart that I would like some help how to calculate volume of my tank.
It’s a horizontal cylinder. The mathematical equation for volume is

Volume = πr2h

How can I make this a template which will also give me the volume of the tank?

I have already template the height as following:

  - platform: mqtt
    name: "Oil Level Sensor"
    state_topic: "Oil-Level/tele/SENSOR"
    unit_of_measurement : "cm"
    value_template: "{{value_json['SR04'].Distance }}"
    icon: mdi:arrow-expand-vertical

Awaiting the pleasure of your reply

I am using SR04 with esphome to monitor my water tank levels. I have made it to turn on every half an hour and then take 3 readings over 10second interval and then turn off. If you want i can share that code with you but you will have to setup and install esphome. if you want any help with that i can help

I have tried tasmota for this purpose but i couldn’t find anything as you asked for.

Thank you for your reply.
I gave up with Arduino and ESPhome as I had many disconnects.

Tasmota MQTT client is far more stable than the rest too.
So thank you but I will stick with Tasmota.

Am sure that someone out there has find a way so sensor is not working constantly.

In my case I dont see any issue with my esphome connectivity till now. Besides that except this device all my other devices are tasmotas only. More than that esphome works with its own integration and I dont rely on mqtt maybe that would be why i havent seen any connectivity issues. I havent tested it with mqtt till now.

Would you please someone help me with the volume calculation?

Did you see this post.

thank you for your reply
I found this formula on the internet
(r-h)*1000/r will give me liters

so based on the above I came up with this template

  - platform: template      
    sensors:
      oil_in_tank:
        friendly_name: "Oil left in tank"
        unit_of_measurement: 'liters'
        value_template: "{{ (200 - states('sensor.oil_level_sensor') | float)*1000/200 }}" 

Which I really don’t know if is correct or not.
What I know for sure is that it looks correct.

Any help would be more than welcome
thank you in advance.

I dont know if this formula is right. I have strong concerns that this could be wrong. As the volume of a horizontal tank should contain a factor of the angle made by the arc which is filled by the fluid. But you can try this maybe after simplification we ight end up with this. Please make sure you test the accuracy before use. What is the volume of tank as mentioned in the product?

Also Instead of 1000/200 you can give 5

{{ (200 - states('sensor.oil_level_sensor') | float)*5 }}"

You can test if this is right by bleeding the tank empty and measuring the bled oil and then refilling it. If you need any tweaks you can do it after assessing that.

Thank you so much for your prompt reply.
The number 1000 is there so I can convert value to liters.

The diameter of the tank is 200 cm.
Tank’s height is 200cm too. If the tank is empty the formula looks like this:

(200 - 200)*1000/200

If the tank is full the formula look likes this :

(200-1)*1000/200 = 995 Liters (which is correct because this is my tank’s volume)

What do you think?
Does it look correct?

Because for example if we say that tank is half full the formula will look like this

(200 - 100)*1000/200

Which gives 500 Liters … Which is also correct

Am I wrong? Cause I really don’t know and please excuse me if my math are not that good.

In your formula if you want to divide the entire * (200 - states(‘sensor.oil_level_sensor’) | float)1000 by 200 you will have to do it like this.

{{ ((200 - states('sensor.oil_level_sensor') | float)*1000)/200 }}

Or else according to BODMAS rule the division of 1000 with 200 could be done first.

Except that the logic looks good. If you get a chance test this at empty, half and full.

thank you for your time.
I tried my formulas at Developers Tools / Template and I get the following:

Then using the same entity on Lovelace I get this:

image

I get 749.56 Liters instead of 390.625 which the template editor is giving me (see above)

please share the lovelace config and the config of template sensor if any.

here is the lovelace config

type: horizontal-stack
cards:
  - type: sensor
    entity: sensor.oil_level_sensor
    graph: line
  - type: gauge
    entity: sensor.oil_in_tank

and here is the templates for the above two entitites

  - platform: mqtt
    name: "Oil Level Sensor"
    state_topic: "Oil-Level/tele/SENSOR"
    unit_of_measurement : "cm"
    value_template: "{{value_json['SR04'].Distance }}"
    icon: mdi:arrow-expand-vertical

    
  - platform: template      
    sensors:
      oil_in_tank:
        friendly_name: "Oil left in tank"
        unit_of_measurement: 'liters'
        value_template: "{{ ((100 - states('sensor.oil_level_sensor') | float)*1000)/128 }}"      

Did you check what is the state of sensor.oil_in_tank in the devloper console?

ofcourse!
here it is

εικόνα

I tested the same template with one of my sensors and it returns correct value. I cant see any issues, its very strange. Please try deleting the sensor and adding it again.
If it is not working you can try an alternate template but this is all the same.

{{ ((100 - states.sensor.oil_level_sensor.state | float) * 1000)/128 }}

it seems that the gauge retained the first value I tested.
It didn’t update with new value.

As soon as I restarted home assistant it returned back to normal

εικόνα

thank you for your help!

1 Like

thank you for all your help! Really appreciate it
Kindly please let me know if is possible to turn off sensor with tasmota?

And make it work once every 30 minutes?

Because although I have set teleperiod=1800, sensor is constantly reading oil level.
Which is something that I don’t need.

Is there a way I can make sensor to work once every 30 minutes?

I havent found a way to do that till now. You could try something with deepsleep but its complicated. This is the only reason why I had to shift my sensor to esphome.

You can do one thing to make the sensor not work but its not elegant method. You can use a relay or a transistor to cut the trigger line of the sr04 when you dont want it in use. I guess you already have free pins and vcc with the esp8266 module. You can use this relay to trigger and block the sensor. In theory it should work but do it only if so desperate.

Unfortunatelly there is no way of doing it.
Although is easy with other platforms such as arduino and esphome, it is not possible with Tasmota.

With Arduino for example you can use a digital pin and switch off the power, when you wish, in your sketch.

For me it is a disadvantage but there are so many other advantages that I forget about it.