Changing template from float(...) to (...) | float(0)

Heyhey
Im using the following formula to calculate the total humidity in my config.yaml.

{{ ( 1000*e**(19.016-(4064.95/(float(states('sensor.shellyplusht_kasse_temperature'))+236.25)))*100/(461.66*(float(states('sensor.shellyplusht_kasse_temperature'))+273.15)) * float(states('sensor.shellyplusht_kasse_humidity'))/100 | float) | round (2) }}

In addition to this Im using an availability sensor:

{{ is_number(states('sensor.shellyplusht_kasse_temperature')) and is_number(states('sensor.shellyplusht_kasse_humidity')) }}

But I found out availability is wrong in my case, as this raises errors when total humidity sensor is not present. (e.g. at start when z2m takes longer then ha to startup),
so I want to move this template from config.yaml to my helpers and I want to have default values for the case one of the sensors is not reachable.
So I changed the formula to the following in my helpers:

{{ ( 1000*e**(19.016-(4064.95/(states('sensor.shellyplusht_kasse_temperature') | float(0) +236.25)))*100/(461.66*(states('sensor.shellyplusht_kasse_temperature') | float(1) +273.15)) * (states('sensor.shellyplusht_kasse_humidity') | float(0))/100 | float(0)) | round (2) }}

Now the result is the same (tested in Developer Tools → Template) and if I change any of the sensors’ name to nonsense it seems like the correct default (set after float()) is used.

But im not 100% knowing what Im doing here, so can anyone please confirm that both formulas are the exact same, or did I do something wrond?

At first glance. no need for the second float.

| int(0))/100 ) | round (2)

availability: >
  {{ has_value('sensor.shellyplusht_kasse_temperature') and  
     has_value('sensor.shellyplusht_kasse_humidity') }}

@LiQuid_cOOled
My helper formula

{{ ( 1000*e**(19.016-(4064.95/(states('sensor.shellyplusht_kasse_temperature') | float(0) +236.25)))*100/(461.66*(states('sensor.shellyplusht_kasse_temperature') | float(1) +273.15)) * (states('sensor.shellyplusht_kasse_humidity') | float(0))/100 | float(0)) | round (2) }}

returns 6.15
If Im using your suggestion and change it to:

{{ ( 1000*e**(19.016-(4064.95/(states('sensor.shellyplusht_kasse_temperature') | float(0) +236.25)))*100/(461.66*(states('sensor.shellyplusht_kasse_temperature') | float(1) +273.15)) * (states('sensor.shellyplusht_kasse_humidity') | int(0))/100 ) | round (2) }}

it returns 6.07
If I leave your formula as it is and change the convertion to int into convertion to float it returns 6.15 again.
So there shouldnt be the need for my second float, as you assumed.
But in this case im pretty sure float is the better datatype and it should look like this:

{{ ( 1000*e**(19.016-(4064.95/(states('sensor.shellyplusht_kasse_temperature') | float(0) +236.25)))*100/(461.66*(states('sensor.shellyplusht_kasse_temperature') | float(1) +273.15)) * (states('sensor.shellyplusht_kasse_humidity') | float(0))/100 ) | round (2) }}

@tom_l
Your availability sensor obviously looks much more usefull than mine.
But if I understood its function correct then there is no absolut humidity sensor if the availability template returns false.
So if i move the sensor to helpers and give default values for missing sensor data I get (incorrect) data.
If I use availability there will be no absolut humidity sensor at all if availability is False.

Float vs Int is your use case is fine. I was referencing the back to back floats

| float(0))/100 | float(0))

There is no availability option in the GUI. So you would have to do this for the state template:

  {% if has_value('sensor.shellyplusht_kasse_temperature') and  
     has_value('sensor.shellyplusht_kasse_humidity') %}
    {{ ( 1000*e**(19.016-(4064.95/(states('sensor.shellyplusht_kasse_temperature') | float(0) +236.25)))*100/(461.66*(states('sensor.shellyplusht_kasse_temperature') | float(1) +273.15)) * (states('sensor.shellyplusht_kasse_humidity') | float(0))/100 ) | round (2) }}
  {% else %}
    {{ this.state }}
  {% endif %}

This will keep that last known good state if one of the sensors becomes unavailable.

Note I did not actually check the maths or syntax in your template. .

1 Like

And what happens after restarting, when z2m does not provide any sensor data?
I then get my float default values as there is no “last known good state”?

It will have the state unknown

Hmm I think ill then prefer a wrong sensor value, due to float defaults.
In many (really many) mushroom condition cards I check if the value is >40 or <60.
Im too lazy changing all of them, as unknown > 40 surely raises an error.

I now got it.
Ofcourse I do not prefer wrong sensor data (with some thinking).
Ill use your template and change my lovelace cards to fetch the error.

Thanks a lot you two for helping me :))

1 Like