How to find nearest time - value

Hey folks. Im woriking on a blueprint, which should allow to define a temperature profile for whole day.
One defines time to be triggered and the temperature to be set at the triggered time.
This part seems to work.

So, let asume, I have a flag, to disable or enable the temperature profile (for example in dependency on occupation of the room). So, if the flag changes, I would like to set the last temperature which would be set at the last closest time, if the profile would be active.

The idea is to compare the current time with all stored time-values and set temperature of the last closest time. Especially to the midnight I don’t get the idea, how to handle this.

Here ist the whole blueprint

blueprint:
  name: Set thermostat temperature at specific time
  description: Sets a climate entity to a specifc temperature at a set time everyday.
  domain: automation
  input:
    climate_entity:
      name: Thermostat
      description: Climate entity which will get the temperature update
      selector:
        entity:
          domain: climate
    active_flag:
      name: Activity flag
      description: Input boolean flag when the automation is active
      selector:
        entity:
          domain: input_boolean
    active_when:
      name: Active when flag is equal to
      description: Selection if automation is active at ON or OFF value of the flag
      selector:
        select:
          options:
            - 'on'
            - 'off'
    set_last_temperature_flag:
      name: Set last temperature
      description: Set last temperature
      selector:
        entity:
          domain: input_boolean
    time_to_set_1:
      name: Time_1
      default:
      description: When should the temperature be set
      selector:
        time: {}
    temp_to_set_1:
      name: Temperature_1
      description: Temperature to set on defined time
      default: 19.5
      selector:
        number:
          min: 10.0
          max: 30.0
          unit_of_measurement: degrees
          step: 0.5
          mode: slider
    time_to_set_2:
      name: Time_2
      description: When should the temperature be set
      default: 
      selector:
        time: {}
    temp_to_set_2:
      name: Temperature_2
      description: Temperature to set on defined time
      default: 19.5
      selector:
        number:
          min: 10.0
          max: 30.0
          unit_of_measurement: degrees
          step: 0.5
          mode: slider
    time_to_set_3:
      name: Time_3
      description: When should the temperature be set
      default:
      selector:
        time: {}
    temp_to_set_3:
      name: Temperature_3
      description: Temperature to set on defined time
      default: 19.5
      selector:
        number:
          min: 10.0
          max: 30.0
          unit_of_measurement: degrees
          step: 0.5
          mode: slider
    time_to_set_4:
      name: Time_4
      default:
      description: When should the temperature be set
      selector:
        time: {}
    temp_to_set_4:
      name: Temperature_4
      description: Temperature to set on defined time
      default: 19.5
      selector:
        number:
          min: 10.0
          max: 30.0
          unit_of_measurement: degrees
          step: 0.5
          mode: slider
    time_to_set_5:
      name: Time_5
      description: When should the temperature be set
      default: 
      selector:
        time: {}
    temp_to_set_5:
      name: Temperature_5
      description: Temperature to set on defined time
      default: 19.5
      selector:
        number:
          min: 10.0
          max: 30.0
          unit_of_measurement: degrees
          step: 0.5
          mode: slider
    time_to_set_6:
      name: Time_6
      description: When should the temperature be set
      default:
      selector:
        time: {}
    temp_to_set_6:
      name: Temperature_6
      description: Temperature to set on defined time
      default: 19.5
      selector:
        number:
          min: 10.0
          max: 30.0
          unit_of_measurement: degrees
          step: 0.5
          mode: slider
    time_to_set_7:
      name: Time_7
      default:
      description: When should the temperature be set
      selector:
        time: {}
    temp_to_set_7:
      name: Temperature_7
      description: Temperature to set on defined time
      default: 19.5
      selector:
        number:
          min: 10.0
          max: 30.0
          unit_of_measurement: degrees
          step: 0.5
          mode: slider
    time_to_set_8:
      name: Time_8
      description: When should the temperature be set
      default: 
      selector:
        time: {}
    temp_to_set_8:
      name: Temperature_8
      description: Temperature to set on defined time
      default: 19.5
      selector:
        number:
          min: 10.0
          max: 30.0
          unit_of_measurement: degrees
          step: 0.5
          mode: slider
    time_to_set_9:
      name: Time_9
      description: When should the temperature be set
      default:
      selector:
        time: {}
    temp_to_set_9:
      name: Temperature_9
      description: Temperature to set on defined time
      default: 19.5
      selector:
        number:
          min: 10.0
          max: 30.0
          unit_of_measurement: degrees
          step: 0.5
          mode: slider
    time_to_set_10:
      name: Time_10
      description: When should the temperature be set
      default:
      selector:
        time: {}
    temp_to_set_10:
      name: Temperature_10
      description: Temperature to set on defined time
      default: 19.5
      selector:
        number:
          min: 10.0
          max: 30.0
          unit_of_measurement: degrees
          step: 0.5
          mode: slider
trigger:
- platform: state
  entity_id: !input set_last_temperature_flag
  from: "off"
  to: "on"
  id: set_last_temperature
- platform: time
  at: !input 'time_to_set_1'
  id: time_1
- platform: time
  at: !input 'time_to_set_2'
  id: time_2
- platform: time
  at: !input 'time_to_set_3'
  id: time_3
- platform: time
  at: !input 'time_to_set_4'
  id: time_4
- platform: time
  at: !input 'time_to_set_5'
  id: time_5
- platform: time
  at: !input 'time_to_set_6'
  id: time_6
- platform: time
  at: !input 'time_to_set_7'
  id: time_7
- platform: time
  at: !input 'time_to_set_8'
  id: time_8
- platform: time
  at: !input 'time_to_set_9'
  id: time_9
- platform: time
  at: !input 'time_to_set_10'
  id: time_10
  
condition:
  - condition: state
    entity_id: !input active_flag
    state: !input 'active_when'

action:
- choose:
    - conditions:
        - condition: trigger
          id: set_last_temperature
      sequence:
        - service: climate.set_temperature
          entity_id: !input 'climate_entity'
          data:
            # find current temperature to set: TODO
            temperature: 15.5
        
    - conditions:
        - condition: trigger
          id: time_1
      sequence:
        - service: climate.set_temperature
          entity_id: !input 'climate_entity'
          data:
            temperature: !input 'temp_to_set_1'
    - conditions:
        - condition: trigger
          id: time_2
      sequence:
        - service: climate.set_temperature
          entity_id: !input 'climate_entity'
          data:
            temperature: !input 'temp_to_set_2'
    - conditions:
        - condition: trigger
          id: time_3
      sequence:
        - service: climate.set_temperature
          entity_id: !input 'climate_entity'
          data:
            temperature: !input 'temp_to_set_3'
    - conditions:
        - condition: trigger
          id: time_4
      sequence:
        - service: climate.set_temperature
          entity_id: !input 'climate_entity'
          data:
            temperature: !input 'temp_to_set_4'
    - conditions:
        - condition: trigger
          id: time_5
      sequence:
        - service: climate.set_temperature
          entity_id: !input 'climate_entity'
          data:
            temperature: !input 'temp_to_set_5'
    - conditions:
        - condition: trigger
          id: time_6
      sequence:
        - service: climate.set_temperature
          entity_id: !input 'climate_entity'
          data:
            temperature: !input 'temp_to_set_6'
    - conditions:
        - condition: trigger
          id: time_7
      sequence:
        - service: climate.set_temperature
          entity_id: !input 'climate_entity'
          data:
            temperature: !input 'temp_to_set_7'
    - conditions:
        - condition: trigger
          id: time_8
      sequence:
        - service: climate.set_temperature
          entity_id: !input 'climate_entity'
          data:
            temperature: !input 'temp_to_set_8'
    - conditions:
        - condition: trigger
          id: time_9
      sequence:
        - service: climate.set_temperature
          entity_id: !input 'climate_entity'
          data:
            temperature: !input 'temp_to_set_9'
    - conditions:
        - condition: trigger
          id: time_10
      sequence:
        - service: climate.set_temperature
          entity_id: !input 'climate_entity'
          data:
            temperature: !input 'temp_to_set_10'
  default: []

mode: queued

and the part I have problems with, is this one:

    - conditions:
        - condition: trigger
          id: set_last_temperature
      sequence:
        - service: climate.set_temperature
          entity_id: !input 'climate_entity'
          data:
            # find current temperature to set: TODO
            temperature: 15.5

For example:
If I have a non active profile like this

and “set last temperature” - flag is triggered at 10 pm, I would like to set 21°C, as it would be set at 7 pm.

Any ideas, hints or may be similar examples ?

Cheers.

the first draft with hard coded times looks like this.

{% set time_to_set_1 = strptime('19:00:01', "%H:%M:%S") %}
{% set time_to_set_2 = strptime('22:00:34', "%H:%M:%S") %}
{% set time_to_set_3 = strptime('23:10:00', "%H:%M:%S") %}
{% set time_to_set_4 = strptime('23:50:30', "%H:%M:%S") %}
{% set time_to_set_5 = strptime('00:00:00', "%H:%M:%S") %}
{% set temp_to_set_1 = 15 %}
{% set temp_to_set_2 = 16 %}
{% set temp_to_set_3 = 17 %}
{% set temp_to_set_4 = 18 %}
{% set temp_to_set_5 = 19 %}
{% set all_values = [[time_to_set_4, temp_to_set_4],[time_to_set_3, temp_to_set_3],[time_to_set_2, temp_to_set_2],[time_to_set_1, temp_to_set_1]] %}
{% set result = namespace(time_curr = strptime( (((as_timestamp(now()) | int ) | timestamp_custom("%H:%M:%S")) | string), "%H:%M:%S"), temp_last = all_values[all_values|length()-1][1]) %}
{# loop from last time of the day, till first set time #}
{% for curr_values  in all_values -%}
  {% if  curr_values[0] < result.time_curr -%}
    {# we found previous closest time to now() #}
    {# store the temperature #}
    {% set result.temp_last = curr_values[1] -%}
    {# break the loop #}
    {% set result.time_curr = strptime('00:00:00', "%H:%M:%S") -%}
  {% endif %}
{% endfor %}
{{result.temp_last}}

Next, I have to think how to deal with “not set values / empty” values

I’ve managed it here