Template to script or automation help/ heating schedule

Hi could some one help with the below automation which is currently in use and working, i have this automation running for each room of the house. The final goal is to shorten the code using a template within the automation. At the bottom i have added the template which works in the template editor. I think i have the template correct put i do not know how i can add this to the automation.

Current automation:

alias: MasterBedroomSchedule
description: ''
trigger:
  - platform: time_pattern
    minutes: /1
  - 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:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.en_trv_masterbedroom1
            state: 'on'
          - condition: time
            after: input_datetime.onwkday1
            before: input_datetime.offwkday1
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
        sequence:
          - service: climate.set_temperature
            data:
              hvac_mode: auto
              temperature: '{{ states(''input_number.ontemp1'') }}'
            target:
              entity_id: climate.trv_masterbedroom
    default:
      - condition: not
        conditions:
          - condition: state
            entity_id: input_boolean.en_trv_masterbedroom1
            state: 'on'
      - condition: time
        after: input_datetime.onwkday1
        before: input_datetime.offwkday1
        weekday:
          - mon
          - tue
          - wed
          - thu
          - fri
      - service: climate.set_temperature
        target:
          entity_id: climate.trv_masterbedroom
        data:
          hvac_mode: 'off'
          temperature: '{{ states(''input_number.offtemp1'') }}'
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.en_trv_masterbedroom2
            state: 'on'
          - condition: time
            after: input_datetime.onwkday2
            before: input_datetime.offwkday2
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
        sequence:
          - service: climate.set_temperature
            data:
              hvac_mode: auto
              temperature: '{{ states(''input_number.ontemp2'') }}'
            target:
              entity_id: climate.trv_masterbedroom
    default:
      - condition: time
        after: input_datetime.onwkday2
        before: input_datetime.offwkday2
        weekday:
          - mon
          - tue
          - wed
          - thu
          - fri
      - service: climate.set_temperature
        target:
          entity_id: climate.trv_masterbedroom
        data:
          hvac_mode: 'off'
          temperature: '{{ states(''input_number.offtemp2'') }}'
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.en_trv_masterbedroom3
            state: 'on'
          - condition: time
            after: input_datetime.onwkday3
            before: input_datetime.offwkday3
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
        sequence:
          - service: climate.set_temperature
            data:
              hvac_mode: auto
              temperature: '{{ states(''input_number.ontemp3'') }}'
            target:
              entity_id: climate.trv_masterbedroom
    default:
      - condition: time
        after: input_datetime.onwkday3
        before: input_datetime.offwkday3
        weekday:
          - mon
          - tue
          - wed
          - thu
          - fri
      - service: climate.set_temperature
        target:
          entity_id: climate.trv_masterbedroom
        data:
          hvac_mode: 'off'
          temperature: '{{ states(''input_number.offtemp3'') }}'
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.en_trv_masterbedroom4
            state: 'on'
          - condition: time
            after: input_datetime.onwkend1
            before: input_datetime.offwkend1
            weekday:
              - sat
              - sun
        sequence:
          - service: climate.set_temperature
            data:
              hvac_mode: auto
              temperature: '{{ states(''input_number.ontemp4'') }}'
            target:
              entity_id: climate.trv_masterbedroom
    default:
      - condition: time
        after: input_datetime.onwkend1
        before: input_datetime.offwkend1
        weekday:
          - sat
          - sun
      - service: climate.set_temperature
        target:
          entity_id: climate.trv_masterbedroom
        data:
          hvac_mode: 'off'
          temperature: '{{ states(''input_number.offtemp4'') }}'
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.en_trv_masterbedroom5
            state: 'on'
          - condition: time
            after: input_datetime.onwkend2
            before: input_datetime.offwkend2
            weekday:
              - sat
              - sun
        sequence:
          - service: climate.set_temperature
            data:
              hvac_mode: auto
              temperature: '{{ states(''input_number.ontemp5'') }}'
            target:
              entity_id: climate.trv_masterbedroom
    default:
      - condition: time
        after: input_datetime.onwkend2
        before: input_datetime.offwkend2
        weekday:
          - sat
          - sun
      - service: climate.set_temperature
        target:
          entity_id: climate.trv_masterbedroom
        data:
          hvac_mode: 'off'
          temperature: '{{ states(''input_number.offtemp5'') }}'
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.en_trv_masterbedroom6
            state: 'on'
          - condition: time
            after: input_datetime.onwkend3
            before: input_datetime.offwkend3
            weekday:
              - sat
              - sun
        sequence:
          - service: climate.set_temperature
            data:
              hvac_mode: auto
              temperature: '{{ states(''input_number.ontemp6'') }}'
            target:
              entity_id: climate.trv_masterbedroom
    default:
      - condition: time
        after: input_datetime.onwkend3
        before: input_datetime.offwkend3
        weekday:
          - sat
          - sun
      - service: climate.set_temperature
        target:
          entity_id: climate.trv_masterbedroom
        data:
          hvac_mode: 'off'
          temperature: '{{ states(''input_number.offtemp6'') }}'
  - choose:
      - conditions:
          - condition: time
            after: input_datetime.offwkday1
            before: input_datetime.onwkday2
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
        sequence:
          - service: climate.set_temperature
            data:
              hvac_mode: 'off'
              temperature: '{{ states(''input_number.offtemp1'') }}'
            target:
              entity_id: climate.trv_masterbedroom
    default: []
  - choose:
      - conditions:
          - condition: time
            after: input_datetime.offwkday2
            before: input_datetime.onwkday3
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
        sequence:
          - service: climate.set_temperature
            data:
              hvac_mode: 'off'
              temperature: '{{ states(''input_number.offtemp2'') }}'
            target:
              entity_id: climate.trv_masterbedroom
    default: []
  - choose:
      - conditions:
          - condition: time
            after: input_datetime.offwkday3
            before: input_datetime.onwkday1
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
        sequence:
          - service: climate.set_temperature
            data:
              hvac_mode: 'off'
              temperature: '{{ states(''input_number.offtemp3'') }}'
            target:
              entity_id: climate.trv_masterbedroom
    default: []
  - choose:
      - conditions:
          - condition: time
            after: input_datetime.offwkend1
            before: input_datetime.onwkend2
            weekday:
              - sat
              - sun
        sequence:
          - service: climate.set_temperature
            data:
              hvac_mode: 'off'
              temperature: '{{ states(''input_number.offtemp4'') }}'
            target:
              entity_id: climate.trv_masterbedroom
    default: []
  - choose:
      - conditions:
          - condition: time
            after: input_datetime.offwkend2
            before: input_datetime.onwkend3
            weekday:
              - sat
              - sun
        sequence:
          - service: climate.set_temperature
            data:
              hvac_mode: 'off'
              temperature: '{{ states(''input_number.offtemp5'') }}'
            target:
              entity_id: climate.trv_masterbedroom
    default: []
  - choose:
      - conditions:
          - condition: time
            after: input_datetime.offwkend3
            weekday:
              - sat
              - sun
        sequence:
          - service: climate.set_temperature
            data:
              hvac_mode: 'off'
              temperature: '{{ states(''input_number.offtemp6'') }}'
            target:
              entity_id: climate.trv_masterbedroom
    default: []
  - service: climate.set_preset_mode
    data:
      preset_mode: manual
    target:
      entity_id: climate.trv_masterbedroom
mode: single
My template below :
{% set dev = 
(state_attr("climate.trv_masterbedroom","local_temperature")|float - 
state_attr("climate.trv_masterbedroom","temperature")|float)|round(2)%}
{{dev}}
{%for times in range(1,11)%}

{% if now().weekday() < 5 |int %}
{% set ontime = today_at(states("input_datetime.onwkday" ~ times)) %}
{% set offtime = today_at(states("input_datetime.offwkday" ~ times)) %}

{% else %}
{% set ontime = today_at(states("input_datetime.onwkend" ~ times)) %}
{% set offtime = today_at(states("input_datetime.offwkend" ~ times)) %}

{%endif%}

{%if now() >= ontime and now() < offtime %}
{{" Heating on"}}
{%else%}
{{" Heating Off"}}
{%endif%}
{%endfor%}

Pleae wrap code/config in triple backticks so it is readable. See here

I’m having a really hard time reading your automation since the YAML got totally butchered as markdown (particularly the tabbing). I can read your template though and from that can kind of guess what you’re doing? Seems like you have a whole lot of input datetimes and are trying to make a schedule by seeing which two you’re between and setting the climate accordingly.

Assuming I’m right then you might want to put this on pause until 2022.5 comes out. I’m not sure if you’ve seen it yet but the new calendar trigger seems like it will be your new best friend.

Instead of having a million helpers just make a calendar for heating schedule with events for when the hvac should be on. Then use the calendar trigger. When an event starts turn the hvac on. When an event ends turn the hvac off. That’s it, that’s the entire automation. No need for chooses, weekday selectors, datetime helpers, etc. It’ll probably be like 20 lines.

In addition to Mike’s comments…

Generally speaking, if you’re using time pattern to fire your automation every minute, you’re probably doing something wrong. It’s the home automation equivalent of the kid in the back of the car going “Are we there yet? Are we there yet? Are we there yet? Are we there yet?”…

Changing to Time triggers based on your Input datetimes reduces the daily number of times the automation is triggered from 1440 to 12:

trigger:
  - platform: time
    at:
    - 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
  - platform: state
    entity_id: input_boolean.en_trv_masterbedroom

2 Likes

Is there a reason why you chose not to use Time Trigger?

Thats the code above fixed thanks for the update.

Yes i understand what you mean regarding the calender events although i could not get any documentation on the calender trigger, would the times be changeable on the fly i.e from lovelace.

The reason the automation is ran every 1 minute is to allow for a scenraio were the automation could not run at the start time or at the off time or if home assistant was not running or i have been making changes at the time. It also gives us the ability to stop it easily by switching a toggle or changing the times so it has immediate effect.

There are better ways to do that than with a Time Pattern Trigger. All of my scheduled automations are designed to survive restarts and outages without using a Time Pattern Trigger.

Not a direct benefit of a Time Pattern Trigger.

As mentioned, a Time Pattern Trigger is often misused; the vast majority of examples employing it are often inappropriate. It has a very limited range of justifiable applications.

If you are interested, here’s a brief tutorial on the subject of composing a “resilient” automation:

Not sure when you checked but 2022.5 literally just came out and so now the doc for it is visible. The doc on the calendar trigger can now be found here

Yes. I mean not necessarily from lovelace but no restart required. Like what I would do here is this:

  1. Make a calendar in google called hvac schedule
  2. Set up google calendar if you haven’t already
  3. Tell HA to track “hvac schedule”
  4. Use the new calendar trigger in your automation to turn the hvac on when any event starts on the hvac calendar and turn the hvac off when any event ends on the hvac calendar
  5. Make events in that calendar according to your hvac schedule. Like “HVAC on” with start time set to day and time it should turn on and end set to when hvac should turn off. Make the event repeat indefinitely. Keep adding events this way until your entire weekly schedule is in this calendar.

You can modify this calendar at any time in google this way. You can also add new events to it from HA if you want by using google.add_event service although you can only modify or delete existing events from the google calendar UI.

You can also do this with any other calendar HA integrates with, google is just the only one OOTB. But if you use HACS there are other options, like this one for Office 365.

FWIW, the one philosophical problem I have with using Google Calendar (or any cloud-based calendar) is that it makes something as fundamental as scheduling dependent on the cloud. Effectively, it makes a cornerstone of “local control” Home Assistant into “cloud-dependent” SmartThings.

I recognize its value but for basic scheduled tasks, my preference is to keep it local; to each their own.

2 Likes

I appreciate the advise, i agree and have thought about running them on start up as i do this for quite alot of other things.
The benefits i see with time pattern is it can read updated input timedate data should anyone in the house want to switch that rooms heating off or modify how long it will be on for.

In my example how could i incorporate the template into the script where it loops through the 10 input times.

Yea I know, its not ideal. There was discussion about a local calendar at one point. Like an option to simply make and manage calendars that are stored only in HA. I think this new trigger might bring that discussion back to prominence. I’m going to go see if there’s a feature request for it.

EDIT: Couldn’t find one so I made one

1 Like

I could spend time re-engineering your automation but the disincentive is that, despite everyone’s advice, you still believe the Time Pattern Trigger has benefits for this application.

Thank you, again it probably comes down to personal preference the calender schedule does look though.

This was one my first goes at creating automations for our heating schedule although far from perfect it nearly is for us as the rest of the family understand it and a big part to that is the layout we have on lovelace its very easy to make adjustments and see when the schedule is active.

I understand it uses alot of helpers/variables but for me that was really easy to add hundreds in notepad.

I recall experimenting with the Caldav integration and a local instance of Radicale but the clumsy part was adding calendar events. It worked but I lost interest in it.

Upvoted your FR.

I would not expect that at all, i appreciate all the help you have given but i do not expect you to re-engineer it for me.
I do not see a big problem with it, it works as intended and yes it runs 1440 times a day but it does not affect the system and i could easily change that to run every 5 or 10 minutes.

If this solution you added above allowed for modifications in between the times i would totally agree with you it would be the way to go along with runs the automation on startup.

trigger:
  - platform: time
    at:
    - 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
  - platform: state
    entity_id: input_boolean.en_trv_masterbedroom

Another way i could have asked this question was adding a for loop into an automation/script template and using the result as a condition.

Can you elaborate on what you mean by “modifications in between the times”? What is being modified in between the scheduled times?

If anyone changes an input_datetime variable it will currently take effect within 1 minute or whatever we set the time pattern at.

I have maybe over assumed here, but the solution were only triggering the automattion at the input_datetime values would not allow for this.

Add a state change trigger then

trigger:
  - platform: time
    at:
    - 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
  - 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:

Now when any of the helpers change it immediately reruns the automation and sets it according to the new schedule. Instant response for updates, don’t even have to wait for the minute to be up.

Yea thats a very simple solution to the time pattern i never thought of… Thank you.

1 Like