Date Range for Automation

I’m trying to setup an automation to turn off a device during peak hour for my utility company, but I’m not seeing the option for a date range, for example here’s their posted peak hours.

Summer Periods (April 1 through October 31) On-Peak Hours: M-F 1 to 6 pm

Winter Periods (November 1 through March 31) On-Peak Hours: M-F 7 to 10 am & 6 to 9 pm

I would like to automate turning the devices off during these peak hours, is there a way for me to get this done?

This will require the use of templates. Here’s a binary sensor I use to indicate peak hours that you can adapt and use to trigger your automations (DST = daylight savings time):

template:
  - binary_sensor:
      - name: "Is DST"
        state: "{{ now().timetuple().tm_isdst == 1 }}"
        
      - name: "Peak Rate"
        icon: "mdi:power-plug"
        state: >
          {% if states('binary_sensor.is_dst')|bool(0) %}
            {{ (now().weekday() < 5) and ( (7 < now().hour < 11) or (16 < now().hour < 22) ) }}
          {% else %}
            {{ (now().weekday() < 5) and ( (6 < now().hour < 10) or (15 < now().hour < 21) ) }}
          {% endif %}

And a sensor that tells me what rate is being used:

template:
  - sensor:
      - name: "Energy Tariff in Use"
        icon: "mdi:counter"
        state: >
          {% if states('binary_sensor.is_dst')|bool(0) %}
            {% if now().weekday() > 4 %}
              Off Peak (Weekend)
            {% elif 7 < now().hour < 11 %}
              Peak DST (8AM to 11AM)
            {% elif 16 < now().hour < 22 %}
              Peak DST (5PM to 10PM)
            {% elif 10 < now().hour < 17 %}
              Off Peak DST (11AM to 5PM)
            {% else %}
              Off Peak DST (10PM to 8AM)
            {% endif %}
          {% else %}
            {% if now().weekday() > 4 %}
              Off Peak (Weekend)
            {% elif 6 < now().hour < 10 %}
              Peak (7AM to 10AM)
            {% elif 15 < now().hour < 21 %}
              Peak (4PM to 9PM)
            {% elif 9 < now().hour < 16 %}
              Off Peak (10AM to 4PM)
            {% else %}
              Off Peak (9PM to 7AM)
            {% endif %}
          {% endif %}

Another option would be to set up Schedule helpers for each of the two periods to cover the weekdays and hours part, then use a couple simpler templates in your automation to handle the date ranges:

trigger:
  - platform: state
    entity_id: binary_sensor.summer_peak_hours
    to: 'on'
    variables:
      is_season: "{{  4 <= now().month < 11 }}"
  - platform: state
    entity_id: binary_sensor.winter_peak_hours
    to: 'on'
    variables:
      is_season: "{{  4 > now().month or now().month >= 11 }}"
condition:
  - "{{ is_season }}"
action:
....
1 Like

Thank I will try it.

I will also this this out. Thanks