Why is maths in HA so hard?

First post. I’m new to HA after using Domoticz for a long time. In fact still plan to use it for some stuff via MQTT.

So first project I have is monitoring an oil tank. I have a process grabbing values and pushing to an external MQTT instance I have.

I have HA getting the value (its not JSON at present) however the damn maths I need to do is just not working correctly.

From my config:

mqtt:
  #Oil Tank Monitors
  - sensor:
      unique_id: oiltank-1
      name: "Oil Tank Air at Top"
      state_topic: "sensors/rtl_433/Oil-SonicSmart/142703594/depth_cm"
      value_template: "{{ value }}"
      unit_of_measurement: "cm"

  - sensor:
      unique_id: oiltank-2
      name: "Oil Percent"
      state_topic: "sensors/rtl_433/Oil-SonicSmart/142703594/depth_cm"
      #my tank is 120cm high, depth 180cm and width 120cm.   1cm in height is 21.6l.   Tank capacity is 2500
      value_template: "{{ (((120-(value))*21.6)//2500)*100 | int }}"
      unit_of_measurement: "%"

  - sensor:
      unique_id: oiltank-3
      name: "Oil Litres"
      state_topic: "sensors/rtl_433/Oil-SonicSmart/142703594/depth_cm"
      #value_template: "{{ 120 - (value) * 21.6 | int }}"
      value_template: "{{ (120-(value))*21.6 | int }}"
      unit_of_measurement: "L"

  - sensor:
      unique_id: oiltank-4
      name: "Oil Depth Left"
      state_topic: "sensors/rtl_433/Oil-SonicSmart/142703594/depth_cm"
      value_template: "{{ 120 - (value) | int }}"
      unit_of_measurement: "cm"

Oiltank-1 works perfectly, as does oiltank-4. The other two just dont give me back the correct values. I looked at some posts about Jinja formatting but did not make sense.

Anyone got any advice !

Thanks

Try this:
value_template: "{{ ((120-value)*21.6) | int }}"

…and this for oil tank 2
value_template: "{{ ((120-value)*21.6/25)| int }}"

1 Like

Your percentage is going to be off at the upper end if you don’t account for the 92 liter difference between the 2500 nominal value and the value reached by multiplying h*d*w.

1 Like

Just to be explicit, it’s not that maths is hard, it’s the order of precedence is not documented with Jinja. I often make the same slip-up. The filter operator ("|") has a high precedence (for example, above ±*/), so you need to put parentheses around everything as shown by jchh.

{{ 1.1 + 1.2 | int}}
{{ (1.1 + 1.2) | int}}

This outputs “2.1” and “2”, because in the first example, only the “1.2” is converted to integer.

I would alo be inclined to have one MQTT Sensor for the main data, and Template Sensors for the others that are calculated from it. Personal preference I guess.

1 Like

Good point… ill check this later !

@michaelblight Thanks for the advice. Yes Ill be sure to check how do do that. The Jinja order is quite confusing. I’m used to being able to do similar multiple ways with Domoticz, at least as Python or LUA. Ill get there :slight_smile:

I believe Jinja follows the standard BODMAS sequence - just remember that filters act on the immediately preceding item so brackets are necessary if you want it to filter more than that.

value is a string, value_json is the string converted to json, which attempts to assign types. If your value is just a number, you want to convert it to a number with int or float. Also, do yourself a favor and follow standard coding practices, use spaces between each operation. It makes it easier to read. You also don’t need int division if you’re going to be casting to an int.

{{ ((120 - value | float) * 21.6 / 25) | int }}

{{ ((120 - value | float) * 21.6) | int }}

{{ (120 - value | float) | int }}
1 Like

@petro It did have them but one of my desperate phases included removing them. I’m finding there is a lot of HA information waaaay out of date floating around so been part of my issue. Its a learning curve which I am used to. But thanks for the advice.

Where are you looking?

The documentation is normally up to date and forum posts are time-stamped so you can tell the information’s age.

To parrot Taras, the documentation is not out of date. The forums typically have out of date information but it should be backward compatible for the most part. In regards to templates (jinja), they largely haven’t changed in 5+ years.

You should probably understand that many people on these forums copy/paste jinja. They do not take the time to learn it. You have to be able to identify who copies/pastes and who actually knows the code. Then copy/paste the people who know jinjas code :wink:

Yes, i get that. Sadly not everyone has plenty of time to investigate and learn. However they are all good points made and Ill endeavour to learn more. Unfortunately, I have had to spend time getting an RTL433 receiver and container to process my sensors then publish to MQTT, before doing the HA work. Ill get there

All the replies are appreciated !