Help to create 2 templates or sensors - I don't know how to start

I’ve added sensor.my_azimuth_sensor to my system so we can compare results tomorrow.

Screenshot from 2020-10-27 11-00-55

FWIW, everything will change in 0.117 because sensor.date will no longer be needed in the template.

1 Like

Before a while the date changed here so the sensor value. So it works as it should. Dont know why it didn’t yesterday

1 Like

I can confirm that my instance of this sensor also updated today and is reporting 143.43 (which is correct).

Yes, it working fine. I will make one more sensor to open the covers when sun is gone.
Meanwhile I am looking for a reliable way to know if the weather is sunny or not.

*Good morning :slight_smile:

Is it simply for weather forecast or for determining light levels?

for weather forecast,. today is cloudy here but openweather reported sunny so my covers closed but it wasn’t necessary.

In my experience, you need to try several sources of weather forecasting and then choose the one that provides the most reliable forecast for your location. In my case, the national weather office proved to be the best.

Alternatively, you can use an exterior light sensor and base your actions on its measurements. You may wish to use the Filter integration to shape the data (i.e. reject temporary deviations from the trend).

1 Like

Hi
before a while I tried to change the start value of 21 Dec from 127 to 126 just to be a little more accurate.
I replaced the values in the sensor template and restarted HA.
Now the state of the sensor is unknown.
Do you know why?

Yes and it’s due to a subtle bug in how the date is set for dec21 and jun21.

Here’s the corrected version:

    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 %}
          {{ 127 + (today - dec21).days * 0.31 }}
        {% elif today < jun21 %}
          {{ 127 + (today - previous_dec21).days * 0.31 }}
        {% else %}
          {{ jun20_value - ((today - jun21).days * 0.31) }}
        {% endif %}

The only difference from the original are the following two lines:

        {% 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) %}

If you compare them to the original, you’ll notice they begin by replacing the day first and then the month. The original version is opposite, it replaces the month first, then the day.

The problem is today (now()) is October 31. If I change the month first, to June, it produces June 31 which is an invalid date. That’s the bug.

Yes, now it is fixed. To be honest I still trying to understand how you do the calculations and I can’t.
I will try tomorrow to reproduce these formulas in a excel but I am not sure I can make it.
Please tell me that your job is a programmer or something relative… :slight_smile:

Hi, although it is working ok, the number format is lije 141.200000003 so I tried to add round(2) in the formulas. It can change the number format only in the below formula however the result is not corect.
I don’t mind to stay this way, but something seems strange to me thats why I am asking.
So in the below formula why there is one parenthesis in —> previous_dec21). ?

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:
Screenshot from 2020-11-01 07-56-21

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:
Screenshot from 2020-11-01 08-04-16

It’s not just one closed parenthesis, it has a matching open parenthesis:

 ((jun21 - timedelta(days=1)) - previous_dec21).days
 ^                                            ^
1 Like

If you have no additional questions concerning this topic, please consider marking my post (above) with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. In addition, it will place a link below your first post that leads to the solution post. This helps other users find answers to similar questions.

1 Like