Setting the time Problem

Hello everyone,

I have a problem with setting the time.

I have 2 helpers 1 is
‘input_datetime.living_thermostat_time_day’ set to 9:00 and another
‘input_datetime.living_thermostat_time_night’ 01:00

I would like to set a temperature of the heating to 23° between 09:00 and 01:00.

I have the following code:

´´´´
{% if today_at(states(‘input_datetime.living_thermostat_time_day’)) <= now() <= today_at(states(‘input_datetime.living_thermostat_time_night’))%}
{{states(‘input_number.living_thermostat_temp_day’)}}
{% else %}
{{states(‘input_number.living_thermostat_temp_night’)}}
{% endif %}

I can’t do this at all. I’ve tried everything but it doesn’t work.
Can someone help me with this?

Thanks in advance.

That is between 09:00 today and 01:00 tomorrow.

Your template checks between 09:00 today and 01:00 today.

Copy-paste the following template into the Template Editor and confirm it reports the correct value.

{% set start = states('input_datetime.living_thermostat_time_day') %}
{% set end = states('input_datetime.living_thermostat_time_night') %}
{% set day = states('input_number.living_thermostat_temp_day') %}
{% set night = states('input_number.living_thermostat_temp_night') %}
{{ day if today_at(start) <= now() <= today_at(end) + timedelta(days=1) else night }}

Thanks for your answer.

it didn’t solve my problem. If I now set the end time lower than the actual time it still gives me 23°? I really can’t figure it out

The idea is that I can adjust the times and between the two times (so for example between 09:00 and 15:00 or between 09:00 and 02:00) it should be 23° and outside the 2 times it should be 18°!!

That’s to be expected because the end time is now being set to tomorrow. The expectation is that the end time is normally shortly after 00:00 (like 01:00).

Any end time greater than 00:00 but less than the start time is a time that is tomorrow.

That’s why your original template failed because it attempted to find a time greater than 9 but less than 1 today and there’s no such time.

Thanks for your response.

Yes, I understand. But is there a solution for this? If so, how do I do this. I’ve been searching all afternoon, but can’t find a solution.

  • Between 9:00 today and 15:00 today.

  • Between 9:00 today and 02:00 tomorrow.

There’s no time that is between 09:00 today and 02:00 today.

The solution is if you intend to change the end time so that sometimes it’s today and sometimes it’s tomorrow, your template will need to check if the end time value is greater or less than the start time and adjust the timedelta value accordingly.

Copy-paste the following into the Template Editor and test it.

{% set start = today_at(states('input_datetime.living_thermostat_time_day')) %}
{% set end = today_at(states('input_datetime.living_thermostat_time_night')) %}
{% set day = states('input_number.living_thermostat_temp_day') %}
{% set night = states('input_number.living_thermostat_temp_night') %}
{% set offset = 1 if start > end else 0 %}
{{ day if start <= now() <= end + timedelta(days=offset) else night }}

hi, is the 01:00 AM or PM ?

maybe try 13:00 for PM

01:00 is the value of input_datetime.living_thermostat_time_night (therefore 1:00 AM).

It’s mentioned here:

Thank you for your response.

I had already found this myself. But this doesn’t work either. Because when it is 0:00 am I already get the 18° because the start time is already smaller than the end time, but I actually want this from 01:00 am. I really can’t solve this and I think this should be possible, right?

I suggest you consider solving the problem a completely different way. Instead of attempting to create a template that checks if the current time is between a starting and ending time, simply create an automation with two Time Triggers.

alias: example 
triggers:
  - id: 'day'
    trigger: time
    at: input_datetime.living_thermostat_time_day
  - id: 'night'
    trigger: time
    at: input_datetime.living_thermostat_time_night
conditions: []
actions:
  - action: climate.set_temperature 
    target:
      entity_id: climate.living_thermostat
    data:
      temperature: >
        {{ states('input_number.living_thermostat_temp_' ~ id) }}

Thanks for your response.

Yes I have tried this already, and it works but the problem is that ariston.net often goes offline and when it comes back online it still doesn’t work. Apparently this is an almost unsolvable problem.

The template displayed in your first post is used in an automation?

Please post the entire automation (as properly formatted YAML).

Below is my test automation where I think this piece comes from:

alias: ATest Verwarmingsknop
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.keuken_temperatuur_gevraagd
      - sensor.living_temperatuur_gevraagd
      - sensor.bureau_temperatuur_gevraagd
      - sensor.badkamer_mb_temperatuur_gevraagd
      - sensor.slaapkamer_mb_temperatuur_gevraagd
      - sensor.badkamer_stijn_temperatuur_gevraagd
    id: Overheat
    enabled: true
  - trigger: state
    entity_id:
      - climate.thuis
      - input_number.living_thermostaat_temp_dag
      - input_number.living_thermostaat_temp_nacht
    enabled: true
  - trigger: time
    at:
      - input_datetime.living_thermostaat_tijd_dag
      - input_datetime.living_thermostaat_tijd_nacht
conditions: []
actions:
  - condition: template
    value_template: |
      {{not is_state_attr('climate.thuis','temperature',Temp) and
         not is_state('climate.thuis','unavailable')}}
  - variables:
      TempMoment: >
        {% if today_at(states('input_datetime.living_thermostaat_tijd_nacht')) <
        now() >  today_at(states('input_datetime.living_thermostaat_tijd_dag'))
        %}
            {{states('input_number.living_thermostaat_temp_nacht')}}
        {% else %}
            {{states('input_number.living_thermostaat_temp_dag')}}
        {% endif %}

        {% if is_state('sensor.dagnacht','sNachts') %}
            {% if now().hour <
                    states('input_datetime.living_thermostaat_tijd_nacht').split(':')[0]|int %}
                {{states('input_number.living_thermostaat_temp_dag')}}
            {% else %}
                {{states('input_number.living_thermostaat_temp_nacht')}}
            {% endif %}
        {% elif is_state('sensor.dagnacht','Overdag') %}
            {% if now().hour <
                        states('input_datetime.living_thermostaat_tijd_dag').split(':')[0]|int %}
                {{states('input_number.living_thermostaat_temp_nacht')}}
            {% else %}
                {{states('input_number.living_thermostaat_temp_dag')}}
            {% endif %}
        {% endif %}
      Overheat: |
        {% if trigger.id == 'Overheat' %}
            {% if trigger.from_state.state|float <
                                    state_attr('climate.thuis', 'current_temperature')|float
                                    and states('sensor.max_temp_gevraagd')|float > state_attr('climate.thuis', 'temperature')|float and
                      state_attr('climate.thuis', 'current_temperature')|float > state_attr('climate.thuis', 'temperature')|float  %}
                      {% if state_attr('climate.thuis', 'current_temperature')|float + 2 < states('input_number.living_thermostaat_temp_dag')|float %}
                            {{states('input_number.living_thermostaat_temp_dag')}}
                      {% else %}
                            {{state_attr('climate.thuis', 'current_temperature')|float + 2}}
                      {% endif %}
             {% else %}
                {{TempMoment}} 
             {% endif %}
          {% else %}
              {{TempMoment}} 
          {% endif %}
      Temp: |
        {% if Overheat > TempMoment %}
           {{Overheat}}
        {% else %}
           {{TempMoment}}
        {% endif %}
  - repeat:
      sequence:
        - action: climate.set_temperature
          metadata: {}
          data:
            temperature: |
              {{Temp}}
          target:
            entity_id: climate.thuis
          continue_on_error: true
          enabled: false
        - delay:
            seconds: 60
          enabled: false
      until:
        - condition: template
          value_template: |
            {{is_state_attr('climate.thuis','temperature', Temp)}}
  - delay:
      seconds: 2
    enabled: true
  - data:
      title: >-
        {{now().strftime('%H:%M:%S')}} {{this.attributes.friendly_name|upper }}
        DOOR {{trigger.entity_id}}
      message: >
        Thermostaat Living is terug online en is op
        {{states('sensor.living_thermostaat')}} C° gezet!
    enabled: false
    action: notify.bart_ios
trace:
  stored_traces: 100
mode: parallel
max: 60

I’ve tried so many things that I don’t really remember where I made the first one.

The intention is that when Ariston.net comes back online it will be checked whether the thermostat should be set to 22 or 18 degrees. and then it will be checked whether a higher temperature is requested in the house. If so, the thermostat of the heating boiler should be set higher than 22 (if the temperature in Living is higher than 22 otherwise not). It is very difficult to explain what the intention is in the end. My first step however is the temperature in teseellen at 22° at 9:00 AM and at 18° 01:00 AM, if I can manage that I can continue I think. But this is apparently much more difficult than I expected.

Have you tried using an if-then with a Time Condition?

  - if:
      - condition: time
        after: input_datetime.living_thermostat_time_day
        before: input_datetime.living_thermostat_time_night
    then:
      - variables:
          TempMoment: "{{ states('input_number.living_thermostat_temp_day') }}"
    else:
      - variables:
          TempMoment: "{{ states('input_number.living_thermostat_temp_night') }}"

Thank you very much for helping me a little further. I am quite new but I try a lot of things to learn a lot. I did not even know this code. ok I will try this immediately. :+1:

At the moment this code works. I
have to test it after 0:00 or 01:00 AM to see if it works then. Anyway, thank you for teaching me this piece again.

I tested your solution tonight at 0:00 and 01:00 and it worked. So my first step in my automation works now and I can continue searching. Thank you very much for helping me out!!

1 Like