Converting/filtering state values

Could someone kindly explain what I a seeing/missing please? I have a standard history_stat sensor that records a zone’s irrigation time in hours. I then have a template sensor that (I’m trying) to convert the state value in hours to minutes with one decimal place of precision. I’m not getting the result I expect - even trying things in the template editor. Here’s the current value of the history_stat sensor and my template editor attempts to resolve this…

Line 1 displays the current state value of the object (I assume string?)
Line 2 is the template sensor function (via filtering). I expected 24.6 not 24.599999999999998!
Line 3 Using a constant I get the expected result.
Line 4 Expanding out further with all constants just for giggles. Unexpected result here too.

Obviously I am not understanding something about accessing the state value or type correctly and/or the template filtering logic. This is fundamental and I’d appreciate it greatly if someone could shed some light on this for me. Thank you and kudos to all those that are contributing to this wonderful HA ecosystem.

Hello makerglenn and welcome to the community! :partying_face:

I think you’re stumbling over: Templating - Home Assistant

The default priority of operators is that the filter ( | ) has priority over everything except brackets.

So you need to put brackets around the first operation:

{{ ( states('sensor.nw_wall_time') | float * 60) | round(2) }}

Oh Geeez! Sometimes you miss the obvious when looking for something deeper…LOL!
Thank you for your eyes!

1 Like

I didn’t know about this either, so we both learned something new :smile:

I originally thought the filtering was linear from left to right performing an evaluation at each filter (i.e. “|”). That is why I was confused. Also, never think someone else’s code works! I lifted the original formula (hours to minutes) from another post! It was wrong…

(Once in a while you get shown the light In the strangest of places if you look at it right.)

1 Like