Time based climate automation (weekdays/weekend)

Ah OK. Let me say it this way: It’s hard too “read” the script part for me and creating such code for me is doing magic :).

That’s easy: It’s a sensor that detect “door is open” and if the door is longer open than 2 minutes the thermostate should turned off and under no circumstances it should turned on. If the door is closed (“off”) for one minute the thermostate should turned on and the regarding temperature for the time slot should be set.

Hmm, I just copied your code. But I will check. Thanks.

That’s how I assumed it should work so I advise that you do not make the change you suggested.

The only change I would make is to rename override to enable to make it clearer that the scheduled operation is being re-enabled (after having been disabled by the open door).

Oh, I think I found it: I created an automation with gui and pasted the code in the integrated yaml editor but after saving the code will be transformed. Now I created a fake automation and paste the code directly into the yaml and it seams to work.

You´re right - I´ve overseen the “!=” as “if not”.

The Automation Editor has a habit of reformatting YAML according to its rules. Sometimes this reformatting changes the meaning of the data and results in a broken automation.

I found a thread that talks about tuples

{% set schedule = {4:20, 6:21, 8:20, 10:19 12:20 22:17, 24:17} %}
{% set key = schedule.keys() | list %}
{% set value = schedule.values() | list %}
{% set hour = now().hour %}
{{ hour }}
{{ value[key.index(hour)] }}
{{ schedule[hour] }}

You can change {{ value[key.index(hour)] }} to {{ value[key.index(8)] }} or {{schedule[8]}}and it will show in the developer the second half of the tuple 20.

Hope this helps with your understanding

@AllHailJ Is that an answer to me? I can’t do anything with that, sorry :).

Could you please say something to that. What does it mean?

“Take from the list of scheduled times the line which contains a number which is bigger than the current hour …” - if I read it right, but what comes after that?

It was for you. Use developer tools and you can cut and paste into templates and see how tuples work.

Ah - understood. Thanks!

I can get the array to be dynamic in the developers tools

{% set s_wk = 
   {state_attr('input_datetime.sp1_wk', 'hour'): states('input_number.t1_wk'), 
   state_attr('input_datetime.sp2_wk', 'hour'): states('input_number.t2_wk'), 
   state_attr('input_datetime.sp3_wk', 'hour'): states('input_number.t3_wk'),
   state_attr('input_datetime.sp4_wk', 'hour'): states('input_number.t4_wk'),
   state_attr('input_datetime.sp5_wk', 'hour'): states('input_number.t5_wk')} %}
{% set s_wknd =
   {state_attr('input_datetime.sp1_wknd', 'hour'): states('input_number.t1_wknd'),
   state_attr('input_datetime.sp2_wknd', 'hour'): states('input_number.t2_wknd'), 
   state_attr('input_datetime.sp3_wknd', 'hour'): states('input_number.t3_wknd'), 
   state_attr('input_datetime.sp4_wknd', 'hour'): states('input_number.t4_wknd'),
   state_attr('input_datetime.sp5_wknd', 'hour'): states('input_number.t5_wknd')} %}
{% set hour = now().hour %}
{{ hour }}
{{s_wk[19]}}
{{s_wknd[20]}}

And here is screen shot from developer tools

but I can’t get it to work in the automation.

Also when working on an ecobee thermostat it would not set the temperature it you also set the hvac_mode.

  alias: Main Floor Thermostat Control Heat Control
  description: 'Set the main floor thermostat for Heat'
  trigger:
    - id: 'schedule'
      platform: time
      at:
        - input_datetime.sp1_wk
        - input_datetime.sp2_wk
        - input_datetime.sp3_wk
        - input_datetime.sp4_wk
        - input_datetime.sp5_wk
        - input_datetime.sp1_wknd
        - input_datetime.sp2_wknd
        - input_datetime.sp3_wknd
        - input_datetime.sp4_wknd
        - input_datetime.sp5_wknd
    - id: 'override'
      platform: state
      entity_id: input_boolean.thermostat_main
      to: 'on'
      for: '00:00:30'
    - id: 'disable'
      platform: state
      entity_id: input_boolean.thermostat_main
      to: 'off'
      for: '00:00:30'
  condition: []
  action:
    - choose:
        - conditions:
            - "{{ trigger.id != 'disable' }}"
            - "{{ is_state('input_boolean.thermostat_main', 'on') }}"
          sequence:
            - variables:
                weekday: 
                  1: 61
                  2: 61
                  3: 61
                  4: 61
                  5: 61
                  6: 61
                  7: 61
                  8: 68
                  9: 68
                  10: 68
                  11: 68
                  12: 68
                  13: 68
                  14: 68
                  15: 68
                  16: 68
                  17: 50
                  18: 68
                  19: 68
                  20: 68
                  21: 61
                  22: 61
                  23: 61
                  24: 61                       
                weekend:
                  1: 61
                  2: 61
                  3: 61
                  4: 61
                  5: 61
                  6: 61
                  7: 61
                  8: 68
                  9: 68
                  10: 68
                  11: 68
                  12: 68
                  13: 68
                  14: 68
                  15: 68
                  16: 68
                  17: 50
                  18: 68
                  19: 68
                  20: 68
                  21: 61
                  22: 61
                  23: 61
                  24: 61   
                schedule: "{{ weekday if now().isoweekday() in [1,2,3,4,5] else weekend }}"
            - service: climate.set_temperature
              target:
                entity_id: climate.main_floor
              data:
                temperature: "{{ schedule[now().hour] }}"
        - conditions:
            - "{{ trigger.id == 'disable' }}"
          sequence:
            - service: climate.turn_off
              target:
                entity_id: climate.main_floor
              data:
                hvac_mode: 'off'
      default: []
  mode: single

I have searched and can find nothing that helps.

Also simplified the temperature as the documentation explained that it always took the first match so this limits it to one action per hour but that was limited anyways. You can have it anywhere in the hour to trigger. I was doing the 1-24 so I could test. I have tried

sequence:
            - variables:
                weekday: 
                  - value_template: >-
                  {% {state_attr('input_datetime.sp1_wk', 'hour'): states('input_number.t1_wk'), 
                      state_attr('input_datetime.sp2_wk', 'hour'): states('input_number.t2_wk'), 
                      state_attr('input_datetime.sp3_wk', 'hour'): states('input_number.t3_wk'),
                      state_attr('input_datetime.sp4_wk', 'hour'): states('input_number.t4_wk'),
                      state_attr('input_datetime.sp5_wk', 'hour'): states('input_number.t5_wk')} %}

and get this error

Error loading /config/configuration.yaml: while scanning for the next token found character ‘%’ that cannot start any token in “/config/automations/main_floor/MainFloorThermostatControl.yaml”, line 38, column 20

I have tried this:

          sequence:
            - variables:
                weekday: 
                  - value_template: >-
                  {{ {state_attr('input_datetime.sp1_wk', 'hour'): states('input_number.t1_wk'), 
                      state_attr('input_datetime.sp2_wk', 'hour'): states('input_number.t2_wk'), 
                      state_attr('input_datetime.sp3_wk', 'hour'): states('input_number.t3_wk'),
                      state_attr('input_datetime.sp4_wk', 'hour'): states('input_number.t4_wk'),
                      state_attr('input_datetime.sp5_wk', 'hour'): states('input_number.t5_wk')} }}

and I get this error:

Error loading /config/configuration.yaml: while scanning a simple key in “/config/automations/main_floor/MainFloorThermostatControl.yaml”, line 38, column 19 could not find expected ‘:’
in “/config/automations/main_floor/MainFloorThermostatControl.yaml”, line 39, column 23

I tried this:

          sequence:
            - variables:
                weekday: 
                  - value_template: >-
                  "{{ {state_attr('input_datetime.sp1_wk', 'hour'): states('input_number.t1_wk'), 
                      state_attr('input_datetime.sp2_wk', 'hour'): states('input_number.t2_wk'), 
                      state_attr('input_datetime.sp3_wk', 'hour'): states('input_number.t3_wk'),
                      state_attr('input_datetime.sp4_wk', 'hour'): states('input_number.t4_wk'),
                      state_attr('input_datetime.sp5_wk', 'hour'): states('input_number.t5_wk')} }}"

and got this error

Error loading /config/configuration.yaml: while scanning a simple key in “/config/automations/main_floor/MainFloorThermostatControl.yaml”, line 38, column 19 could not find expected ‘:’
in “/config/automations/main_floor/MainFloorThermostatControl.yaml”, line 42, column 101

And I have tried this:

          sequence:
            - variables:
                weekday: 
                  - value_template: >-
                  "{{ {state_attr('input_datetime.sp1_wk', 'hour') ~ ':' ~ states('input_number.t1_wk'), 
                      state_attr('input_datetime.sp2_wk', 'hour') ~ ':' ~ states('input_number.t2_wk'), 
                      state_attr('input_datetime.sp3_wk', 'hour') ~ ':' ~ states('input_number.t3_wk'),
                      state_attr('input_datetime.sp4_wk', 'hour') ~ ':' ~states('input_number.t4_wk'),
                      state_attr('input_datetime.sp5_wk', 'hour') ~ ':' ~ states('input_number.t5_wk')} }}"

And I get this error:

Error loading /config/configuration.yaml: while scanning a simple key in “/config/automations/main_floor/MainFloorThermostatControl.yaml”, line 38, column 19 could not find expected ‘:’
in “/config/automations/main_floor/MainFloorThermostatControl.yaml”, line 42, column 108

and lastly I tried this:

          sequence:
            - variables:
                weekday: 
                  - value_template: 
                    {{state_attr('input_datetime.sp1_wk', 'hour') }} : {{states('input_number.t1_wk')}}
                    {{state_attr('input_datetime.sp2_wk', 'hour') }} : {{states('input_number.t2_wk')}} 
                    {{state_attr('input_datetime.sp3_wk', 'hour') }} : {{states('input_number.t3_wk')}}
                    {{state_attr('input_datetime.sp4_wk', 'hour') }} : {{states('input_number.t4_wk')}}
                    {{state_attr('input_datetime.sp5_wk', 'hour') }} : {{states('input_number.t5_wk')}}

Error loading /config/configuration.yaml: while parsing a flow mapping in “/config/automations/main_floor/MainFloorThermostatControl.yaml”, line 38, column 22 expected ‘,’ or ‘}’, but got ‘’
in “/config/automations/main_floor/MainFloorThermostatControl.yaml”, line 38, column 65

and I tried it with quotes and a comma

          sequence:
            - variables:
                weekday: 
                  - value_template: 
                    "{{state_attr('input_datetime.sp1_wk', 'hour') }}" : "{{states('input_number.t1_wk')}}",
                    "{{state_attr('input_datetime.sp2_wk', 'hour') }}" : "{{states('input_number.t2_wk')}}",
                    "{{state_attr('input_datetime.sp3_wk', 'hour') }}" : "{{states('input_number.t3_wk')}}",
                    "{{state_attr('input_datetime.sp4_wk', 'hour') }}" : "{{states('input_number.t4_wk')}}",
                    "{{state_attr('input_datetime.sp5_wk', 'hour') }}" : "{{states('input_number.t5_wk')}}"

this passed the configuration validation

variables:
  weekday:
    - value_template: ''
      '{{state_attr(''input_datetime.sp1_wk'', ''hour'') }}': '{{states(''input_number.t1_wk'')}}'
      '{{state_attr(''input_datetime.sp2_wk'', ''hour'') }}': '{{states(''input_number.t2_wk'')}}'
      '{{state_attr(''input_datetime.sp3_wk'', ''hour'') }}': '{{states(''input_number.t3_wk'')}}'
      '{{state_attr(''input_datetime.sp4_wk'', ''hour'') }}': '{{states(''input_number.t4_wk'')}}'
      '{{state_attr(''input_datetime.sp5_wk'', ''hour'') }}': '{{states(''input_number.t5_wk'')}}'

but when executed got this:

Took the value_template out and same issue . The thermostat never set and when I do it with numbers it works.

Any guidance ???

Most of the error messages are related to a incorrect indentation. However, even if you fix the indentation, it will still fail because you are using a Jinja2template to generate YAML and that won’t work.

YAML is processed first and then Jinja2 is processed.

You will need to define the dictionaries in JSON format (not YAML) within the temperature option (in the climate.set_temperature service call).

I think I sorta have this. Here is the code

  alias: Main Floor Thermostat Control Heat Control
  description: 'Set the main floor thermostat for Heat'
  trigger:
    - id: 'schedule'
      platform: time
      at:
        - input_datetime.sp1_wk
        - input_datetime.sp2_wk
        - input_datetime.sp3_wk
        - input_datetime.sp4_wk
        - input_datetime.sp5_wk
        - input_datetime.sp1_wknd
        - input_datetime.sp2_wknd
        - input_datetime.sp3_wknd
        - input_datetime.sp4_wknd
        - input_datetime.sp5_wknd
    - id: 'override'
      platform: state
      entity_id: input_boolean.thermostat_main
      to: 'on'
      for: '00:00:30'
    - id: 'disable'
      platform: state
      entity_id: input_boolean.thermostat_main
      to: 'off'
      for: '00:00:30'
  condition: []
  action:
    - choose:
        - conditions:
            - "{{ trigger.id != 'disable' }}"
            - "{{ is_state('input_boolean.thermostat_main', 'on') }}"
          sequence:
            - variables:
                weekday: 
                  "{{state_attr('input_datetime.sp1_wk', 'hour') }}": "{{states('input_number.t1_wk')}}"
                  "{{state_attr('input_datetime.sp2_wk', 'hour') }}": "{{states('input_number.t2_wk')}}"
                  "{{state_attr('input_datetime.sp3_wk', 'hour') }}": "{{states('input_number.t3_wk')}}"
                  "{{state_attr('input_datetime.sp4_wk', 'hour') }}": "{{states('input_number.t4_wk')}}"
                  "{{state_attr('input_datetime.sp5_wk', 'hour') }}": "{{states('input_number.t5_wk')}}"
                weekend:
                  "{{state_attr('input_datetime.sp1_wknd', 'hour') }}": "{{states('input_number.t1_wknd')}}"
                  "{{state_attr('input_datetime.sp2_wknd', 'hour') }}": "{{states('input_number.t2_wknd')}}"
                  "{{state_attr('input_datetime.sp3_wkdn', 'hour') }}": "{{states('input_number.t3_wknd')}}"
                  "{{state_attr('input_datetime.sp4_wknd', 'hour') }}": "{{states('input_number.t4_wknd')}}"
                  "{{state_attr('input_datetime.sp5_wknd', 'hour') }}": "{{states('input_number.t5_wknd')}}"
                schedule: "{{ weekday if now().isoweekday() in [1,2,3,4,5] else weekend }}"
            - service: climate.set_temperature
              target:
                entity_id: climate.main_floor
              data:
                temperature: "{{ schedule[now().hour] }}"
        - conditions:
            - "{{ trigger.id == 'disable' }}"
          sequence:
            - service: climate.turn_off
              target:
                entity_id: climate.main_floor
              data:
                hvac_mode: 'off'
      default: []
  mode: single

The interesting part is that it has to execute once before it will execute correctly. I assume this is because it has to populate the automation with the current values. This obviously is not the desired solution. My error was having a space before the : and having the value_template.

@123 can you point me to some documentation on JSON dictionaries. Totally clueless on this and what I have found in searching is not related to home assistant.

@controlc This is my input_datetime and input_number declarations. I have then in separate files.

my input_datetime file:

sp1_wk:
  name: Time for Temp Setpoint 1 for week
  has_date: false
  has_time: true
sp2_wk:
  name: Time for Temp Setpoint 2 for week
  has_date: false
  has_time: true
sp3_wk:
  name: Time for Temp Setpoint 3 for week
  has_date: false
  has_time: true
sp4_wk:
  name: Time for Temp Setpoint 4 for week
  has_date: false
  has_time: true
sp5_wk:
  name: Time for Temp Setpoint 5 for week
  has_date: false
  has_time: true
sp1_wknd:
  name: Time for Temp Setpoint 1 for weekend
  has_date: false
  has_time: true
sp2_wknd:
  name: Time for Temp Setpoint 2 for weekend
  has_date: false
  has_time: true
sp3_wknd:
  name: Time for Temp Setpoint 3 for weekend
  has_date: false
  has_time: true
sp4_wknd:
  name: Time for Temp Setpoint 4 for weekend
  has_date: false
  has_time: true
sp5_wknd:
  name: Time for Temp Setpoint 5 for weekend
  has_date: false
  has_time: true

My input_number file

t1_wk:
  name: 1st Temperature Weekday 
  icon: mdi:thermostat
  min: 55
  max: 80
  unit_of_measurement: F
t2_wk:
  name: 2nd Temperature Weekday 
  icon: mdi:thermostat
  min: 55
  max: 80
  unit_of_measurement: F
t3_wk:
  name: 3rd Temperature Weekday 
  icon: mdi:thermostat
  min: 55
  max: 80
  unit_of_measurement: F
t4_wk:
  name: 4th Temperature Weekday 
  icon: mdi:thermostat
  min: 55
  max: 80
  unit_of_measurement: F
t5_wk:
  name: 5th Temperature Weekday 
  icon: mdi:thermostat
  min: 55
  max: 80
  unit_of_measurement: F  
t1_wknd:
  name: 1st Temperature Weekend 
  icon: mdi:thermostat
  min: 55
  max: 80
  unit_of_measurement: F
t2_wknd:
  name: 2nd Temperature Weekend 
  icon: mdi:thermostat
  min: 55
  max: 80
  unit_of_measurement: F
t3_wknd:
  name: 3rd Temperature Weekend 
  icon: mdi:thermostat
  min: 55
  max: 80
  unit_of_measurement: F
t4_wknd:
  name: 4th Temperature Weekend 
  icon: mdi:thermostat
  min: 55
  max: 80
  unit_of_measurement: F
t5_wknd:
  name: 5th Temperature Weekend 
  icon: mdi:thermostat
  min: 55
  max: 80
  unit_of_measurement: F 

Here is the lovelace configuration:

I get the impression my explanation didn’t quite register so I’ll let you wrestle with this for awhile until you have a “Doh! :man_facepalming:” moment. :slightly_smiling_face:

Yes it did. That is why I asked:

" @123 can you point me to some documentation on JSON dictionaries. Totally clueless on this and what I have found in searching is not related to home assistant."

what I meant by this is: JSON

Sorry if I didn’t make that clear.

A script variable called foo containing a dictionary with three key-values defined in YAML:

- variables:
    foo:
      apples: 23
      oranges: 5
      bananas: 16

A Jinja2 variable called foo containing a dictionary with three key-values defined in JSON:

{% set foo = {"apples": 23, "oranges": 5, "bananas": 16} %}

This actually makes it much simpler:

- id: '1Main Floor Thermostat Control Heating'
  alias: 1Main Floor Thermostat Control Heat Control
  description: 'Set the main floor thermostat for Heat'
  trigger:
    - id: 'schedule'
      platform: time
      at:
        - input_datetime.sp1_wk
        - input_datetime.sp2_wk
        - input_datetime.sp3_wk
        - input_datetime.sp4_wk
        - input_datetime.sp5_wk
        - input_datetime.sp1_wknd
        - input_datetime.sp2_wknd
        - input_datetime.sp3_wknd
        - input_datetime.sp4_wknd
        - input_datetime.sp5_wknd
    - id: 'override'
      platform: state
      entity_id: input_boolean.thermostat_main
      to: 'on'
      for: '00:00:30'
    - id: 'disable'
      platform: state
      entity_id: input_boolean.thermostat_main
      to: 'off'
      for: '00:00:30'
  condition: []
  action:
    - choose:
        - conditions:
            - "{{ trigger.id != 'disable' }}"
            - "{{ is_state('input_boolean.thermostat_main', 'on') }}"
          sequence:
            - service: climate.set_temperature
              target:
                entity_id: climate.main_floor
              data:
                temperature: >-
                  {% if now().isoweekday() in [1,2,3,4,5] %}
                    {% set weekday = {state_attr('input_datetime.sp1_wk', 'hour'): states('input_number.t1_wk'),
                                      state_attr('input_datetime.sp2_wk', 'hour'): states('input_number.t2_wk'),
                                      state_attr('input_datetime.sp3_wk', 'hour'): states('input_number.t3_wk'),
                                      state_attr('input_datetime.sp4_wk', 'hour'): states('input_number.t4_wk'),
                                      state_attr('input_datetime.sp5_wk', 'hour'): states('input_number.t5_wk')} %}
                    {{ weekday[now().hour] }}
                  {% else %}
                    {% set weekend = {state_attr('input_datetime.sp1_wknd', 'hour'): states('input_number.t1_wknd'),
                                      state_attr('input_datetime.sp2_wknd', 'hour'): states('input_number.t2_wknd'),
                                      state_attr('input_datetime.sp3_wknd', 'hour'): states('input_number.t3_wknd'),
                                      state_attr('input_datetime.sp4_wknd', 'hour'): states('input_number.t4_wknd'),
                                      state_attr('input_datetime.sp5_wknd', 'hour'): states('input_number.t5_wknd')} %}
                    {{ weekend[now().hour] }}
                  {% endif %}
        - conditions:
            - "{{ trigger.id == 'disable' }}"
          sequence:
            - service: climate.turn_off
              target:
                entity_id: climate.main_floor
              data:
                hvac_mode: 'off'
      default: []
  mode: single

Many thanks @123

I have tested it and it fires correctly.

I just got schooled in a much easier way to do this. @controlc hope you find this helpful.

  1. Create a generic thermostat if needed
  2. Go to hacs and load the schedule integration and card.
  3. Add the schedule integration to configuration → integration.
  4. Build your schedule to control your thermostat by adding card in Lovelace.

Easy peasy!

Schedule is awesome.

Arguably, not much easier.

Copy-paste the example I posted
versus
four steps, potentially five if HACS isn’t installed (which is a multi-step process in itself), and then build the schedule from scratch.

The end-result is a nice UI for adjusting the schedule but that wasn’t a requirement for this topic.

For what I read into @controlc desires (magic commands, something smarter) this fits. My only point is this allows you to adjust easily and without writing yaml or modifying templates. Not saying yours is not elegant - it is - but this allows a greater WAF to adjust settings and have control over the system.