Template to script or automation help/ heating schedule

The Time Trigger will trigger at whatever time specified by the Input Datetime even if you change its value (EDIT: However, a State Trigger is also needed to detect backdated times.) There’s simply no justification for employing a Time Pattern Trigger for your intended application.

Unnecessary; the Time Trigger takes into consideration any changes made to the Input Datetime’s value. EDIT Yes, it is needed to detect backdated times.

Well it matters if the time is in the past.

Like let’s say input_datetime.onwkday1 was set to 11:00:00 and the time is currently 10:30:00. And you realized you’re cold because its winter now and mornings are colder so you want to move the time up to 10:00:00. Well now you have an issue because that time is in the past. You missed that time trigger and now it won’t trigger at 11:00:00 anymore. Everything will be fine tomorrow but what about today?

That’s where the state trigger comes in. It fires because you changed one of the helpers and re-evaluates the schedule. This causes it to recognize that the hvac should be on right now and self-correct.

1 Like

You’re absolutely correct; I had overlooked to consider the need to backdate the scheduled time; a Time Trigger certainly won’t trigger if its time is set to the past. Your suggestion to employ a State Trigger is definitely needed to detect the change in the input_datetime’s value. Then the Time Conditions in the choose come into play to determine which action to perform (if any).

I’ll amend my previous post.

So I have changed the triggers to time at and state changes of the input times and is all working as expected without the time pattern… so thanks for this improvement.

I will now work on for loop logic to reduce the code size I will post when I get something up and running.

Two things that might be helpful:

  1. Splitting the time triggers into ‘on’ and ‘off’ groupings so you can leverage the trigger id in your Choose action
  2. Declaring trigger-based variables to derive the necessary time and weekday/weekend conditions.

The following only addresses actions based on the time triggers…

trigger:
  - platform: time
    id: on
    at:
    - input_datetime.onwkday1
    - input_datetime.onwkday2
    - input_datetime.onwkday3
    - input_datetime.onwkend1
    - input_datetime.onwkend2
    - input_datetime.onwkend3
  - platform: time
    id: off
    at:
    - input_datetime.offwkday1
    - input_datetime.offwkday2
    - input_datetime.offwkday3
    - input_datetime.offwkend1
    - input_datetime.offwkend2
    - input_datetime.offwkend3
  - platform: state
    entity_id:
    - input_datetime.onwkday1
    - input_datetime.onwkday2
    - input_datetime.onwkday3
    - input_datetime.offwkday1
    - input_datetime.offwkday2
    - input_datetime.offwkday3
    - input_datetime.onwkend1
    - input_datetime.onwkend2
    - input_datetime.onwkend3
    - input_datetime.offwkend1
    - input_datetime.offwkend2
    - input_datetime.offwkend3
    to:
  - platform: state
    entity_id: input_boolean.en_trv_masterbedroom
condition:
  - condition: state
    entity_id: input_boolean.boost_masterbedroom
    state: 'off'
    for:
      hours: 0
      minutes: 0
      seconds: 10
  - condition: state
    entity_id: input_boolean.en_trv_masterbedroom
    state: 'on'
action:
  - variables:
      dow_type: "{{ trigger.entity_id[-6:-1] }}"
      dow_match: "{{ (dow_type == 'wkend' and now().isoweekday() >= 6) or (dow_type == 'wkday' and now().isoweekday() <= 5 ) }}"
      time_index:  "{{ trigger.entity_id[-1:] }}"
  - choose:
    - conditions:
        - condition: trigger
          id: on
        - '{{ dow_match }}'
        - condition: time
          before: input_datetime.off{{dow_type}}{{time_index}}
      sequence:
        - service: climate.set_temperature
          data:
            hvac_mode: auto
            temperature: '{{ states(''input_number.ontemp''~time_index) }}'
          target:
            entity_id: climate.trv_masterbedroom
    - conditions:
        - condition: trigger
          id: off
        - '{{ dow_match }}'
      sequence:
        - service: climate.set_temperature
          data:
            hvac_mode: off
          target:
            entity_id: climate.trv_masterbedroom

Hey that’s nice solution, short and easy to understand.

The only issue when compiling is the time variable

- condition: time
            before: input_datetime.off{{dow_type}}{{time_index}}

I think the indents got thrown off when I copy/pasted back and forth from the forum, I’ve corrected it in the post above.

FWIW, I’m not sure that condition is really necessary in this configuration. I guess there might be a weird edge case that it catches…

Hey thats a nice solution i have it place for one of our rooms im making a blueprint to replate it aswell it works as intended. thank you.

Have to mention, 123 Traras and CentralCommand for there guidance and support thank you

alias: Heating Master Bedroom Schedule
description: ''
trigger:
  - platform: time
    id: 'on'
    at:
      - input_datetime.onwkday1
      - input_datetime.onwkday2
      - input_datetime.onwkday3
      - input_datetime.onwkday4
      - input_datetime.onwkday5
      - input_datetime.onwkend1
      - input_datetime.onwkend2
      - input_datetime.onwkend3
      - input_datetime.onwkend4
      - input_datetime.onwkend5
  - platform: time
    id: 'off'
    at:
      - input_datetime.offwkday1
      - input_datetime.offwkday2
      - input_datetime.offwkday3
      - input_datetime.offwkday4
      - input_datetime.offwkday5
      - input_datetime.offwkend1
      - input_datetime.offwkend2
      - input_datetime.offwkend3
      - input_datetime.offwkend4
      - input_datetime.offwkend5
  - platform: state
    id: mod
    entity_id:
      - input_datetime.onwkday1
      - input_datetime.onwkday2
      - input_datetime.onwkday3
      - input_datetime.onwkday4
      - input_datetime.onwkday5
      - input_datetime.onwkend1
      - input_datetime.onwkend2
      - input_datetime.onwkend3
      - input_datetime.onwkend4
      - input_datetime.onwkend5
      - input_datetime.offwkday1
      - input_datetime.offwkday2
      - input_datetime.offwkday3
      - input_datetime.offwkday4
      - input_datetime.offwkday5
      - input_datetime.offwkend1
      - input_datetime.offwkend2
      - input_datetime.offwkend3
      - input_datetime.offwkend4
      - input_datetime.offwkend5
      - input_boolean.en_trv_masterbedroom
      - input_boolean.en_trv_masterbedroomwkday1
      - input_boolean.en_trv_masterbedroomwkday2
      - input_boolean.en_trv_masterbedroomwkday3
      - input_boolean.en_trv_masterbedroomwkday4
      - input_boolean.en_trv_masterbedroomwkday5
      - input_boolean.en_trv_masterbedroomwkend1
      - input_boolean.en_trv_masterbedroomwkend2
      - input_boolean.en_trv_masterbedroomwkend3
      - input_boolean.en_trv_masterbedroomwkend4
      - input_boolean.en_trv_masterbedroomwkend5
    to: null
  - platform: homeassistant
    event: start
    id: start
condition:
  - condition: state
    entity_id: input_boolean.boost_masterbedroom
    state: 'off'
    for:
      hours: 0
      minutes: 0
      seconds: 10
  - condition: state
    entity_id: input_boolean.en_trv_masterbedroom
    state: 'on'
action:
  - variables:
      dow_type: '{{''sensor.dayofweek_type''}}'
  - repeat:
      until:
        - condition: template
          value_template: >-
            {{repeat.index ==5 

            or

            now().strftime('%H:%M:%S') < states('input_datetime.off'+
            states(dow_type)~repeat.index) 

            and 

            now().strftime('%H:%M:%S') >= states('input_datetime.on'+
            states(dow_type)~repeat.index) 

            and 

            is_state('input_boolean.en_trv_masterbedroom' +
            states(dow_type)~repeat.index,'on' )

            }}
      sequence:
        - if:
            - condition: template
              value_template: |-
                {{ now().strftime('%H:%M:%S') <
                states('input_datetime.off'+ states(dow_type)~repeat.index)
                and now().strftime('%H:%M:%S') >= 
                states('input_datetime.on'+ states(dow_type)~repeat.index)
                and   
                is_state('input_boolean.en_trv_masterbedroom' +
                states(dow_type)~repeat.index,'on' )}}
          then:
            - service: climate.set_temperature
              data:
                hvac_mode: auto
                temperature: >-
                  {{ states("input_number.ontemp"+
                  states(dow_type)~repeat.index) }}
              target:
                entity_id: climate.trv_masterbedroom
          else:
            - service: climate.set_temperature
              data:
                hvac_mode: 'off'
                temperature: >-
                  {{ states("input_number.offtemp"+
                  states(dow_type)~repeat.index) }}
              target:
                entity_id: climate.trv_masterbedroom
mode: single

just an update on the above this is what i ended up doing.