Help with utility time of use (TOU) template sensor?

I’ve got a somewhat complex TOU rate from my utility. I’d like to make a sensor that can tell me what

I tried to search solutions from others and found this. I like how it first considers the holidays, but it’s still a bit too simple on the month/day of week/time of day logic.

Does anyone have an example for this?

Have you considered using the workday binary_sensor.

In your configuration.yaml

#set up workdays - https://www.home-assistant.io/integrations/workday
binary_sensor:
  - platform: workday
    country: USA
    workdays: [mon, tue, wed, thu, fri]
    excludes: [sat, sun, holiday]

setup an energy season sensor

 - name: "Energy Season"
      unique_id: "energy_season"
      state: >
        {% if now().month in [6,7,8,9] %}
          summer
        {% else %}
          winter
        {% endif %}

Then you can setup the energy sensor

 - name: "Energy Price"
      unique_id: "energy_price"
      state: >-
        {% if states('sensor.energy_season') == "summer" %}
          {% if states('binary_sensor.workday_sensor') and (17 <= now().hour <=20) %}
            0.12  Peak Rate
          {% elif states('binary_sensor.workday_sensor') and ((14 <= now().hour <=15) or (20 <= now().hour <=21))) or (now().day in [6,7] and (17 <= now().hour <=19))  %}
            0.10 Parital-Peak Rate
          {% else %}
            0.08 Off Peak Rate
          {% endif %}
        {% else %} 
          {% if states('binary_sensor.workday_sensor') and (17 <= now().hour <=19) %}
            0.10 Parital-Peak Rate
          {% else %}
            0.08 Off-Peak Rate
          {% endif %}
        {% endif %} 
      unit_of_measurement: USD/kWh

Here it is in the developer tools. I put the text by the rate so I wouldn’t lose track of where I was. These will need removal from the sensor.

I think I have all of the indentations and quoting correct. I can’t check on my system but this should get you there.

EDIT: found a way to test and added the corrections to make it function. Fixed the times

1 Like

Here it is cleaned up and using only one sensor.

#--------------------------------------------------------------------------------------------------
# For peak demand pricing
#--------------------------------------------------------------------------------------------------
sensor:
  - name: "Energy Price"
    unique_id: "energy_price"
    state: >-
      {% set wd = states('binary_sensor.workday_sensor') %}
      {% set hr = now().hour %}
      {% if (now().month in [4,5,6,7,8,9]) %}
        {% if wd and (16 <= now().hour < 21) %}
          0.121
        {% elif (wd and (14 <= hr < 16) or (20 <= hr < 22)) or (now().day in [6,7] and (17 <= hr < 20)) %}
          0.101
        {% else %}
          0.081
        {% endif %}
      {% else %} 
        {% if wd and (17 <= hr < 20) %}
          0.102
        {% else %}
          0.082
        {% endif %}
      {% endif %} 
    unit_of_measurement: "USD/kWh"

EDIT: fixed list for summer. @poldim please see the change in months