Time based climate automation (weekdays/weekend)

Hi,
I don´t know if I could explain it very well - so if something isn´t clear please ask.

I´m using HA a while and the automations are growing and growing. And I ask me: Isn´t it possible to reduce the number of automations (and the complexity of the automations) with some magic commands? Let me explain: I am using a thermostate which I automate to change the temperature several times a day - depending on if the present day is a workday or on a weekend. So at my point (and with my skills) I´ve got two options:

  1. One automation for one time of changing the thermostate.
  2. One automation with very complex if/then (translated :)) constructions

But isn´t it possible to declare my time table in something like a variable and creating one automation with a relative simple syntax?

Hope you could understand what I like to say. I´ve tried different blueprints but not ones fit to my needs :frowning: and I´m wondering: I couldn´t be the first one with these wishes.

best wishes,
M.

If I understand you correctly, you wish to run the same automation at different times with different actions?

You can use choose and have conditions set each choose option.

alias: Multiple Times
description: ''
mode: single
trigger:
  - platform: time
    at: '06:00:00'
  - platform: time
    at: '07:00:00'
condition: []
action:
  - choose:
      - conditions:
          - condition: time
            after: '06:00:00'
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
        sequence:
          - service: switch.turn_on
            target:
              entity_id:
                - switch.owner_bath_tub_light
                - switch.owner_bedroom_fan_light
      - conditions:
          - condition: time
            after: '07:00:00'
            weekday:
              - sat
              - sun
        sequence:
          - service: switch.turn_on
            target:
              entity_id:
                - switch.owner_bedroom_fan_light
                - switch.owner_bath_sconces
    default: []

you can add evening times as well.

1 Like

I’m unaware of any “magic”, just via the traditional way of reading and learning.

Post the automations you want to consolidate.

Well, thats what I mean with number 2 (very complex if/then constructions) but you are right - these would reduce the number of automations. But will it be faster? What is the best for automations: Many but simple automations or fewer automations with complexer commands?

I don’t think this will affect speed very much - maybe a millisecond or less. It is more not having to manage so many automations. If you are comfortable with managing more automations then please continue on that route. If you start losing track of automations then you should choose the combined mode. This is really personal preference. As an engineer, I appreciate the elegance of some of the combined automations. Getting things to work is the most important and if you need brute force and stupidity to get it done then I am all for that - have and still use it for some automations. I can clean up after the automation works and meets the wife approval factor.

Hope this helps.

@AllHailJ I´m an engineer too and I totally agree with your approach. But I´m also looking for something smarter solution. I´ve read a great blog post about smarter automation and I´m fixed on :). And I found Template integration which will allows you to reduce the complexity of an automation. But, and thats the annoying point, I don´t get it and I hope I´m not the only one who is seeking the solution for that climate control automation.

I tried to control my heating with automations as well and quickly gave up. Automations seem to be a bit bloaty for this task.
Currently I am using the appdaemon addon with schedy and it works nicely.

https://hass-apps.readthedocs.io/en/stable/apps/schedy/

Frankly, that doesn’t sound complicated. What have you created so far to implement those requirements?

Well: At this point I tried out so many solutions: schedy, different blueprints but I started with my own automations:

alias: Kueche | Heizung wochentags
description: ''
trigger:
  - platform: time
    at: '06:00'
  - platform: time
    at: '08:00'
  - platform: time
    at: '15:00'
  - platform: time
    at: '20:00'
  - platform: time
    at: '22:00'
  - platform: state
    to: 'off'
    for: '00:01:00'
    entity_id: binary_sensor.tuer_balkon_onoff
condition: []
action:
  - choose:
      - conditions:
          - condition: time
            after: '06:00'
            before: '08:00'
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
        sequence:
          - service: climate.set_temperature
            data:
              hvac_mode: heat
              temperature: 24
            target:
              device_id: 5c201da722bc2c509ba1a9c0753b204e
      - conditions:
          - condition: time
            after: '08:00'
            before: '15:00'
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
        sequence:
          - service: climate.set_temperature
            target:
              device_id: 5c201da722bc2c509ba1a9c0753b204e
            data:
              temperature: 20
              hvac_mode: heat
      - conditions:
          - condition: time
            after: '15:00'
            before: '20:00'
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
        sequence:
          - service: climate.set_temperature
            data:
              hvac_mode: heat
              temperature: 22
            target:
              device_id: 5c201da722bc2c509ba1a9c0753b204e
      - conditions:
          - condition: time
            after: '20:00'
            before: '22:00'
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
        sequence:
          - service: climate.set_temperature
            data:
              hvac_mode: heat
              temperature: 20
            target:
              device_id: 5c201da722bc2c509ba1a9c0753b204e
      - conditions:
          - condition: time
            after: '22:00'
            before: '06:00'
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
        sequence:
          - service: climate.set_temperature
            data:
              hvac_mode: heat
              temperature: 17
            target:
              device_id: 5c201da722bc2c509ba1a9c0753b204e
    default: []
mode: single
alias: Kueche | Heizung wochenends
description: ''
trigger:
  - platform: time
    at: '08:00'
  - platform: time
    at: '21:00'
  - platform: time
    at: '22:00'
  - platform: state
    to: 'off'
    for: '00:01:00'
    entity_id: binary_sensor.tuer_balkon_onoff
condition: []
action:
  - choose:
      - conditions:
          - condition: time
            after: '08:00'
            before: '21:00'
            weekday:
              - sat
              - sun
        sequence:
          - service: climate.set_temperature
            data:
              hvac_mode: heat
              temperature: 24
            target:
              device_id: 5c201da722bc2c509ba1a9c0753b204e
      - conditions:
          - condition: time
            after: '21:00'
            before: '22:00'
            weekday:
              - sat
              - sun
        sequence:
          - service: climate.set_temperature
            data:
              hvac_mode: heat
              temperature: 20
            target:
              device_id: 5c201da722bc2c509ba1a9c0753b204e
      - conditions:
          - condition: time
            after: '22:00'
            before: '08:00'
            weekday:
              - sat
              - sun
        sequence:
          - service: climate.set_temperature
            data:
              hvac_mode: heat
              temperature: 17
            target:
              device_id: 5c201da722bc2c509ba1a9c0753b204e
    default:
      - service: climate.set_temperature
        target:
          device_id: 5c201da722bc2c509ba1a9c0753b204e
        data:
          hvac_mode: heat
mode: single
alias: Balkon | Tuer AUF
description: ''
trigger:
  - platform: state
    to: 'on'
    for: '00:02:00'
    entity_id: binary_sensor.tuer_balkon_onoff
condition: []
action:
  - device_id: 5c201da722bc2c509ba1a9c0753b204e
    domain: climate
    entity_id: climate.thermostat_kueche
    type: set_hvac_mode
    hvac_mode: 'off'
  - service: automation.turn_off
    data: {}
    target:
      entity_id:
        - automation.kueche_heizung_wochenends
        - automation.kueche_heizung_wochentags
mode: single

But I´m not happy with that: Too many redundant configurations and to complex to understand it with only one view. Every condition I have to add makes it more and more complex.

I´m searching for a simple automation “set temperature with settings depend on the time of day” with definition of timetables with responding temperatures. You know what I mean?

Of course: Shedy is nice but not easy to manipulate (after configuration) and its a separate configuration on a different point. I like all settings on one point.

The following is a consolidated version of all three automations. I think I understood the purpose of the binary_sensor but if I didn’t then it will require adjustments.

alias: example
description: ''
trigger:
  - id: 'weekday'
    platform: time
    at:
      - '06:00'
      - '08:00'
      - '15:00'
      - '20:00'
      - '22:00'
  - id: 'weekend'
    platform: time
    at:
      - '08:00'
      - '21:00'
      - '22:00'
  - id: 'override'
    platform: state
    entity_id: binary_sensor.tuer_balkon_onoff
    to: 'off'
    for: '00:01:00'
  - id: 'disable'
    platform: state
    entity_id: binary_sensor.tuer_balkon_onoff
    to: 'on'
    for: '00:02:00'
condition: []
action:
  - choose:
      - conditions:
          - "{{ trigger.id == ['weekday', 'override'] }}"
          - "{{ 1 <= now().isoweekday() <= 5 }}"
          - "{{ is_state('binary_sensor.tuer_balkon_onoff', 'off') }}"
        sequence:
          - variables:
              schedule:
                6: 17
                8: 24
                15: 20
                20: 22
                22: 20
                24: 17
          - service: climate.set_temperature
            target:
              entity_id: climate.thermostat_kueche
            data:
              hvac_mode: 'heat'
              temperature: "{{ schedule.get(schedule.keys() | select('>', now().hour) | first, 21) }}"

      - conditions:
          - "{{ trigger.id in ['weekend', 'override'] }}"
          - "{{ now().isoweekday() in [6,7]  }}"
          - "{{ is_state('binary_sensor.tuer_balkon_onoff', 'off') }}"
        sequence:
          - variables:
              schedule:
                8: 17
                21: 24
                22: 20
                24: 17
          - service: climate.set_temperature
            target:
              entity_id: climate.thermostat_kueche
            data:
              hvac_mode: 'heat'
              temperature: "{{ schedule.get(schedule.keys() | select('>', now().hour) | first, 21) }}"

      - conditions:
          - "{{ trigger.id == 'disable' }}"
        sequence:
          - service: climate.turn_off
            target:
              entity_id: climate.thermostat_kueche
    default: []
mode: single

It has four triggers:

  1. Weekday schedule
  2. Weekend schedule
  3. Override when the binary_sensor is off for 1 minute
  4. Disable when the binary_sensor is on for 2 minutes

The choose has three choices where the first two corresponds to one of the first three triggers and the last choice is for the fourth trigger.


EDIT

I overlooked to mention that because this is a single automation, it cannot do what your third automation does and that’s to turn off the first two automations (because it would be turning itself off in this case). What it does instead is to have the weekday/weekend triggers check if the binary_sensor is off before setting the thermostat’s target temperature. If it’s on then the scheduled operation isn’t performed.

@123 suppose you wanted to use input_numbers for the temperature, Would the code here

          - variables:
              schedule:
                6: 17
                8: 24
                15: 20
                20: 22
                22: 20
                24: 17

then be changed to:

          - variables:
              schedule:
                6: {{ input_numbers.temp1 }}
                8: {{ input_numbers.temp2 }}
                15: {{ input_numbers.temp3 }}
                20: {{ input_numbers.temp4 }}
                22: {{ input_numbers.temp5 }}
                24: {{ input_numbers.temp6 }}

Try it; be sure to wrap the template in quotes and remove the last s in input_numbers.

I just noticed an edge case as I was testing. If the times are the same in weekday and weekend it will always take the first. So for 08:00 and 22:00 it will take weekday and not run.

1 Like

Good point; I overlooked that important detail. It effectively prevents consolidating the weekday and weekend schedules into one automation if both have common hours. EDIT I was too hasty when I wrote that; I now believe it can be done. I’ll post it tomorrow.

Dual version

It’s fairly easy to split the consolidated version into two separate automations, each handling its own schedule. The disabling logic can go in either one; I put it in the first automation.

alias: weekday schedule
description: ''
trigger:
  - id: 'weekday'
    platform: time
    at:
      - '06:00'
      - '08:00'
      - '15:00'
      - '20:00'
      - '22:00'
  - id: 'override'
    platform: state
    entity_id: binary_sensor.tuer_balkon_onoff
    to: 'off'
    for: '00:01:00'
  - id: 'disable'
    platform: state
    entity_id: binary_sensor.tuer_balkon_onoff
    to: 'on'
    for: '00:02:00'
condition: []
action:
  - choose:
      - conditions:
          - "{{ trigger.id == ['weekday', 'override'] }}"
          - "{{ 1 <= now().isoweekday() <= 5 }}"
          - "{{ is_state('binary_sensor.tuer_balkon_onoff', 'off') }}"
        sequence:
          - variables:
              schedule:
                6: 17
                8: 24
                15: 20
                20: 22
                22: 20
                24: 17
          - service: climate.set_temperature
            target:
              entity_id: climate.thermostat_kueche
            data:
              hvac_mode: 'heat'
              temperature: "{{ schedule.get(schedule.keys() | select('>', now().hour) | first, 21) }}"
      - conditions: "{{ trigger.id == 'disable' }}"
        sequence:
          - service: climate.turn_off
            target:
              entity_id: climate.thermostat_kueche
    default: []
mode: single
alias: weekend schedule
description: ''
trigger:
  - id: 'weekend'
    platform: time
    at:
      - '08:00'
      - '21:00'
      - '22:00'
  - id: 'override'
    platform: state
    entity_id: binary_sensor.tuer_balkon_onoff
    to: 'off'
    for: '00:01:00'
condition: []
action:
  - choose:
      - conditions:
          - "{{ now().isoweekday() in [6,7]  }}"
          - "{{ is_state('binary_sensor.tuer_balkon_onoff', 'off') }}"
        sequence:
          - variables:
              schedule:
                8: 17
                21: 24
                22: 20
                24: 17
          - service: climate.set_temperature
            target:
              entity_id: climate.thermostat_kueche
            data:
              hvac_mode: 'heat'
              temperature: "{{ schedule.get(schedule.keys() | select('>', now().hour) | first, 21) }}"
    default: []
mode: single

As promised, here’s the revised version. It’s more compact than the previous version I posted and can handle a scheduled hour that’s common to both the weekday and weekend schedules.

alias: example
description: ''
trigger:
  - id: 'schedule'
    platform: time
    at:
      - '06:00'
      - '08:00'
      - '15:00'
      - '20:00'
      - '21:00'
      - '22:00'
  - id: 'override'
    platform: state
    entity_id: binary_sensor.tuer_balkon_onoff
    to: 'off'
    for: '00:01:00'
  - id: 'disable'
    platform: state
    entity_id: binary_sensor.tuer_balkon_onoff
    to: 'on'
    for: '00:02:00'
condition: []
action:
  - choose:
      - conditions:
          - "{{ trigger.id != 'disable' }}"
          - "{{ is_state('binary_sensor.tuer_balkon_onoff', 'off') }}"
        sequence:
          - variables:
              weekday:
                6: 17
                8: 24
                15: 20
                20: 22
                22: 20
                24: 17
              weekend:
                8: 17
                21: 24
                22: 20
                24: 17
              schedule: "{{ weekday if 1 <= now().isoweekday() <= 5 else weekend }}"
          - service: climate.set_temperature
            target:
              entity_id: climate.thermostat_kueche
            data:
              hvac_mode: 'heat'
              temperature: "{{ schedule.get(schedule.keys() | select('>', now().hour) | first, 21) }}"
      - conditions:
          - "{{ trigger.id == 'disable' }}"
        sequence:
          - service: climate.turn_off
            target:
              entity_id: climate.thermostat_kueche
    default: []
mode: single
1 Like

Thanks Taras - that looks good. And that’s what I mean with “some magic” - the “script” part of it. And I like the “input.number” part from @AllHailJ.

Two points:

  1. in the first condition

should be
- "{{ trigger.id != 'override' }}".

  1. I´ve got an error message
Error rendering data template: TypeError: '>' not supported between instances of 'str' and 'int'

and the only data template part is in
temperature: "{{ schedule.get(schedule.keys() | select('>', now().hour) | first, 21) }}"

You know where the problem is? And another question: What does this line mean?

And after all there are still some questions:

  1. What happens if the door is open and a schedule time is on time?
  2. What happens if the door will be closed (after one minute)? Which temperature will be choosen?
  3. What happens if it the time from 8 to 15 somebody changed the temp manually? When will the temp adjusted?

I can assure you that saying ‘abracadabra’ didn’t make the “script part” materialize for me.

That changes my understanding of the binary_sensor’s purpose. Can you explain what role this binary_sensor plays with regards to the schedule?

That error message implies the variables in the example I posted have been altered. In the example, the key-values pairs (in weekend and weekday) are both integers (the error message is indicating one of them is a string).

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).