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

Hi
I need to create 2 sensors or templates not sure yet.
The first one is regarding my hot water temperature sensor. = sensor.temperature_purifier
Right now is at 65.
I need to make a trigger when its temperature raise to 66 for example.(template or sensor?)
I only need to triiger once when and if it’s value raised.
Either way I don’t know how to start.

The second one I find it even more difficult.
I have a start point at 21 of December. Its value is 133,44. I want each day to add 0.25 so 22 Dec = 133,69 and so on. From 21 of June I would like to the value of 20th of June to reduce for 0.25 and keep reducing for 0.25 until the 21 of December.

This I guess should be a sensor (I?) in order to compare it with sun azimuth.

My problem is as I mention that I don’t know how to begin. If someone can give me an example it would be very helpful.

This sounds more like you want an automation. What is it that you want to happen when your temp sensor gets above 65 degress ?

What are you doing with this value ?
Home Assistant already calculates sunrise / sunset for your current location to which you can add a time offset. Would this be helpful ?

To get a notification in my phone and (this will seem strange) probably use it as a condition for an automation to close my covers…

No the sunset - sunrise wouldn’t help.
I need it to compare with sun azimuth value.
If sun azimuth is > than my sensor value then close covers.

You can use a Numeric State Trigger.

Paste this into the Template Editor and experiment with it. To test it, change the month and day of the first line and observe the result. For example it is currently set to June 26 (month=6 day=26).

{% set today = now().replace(year=2020).replace(month=6).replace(day=26).replace(hour=0).replace(minute=0).replace(second=0).replace(microsecond=0) %}
{{ today }}
{% 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 = 133.44 + ((jun21 - timedelta(days=1)) - previous_dec21).days * 0.25 %}
June 20 value is: {{ jun20_value }}

{% if today >= dec21 %}
 A) {{ 133.44 + (today - dec21).days * 0.25 }}
{% elif today < jun21 %}
 B) {{ 133.44 + (today - previous_dec21).days * 0.25 }}
{% else %}
 C) {{ jun20_value - ((today - jun21).days * 0.25) }}
{% endif %}

If your testing confirms the calculations work the way you want, the template can be used to create a Template Sensor like this:

#sensor:
- platform: template
  sensors:
    my_sensor:
      friendly_name: 'My Sensor'
      value_template: >-
        {% 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 = 133.44 + ((jun21 - timedelta(days=1)) - previous_dec21).days * 0.25 %}
        {% if today >= dec21 %}
          {{ 133.44 + (today - dec21).days * 0.25 }}
        {% elif today < jun21 %}
          {{ 133.44 + (today - previous_dec21).days * 0.25 }}
        {% else %}
          {{ jun20_value - ((today - jun21).days * 0.25) }}
        {% endif %}

EDIT

I overlooked to mention that if you plan to use this Template Sensor in version 0.117 then it will update every minute. If you want to use it with an older version, we need to add a reference to sensor.time in the template.

The sensor is perfect! I am trying to “read” your code and I don’t understand most of it… :slight_smile:

I need the sensor to update once every morning (I think).
I will update to 0.117 but after the 0.117 .2 or .3 in order to be stable.
As it is right now in 0.116.4 how often this sensor updates?
In 0.117 version I would to prefer to update a lot than per minute since I only need the day value once per day.

Thank you very much!

Only when you start Home Assistant and then never again (until the next restart). Obviously that’s undesirable.

Add this to the template on the first line and it will cause the template to be updated at the start of each new day:

        {% set x = states('sensor.date') %}

It assumes you have already configured the Time & Date integration and defined sensor.date.

so, template sensor should be

#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 %}

I just added this

- platform: time_date
  display_options:
    - 'time'
    - 'date'
    - 'time_utc'
    - 'beat'

I think it will be ok.
Just to understand the logic
With this code

{% set x = states('sensor.date') %}

we just force HA to update when the day changes? eg at 00:01?

Correct. However, in 0.117 you can remove it because the now() function will automatically cause the template be updated every minute.

If you’re thinking “But I don’t need this Template Sensor to be updated every minute (1440 times a day), once a day is enough” that has been discussed with the development team and they feel the extra processing is not a burden for Home Assistant.

NOTE

It’s possible to perform all of the date calculations without using the now() function. It can be based on sensor.date but it’s not quite as neat as using now().

1 Like

the hot water temperature values varies a lot. the minimum usually is 30 and the maximum is 70 lets say.
My purpose is to catch up the first time when the temperature rises. from 30 to 31 or 50 to 51 etc
in order to achive this should I use something like

above: 1

In a Numeric State Trigger, the above option is a threshold. The moment the value increases above the threshold, the trigger occurs. For example, this automation will be triggered when the value of sensor.temperature increases above 50.

  trigger:
    platform: numeric_state
    entity_id: sensor.temperature
    above: 50

It will not trigger again if the value continues to increase to 52, 53, etc. It only triggers when the value crosses the threshold. To reset the Numeric State Trigger, the value must first fall below the threshold (below 50). The next time it increases above 50, it will trigger the automation again.

If you want an automation to be triggered when the temperature rises above two thresholds, you can use two Numeric State Triggers:

  trigger:
  - platform: numeric_state
    entity_id: sensor.temperature
    above: 30
  - platform: numeric_state
    entity_id: sensor.temperature
    above: 50

It will trigger when the temperature increases above 30 and trigger again when the temperature increases above 50.

For the automation to be triggered again by the first trigger, the temperature must first decrease below 30 and then rise again above 30.

Can I set (somehow) in the evening at 01:00 for example the current temperature to be the “above” number?

Why?

Forcing a temperature sensor’s value is an unusual procedure. Your forced value will last only as long as the next temperature measurement is received by the sensor. For example, if the sensor reports 34 at 00:59 and you set it to 42 at 01:00, it will change back to 34 (or some other value) the next time the sensor gets a new measurement (possibly as soon as 01:01).

Probably, I didn’t express it right, and I forgot to mention that this is for my Solar Water Heater :slight_smile:

I mean, if at 01:00 o’clock the sensor temperature is at 30 or 35 or 70 or whatever value it will be
then ideally I need this value to be the threshold (“above”) so as to understand when the sun start heating the water

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