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

To make the above option adjustable, you would need to use a template but neither above or below support templates.

If you want more flexibility, you will need to use a Template Trigger. For example:

  trigger:
    platform: template
    value_template: "{{ states('sensor.temperature') | float > 50 }}"

The template gets the value of sensor.temperature, converts it to a floating point number, then checks if it is greater than 50. If it is, the automation is triggered.

If we replace 50 with an input_number, we can adjust the value of the input_number (either via the UI or by an automation).

input_number:
  threshold_temperature:
    min: 10
    max: 100
    step: 1
  trigger:
    platform: template
    value_template: "{{ states('sensor.temperature') | float > states('input_number.threshold_temperature') | int }}"

Here’s a simple automation to set the input_number to the sensor’s value at 01:00.

  trigger:
    platform: time
    at: '01:00:00'
  action:
    service: input_number.set_value
    data:
      entity_id: input_number.threshold_temperature
      value: "{{ states('sensor.temperature') | int }}"

Of course, the actual automation will need to be more sophisticated because at some point you will want it to set input_number.threshold_temperature back to the preferred values (30 or 50 or whatever it is you wish to use).

1 Like

That’s a good start! I will study it tomorrow and try to adjust to my needs.
Thanks a lot!!

1 Like

Hi, this didn’t worked today. The sensor value stayed the same like yesterday.

That’s odd. When you check Developer Tools > States, is sensor.date reporting today’s date?

Yes, it does.
I will check it again tomorrow, but at least today the sensor value changed when I restarted HA

That’s not a good indicator. All Template Sensors are updated at startup. If they lack entities, they will never be updated again (until the next restart). This template lacks entities, which is why I suggested to include the line containing sensor.date. It changes state at the beginning of every new day, thereby causing the Template Sensor to be updated at that same moment.

The fact it failed to do that today implies something is wrong.

  • Did you add and configure the Time & Date integration (sensor.date) yesterday or was it already installed?
  • If you added it yesterday, did you restart Home Assistant afterwards?
  • After you added the suggested line to the template, did you execute Reload Template Entities (or restart Home Assistant)?

If you did all those things and the Template Sensor still failed to update automatically today, something somewhere isn’t working correctly.

Yes, yesterday.

Yes, I did.

I restarted HA.

So probably something is wrong.

#sensor:
- platform: template
  sensors:
    my_azimuth_sensor:
      friendly_name: 'My Azimuth Sensor'
      value_template: >-
        {% set x = states('sensor.date') %}
        {% set today = now() %}
        {% set dec21 = now().replace(month=12).replace(day=21).replace(hour=0).replace(minute=0).replace(second=0).replace(microsecond=0) %}
        {% set jun21 = now().replace(month=6).replace(day=21).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 %}     

date sensor

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