Template sensor differs from Template in DeVTools

Hi

I have small problem which I can’t find out how to solve.
I made template sensor to recalculate PM2.5 value to have as percentage.
sensor.air_quality_pm25 is my sensor

So I made this:

   - platform: template
     sensors:
       in_pollution_perc:
         value_template: "{{(((states.sensor.air_quality_pm25.state|round)/25)*100)|round|int}}"
         friendly_name: "In PM2.5 %"
         unit_of_measurement: "%"

And it’s working, but out come is something like 32.0% or 40.0000000000001%, but when I was testing SAME template value

 {{(((states.sensor.air_quality_pm25.state|round)/25)*100)|round|int}}

, then it was just INTEGER (32% or 40%).

Any ideas?

Please format your code correctly. Use three backticks ( ` ) above and below the sections of code.

done, fixed (talking about code and backticks).
@Tediore
Now you can add something more except “format to have it as in forum”? :slight_smile:

Thanks, makes it easier to read when formatted properly.

So that odd rounding (40.0000000000001) looks like floating point rounding error which is unfortunately unavoidable, even if you add the int filter at the end (I believe). As for why it displays differently in the template tool I’m not sure. I did, however, clean up your code:

"{{ ((states.sensor.air_quality_pm25.state | float) / 25 * 100) | round | int }}"

Good practice to add a float filter to a number you’re using in a template to ensure it is parsed correctly. The int filter shouldn’t have an effect since you’re rounding to zero decimal places but I left it in anyway since it shouldn’t have an effect in this case being at the end of the value template.

Also, note that this template will accomplish the same as above with the added benefit of preventing errors from appearing in your log when the underlying sensor isn’t ready (such as upon startup):

"{{ ((states('sensor.air_quality_pm25') | float) / 25 * 100) | round | int }}"

Not quite sure of the way this processes the order but /25*100
Will either equate to /2500 or *4 (can’t check at the moment)
That would make it shorter still.
What number range do you expect ?
We could use text truncation and then just post that eg [0:3]

Geez I must be really tired, probably shouldn’t have removed those other parentheses…

Order of operations for division and multiplication is left to right so no extra brackets are required. Your answer was correct.

1 Like

Yeah but if you go back to the OP then the /25 was bracketed first then the *100 so I was wrong in both cases it should be *4 :crazy_face:
Nanananar !

1 Like

Ah thanks. I thought that was the case but I doubted myself when someone commented :wink:

Those extra zeros and 1 at the end of your number is epsilon error. To remove:

 - platform: template
   sensors:
     in_pollution_perc:
       value_template: >
         {% set value = (((states.sensor.air_quality_pm25.state|round)/25)*100)| round %}
         {{ "%0.2f"%value }}
       friendly_name: "In PM2.5 %"
       unit_of_measurement: "%"
2 Likes

Gonna deposit that into my tasty knowledge account.

@Tediore
@petro
Thank you very much guys :slight_smile:

Sorry for this incorrect start from my side, but leasson learned, now I know a bit more. It’s like reading manuals - mostly nobody is doing that and then this person is suprised…
I will check this right now :slight_smile:

Almost good :slight_smile:

Now it’s showing “xxx.xx%”
How to get rid of this “.xx” ?

change the “%0.2f” to “%0.0f”

1 Like

Super, thank you!
Now it’s perfect :slight_smile:

1 Like

Hi

@petro
I have to come back again to this topic since after last update to 0.115, this stopped to work…

- platform: template
    sensors:
      out_pollution_perc:
        value_template: >
          {% set value = (((states.sensor.out_pm2_5.state|round)/25)*100)| round %}
          {{ "%0.0f"%value }}
        friendly_name: "Out PM2.5 %"
        unit_of_measurement: "%"

Now it’s “unknown”, but… when I’m trying this in DEV TOOLS/template, then it works OK…

          {% set value = (((states.sensor.out_pm2_5.state|round)/25)*100)| round %}
                                                            ^
                                                            |

you’re rounding a string. change the indicated round to float.

1 Like

Awesome!
You are great, thank you :slight_smile:

Hi again,

Interesting thing is that it seems that it is some different problem…
When I’m editing template and then reloading just templates, it works OK. But after restart of HA, then value is incorrect… Any ideas? :slight_smile: