How to configure multiple conditions (I think)

Hi,

I am getting a reading in cm via MQTT from an Oil Tank sensor which i display in HA. However I am struggling to work out how to convert the value into litres and a bar graph. I have a spreadsheet with the value from 1 - 10 for the bar graph and the litres like this:

65cm - 575l - 5

So 65cm = 575 litres or 5 on the bar graph.

But how do I get this to work in HA?

Would I use a condition and how?

Any help greatly appreciated.

what’s the radius of the oil tank? And orientation

EDIT: Assuming you have a vertically standing tank with a radius of 50 cm… I reversed the calc and got 53.0638224516 cm. So…

sensor:
  - platform: template
    sensors:
      oil_tank_liters:
        value_template: >
          {% set height = states('sensor.oil_tank_depth') | float %}
          {% set radius = 53.0638224516 %}
          {{ ((3.14159265359 * radius ** 2 * height) / 1000) | round(2) }}
        unit_of_measurement: l
      oil_tank_bar:
        value_template: >
          {{ (states('sensor.oil_tank_liters') | float // 1000) }}
        unit_of_measurement: bar

If your tank is horizontal, the equation would be very different.

Hi Petro,

Thanks for the response. I have tried the above, but unfortunately the tank is not round so makes the calculation way out. However I was able to get the manufacturers data for the tank, and have worked out the exact litres from the height which I have in a spreadsheet. What I am trying to understand is how to use that information. So I know that at 68cm I have 602 litres according to my spreadsheet. I am trying to work out how to get that from the spreadsheet, and I thought I might be able to do it via a condition, ie if I have a reading of 68cm it would equal 602l. I can attach the spreadsheet if that makes it easier to understand. I don’t really want to use the spreadsheet but somehow create a condition or something that allows me to say that at 65cm it = xx litres and at 70cm it = xx litres etc.

What equation are you using for the spreadsheet? Can you post the data here?

Essentially The Depth is from the manufacturer I have converted this to CM and also Litres the last column (column D) is what the watchman oil guard I have displays, it shows a 1 to 10 and I have worked out what they would be in the spreadsheet as well. I guess what I am looking for is a way to add the litres. Note I am not doing any calculation of the temp so I am aware this will always be slightly incorrect. I wish I could do it via a PHP script but like I said the dimensions of the tank made it way to tricky for me to figure that out.

The details I used to figure out the tank details are from these two websites:

https://www.fueltankshop.co.uk/files/pdf/pdf3558.pdf - Tank Dimensions

https://www.fueltankshop.co.uk/files/pdf/pdf3558_c.pdf - Fuel Calibration Chart with curve

The math seems simple as the graph show a linear correlation

Liters = millimeters * 1250 / 1400

Man, you made this painful. Next time, add a linear trendline to the graph or provide data that can be copied and pasted (i.e not images).

Anyways these should work

sensor:
  - platform: template
    sensors:
      oil_tank_liters:
        value_template: >
          {% set height = states('sensor.oil_tank_depth') | float %}
          {{ (9 * height - 10) | round(0) }}
        unit_of_measurement: l
      oil_tank_bar:
        value_template: >
          {{ states('sensor.oil_tank_liters') | float // 1000 }}
        unit_of_measurement: bar

Hello Petro,

Sorry for making it so difficult. I’m sorry I don’t even know what a linear treadline is.
I put the code you suggested and get the following in the logs:

[homeassistant.components.template.sensor] Template sensor oil_tank_liters has no entity ids configured to track nor were we able to extract the entities to track from the value template(s). This entity will only be able to be updated manually.

The code I had originally to display the Oil Tank Depth in CM is:

- platform: mqtt
    name: "Oil Tank Depth"
    state_topic: "oiltank/level"
    value_template: "{{ value_json.depth }}"
    unit_of_measurement: cm

if you don’t care about the depth then use this for your mqtt sensors

- platform: mqtt
    name: "Oil Tank Volume"
    state_topic: "oiltank/level"
    value_template: "{{ (9 * value_json.depth | float - 10) | round(0) }}"
    unit_of_measurement: l
- platform: mqtt
    name: "Oil Tank Bars"
    state_topic: "oiltank/level"
    value_template: "{{ (9 * value_json.depth | float - 10) // 1000 }}"
    unit_of_measurement: bar

Thanks @petro

I am now getting this under sensors, so it looks like the bar is getting no info:

Also no matter what I try i keep getting an error saying Entity not available: sensor.oil_tank_depth even though it is not referenced anywhere.

oiltank3

and the other two sensors do not show.

you’re referencing it in lovelace configuration. As for the bar measurement remove a 0 on the 1000.

- platform: mqtt
    name: "Oil Tank Bars"
    state_topic: "oiltank/level"
    value_template: "{{ (9 * value_json.depth | float - 10) // 100 }}"
    unit_of_measurement: bar

Thanks @petro That all works now, although weirdly the bar shows 6 and not 5, which is what I would expect, but the litres is exactly correct.

is that last column the bar level? I thought it was just the litres/100

well anyways, i’m goint to assume it’s the unlabeled column and taht would make the equation:

- platform: mqtt
    name: "Oil Tank Bars"
    state_topic: "oiltank/level"
    value_template: "{{ value_json.depth | int // 14 + (value_json.depth | int % 14 != 0) | int }}"
    unit_of_measurement: bar

Thanks @petro

That is working correctly now as far as I can tell.

So am I correct in saying I cannot crate an actual gauge or bar graph graphically to display the oil tank bars without using lovelace which won’t work because of the value_template?

You’d need to use lovelace with a custom card.

Thanks @petro, so my last question, am I right in saying that I cannot use value templates with lovelace based on what you said above? Which is why they don’t show?

I don’t know what you’d use a value template for? Lovelace is an opt in user interface. That means you have to place the items in the interface. If they aren’t showing up its probably because you didn’t configure them to show up.

@petro

Ah okay, so to get this to show:

    name: "Oil Tank Bars"
    state_topic: "oiltank/level"
    value_template: "{{ value_json.depth | int // 14 + (value_json.depth | int % 14 != 0) | int }}"
    unit_of_measurement: bar ```

I need to somehow configure them to show? Sorry I am new to this.

it should already be in your interface if you’ve never set it up. It will show up as an entity row and it will just show a value of 5. It will not look like a bar. For configuring the UI take a look at the lovelace documentation and watch the video and read the blurbs and play around with the test interface.

@petro Thank you.

You have been very helpful, I appreciate it.