That’s due to epsilon error. If you search the term on this forum you’ll see it’s a fairly common issue that can occur with floating-point calculations:
https://community.home-assistant.io/search?q=epsilon%20error
As you’ve discovered, you can use round
to control the number of decimal places. Here’s today’s calculated value without rounding:
Here’s a version of the Template Sensor that rounds the calculated values to two decimal places:
my_azimuth_sensor:
friendly_name: 'My Azimuth Sensor'
value_template: >-
{% set x = states('sensor.date') %}
{% set today = now() %}
{% set dec21 = now().replace(day=21).replace(month=12).replace(hour=0).replace(minute=0).replace(second=0).replace(microsecond=0) %}
{% set jun21 = now().replace(day=21).replace(month=6).replace(hour=0).replace(minute=0).replace(second=0).replace(microsecond=0) %}
{% set previous_dec21 = now().replace(year=now().year-1).replace(month=12).replace(day=21).replace(hour=0).replace(minute=0).replace(second=0).replace(microsecond=0) %}
{% set jun20_value = 127 + ((jun21 - timedelta(days=1)) - previous_dec21).days * 0.31 %}
{% if today >= dec21 %}
{% set y = 127 + (today - dec21).days * 0.31 %}
{% elif today < jun21 %}
{% set y = 127 + (today - previous_dec21).days * 0.31 %}
{% else %}
{% set y = jun20_value - ((today - jun21).days * 0.31) %}
{% endif %}
{{ y | round(2) }}
Here’s the calculated value it produces:
It’s not just one closed parenthesis, it has a matching open parenthesis:
((jun21 - timedelta(days=1)) - previous_dec21).days
^ ^