Sensor template for current energy price

I’m trying to create a sensor for the Energy dashboard that provides the current price of Electricity.

Unfortunately my templating skills are not up to scratch for a task such as this just yet.

My energy rate works as follows

Weekdays:
0700-1359 - $0.27555
1400-1959 - $0.65626
2000-2159 - $0.27555
2200-0659 - $0.17974

Weekends & Public Holidays:
0700-2159 - $0.27555
2200-0659 - $0.17974

I’m not sure if I’m on the right track, but so far I’ve got the following

  - platform: template
    sensors:
      current_electricity_rate:
        value_template: >-
          {% set t = strptime(states('sensor.time'), '%H:%M') %}
          {%- if t.hour >14 and t.hour <=20 and is_state('binary_sensor.workday_sensor', 'on') -%}0.65626
          {%- elif t.hour >7 and t.hour <=14 and is_state('binary_sensor.workday_sensor', 'on') -%}0.27555
          {%- elif t.hour >20 and t.hour <=22 and is_state('binary_sensor.workday_sensor', 'on') -%}0.27555
          {%- elif t.hour >7 and t.hour <=22 and is_state('binary_sensor.workday_sensor', 'off') -%}0.27555
          {%- else -%}0.17974
          {%- endif -%}
        friendly_name: Current Electricity Rate
        unit_of_measurement: AUD/kWh

There seem to be a couple of issues.

Firstly, at 2230 last night it was showing the rate as $0.27555/kWh when it should have been $0.17974/kWh - so there’s obviously something wrong with my template.

Secondly, the Energy panel accepts this sensor I’ve created however it just sits at $0 of usage even though its getting the current energy price from this sensor. Could there be something wrong with the format? Previous attempts it would give an error when trying to add this as the cost sensor, but its no longer giving any errors so I thought it must have been right, but even after a good 12 hours, its still sitting at $0 so clearly something isn’t right.

Would really appreciate some help with this!

1 Like

This:

{%- elif t.hour >20 and t.hour <=22

Should be

{%- elif t.hour >=20 and t.hour <22

Same for all the others.

1 Like

Ah yes! You are indeed correct! I can’t believe I didn’t pick that one up myself. Thankyou very much!

Any thoughts on why the Energy panel continues to show $0 when using this sensor to provide the current electricity price?

Try:

unit_of_measurement: "$/kWh"

That’s what my input numbers use and they are working.

Unfortunately this didn’t work. Changed it last night and left it overnight just in case it needed some time to start ticking over but still sitting on $0

Any other suggestions?

Are there any issues for your energy or price sensors listed in Developer Tools / Statistics?

The Energy sensor is not showing any issues in Statistics - and it’s working in the Energy panel without issue also.

The price sensor doesn’t show up in Statistics though - which I imagine is because it’s not calculating the cost, it’s merely providing the current price, which is the option I’m adding it into the Energy panel as.

So I’ve finally got this all figured out (well 99% sure anyway!) and thought I’d update this thread for anyone who runs into similar issues in the future.

I was never able to get it working using the “Use an entity with current price” option - I still have absolutely no idea why this wouldn’t work.

The route I took was creating a new entity to work with the “Use an entity tracking total costs” option.

There were a few parts to this:

The Utility Meter integration - using this I was able to create some sensors which track the daily and monthly energy consumption in the Peak, Shoulder and Off-Peak tariff periods and reset at the end of the respective time periods, the data recorded by this integration persists across reboots. Note, this integration doesn’t tie directly to my Energy monitor, it simply records the data reported from the existing Sensors in HA for my Energy monitor.

utility_meter:
  daily_energy:
    name: Daily Energy
    source: sensor.daily_consumption
    cycle: daily
    tariffs:
      - peak
      - shoulder
      - off-peak

I then required some automations to change the tariff period. Because of the complicated tariff periods with my Electricity provider, I actually left the template sensor we have been discussing above in place, and use the state of that to change the utility meter tariff because it saves a couple of automations.

Sensor for tracking tariff

- platform: template
    sensors:
      current_electricity_rate:
        value_template: >-
          {% set t = strptime(states('sensor.time'), '%H:%M') %}
          {%- if t.hour >=14 and t.hour <20 and is_state('binary_sensor.workday_sensor', 'on') -%}0.65626
          {%- elif t.hour >=7 and t.hour <14 and is_state('binary_sensor.workday_sensor', 'on') -%}0.27555
          {%- elif t.hour >=20 and t.hour <22 and is_state('binary_sensor.workday_sensor', 'on') -%}0.27555
          {%- elif t.hour >=7 and t.hour <22 and is_state('binary_sensor.workday_sensor', 'off') -%}0.27555
          {%- else -%}0.17974
          {%- endif -%}
        friendly_name: Current Electricity Rate
        unit_of_measurement: $/kWh

Automations to change tariff in utility meter sensors

alias: Energy - Update Tariff - Off-Peak
trigger:
  - platform: state
    entity_id: sensor.current_electricity_rate
    to: '0.17974'
action:
  - service: utility_meter.select_tariff
    data:
      tariff: off-peak
    target:
      entity_id:
        - utility_meter.daily_energy
mode: single


alias: Energy - Update Tariff - Peak
trigger:
  - platform: state
    entity_id: sensor.current_electricity_rate
    to: '0.65626'
action:
  - service: utility_meter.select_tariff
    data:
      tariff: peak
    target:
      entity_id:
        - utility_meter.daily_energy
mode: single


alias: Energy - Update Tariff - Shoulder
trigger:
  - platform: state
    entity_id: sensor.current_electricity_rate
    to: '0.27555'
action:
  - service: utility_meter.select_tariff
    data:
      tariff: shoulder
    target:
      entity_id:
        - utility_meter.daily_energy
mode: single

The final piece in the puzzle is then the template sensor doing the maths for the usage costs and then tap on the $0.95535 flat rate daily service charge to get my final figure

daily_energy_cost:
        friendly_name: Daily Energy Cost
        device_class: monetary
        unit_of_measurement: AUD
        value_template: >- 
          {{(float(states('sensor.daily_energy_off_peak')) * 0.17974) + (float(states('sensor.daily_energy_peak')) * 0.65626) + (float(states('sensor.daily_energy_shoulder')) * 0.27555) + 0.95535}}

Then I just added this Sensor as the cost tracker for the Energy integration and all seems to be working - at this stage anyway. Will report back here if I notice any problems or inconsistencies in the data over the next month but so far so good for the past 24 hours.

2 Likes

Thanks. Your solution was really helpful.
I inverted your logic: a template sensor for electricity rate (night, off-peak, peak) and an automation to convert that to “Electricity Cost”.


Because i wanted to show on the dashboard what the current rate is so we can decide if we want to run expensive things or not. And am considering adding green/orange/red leds to an esphome device so we can see at a glance
A snippet from configuration.yaml below:

# see https://www.home-assistant.io/integrations/workday/
binary_sensor:
  - platform: workday
    country: NZ
    workdays: [mon, tue, wed, thu, fri]
    excludes: [sat, sun, holiday]
    
# see https://www.home-assistant.io/integrations/template/    
template:
  - trigger:
      - platform: time_pattern
        minutes: 0
        # update every hour on the hour
    sensor:
      # for octopus energy
      - name: "Electricity Rate"
        state: >-
          {% set hour = now().hour %}
          {%- if hour < 7 or hour >= 23 -%} 
            night
          {%- else -%}
            {%- if is_state('binary_sensor.workday_sensor', 'on') and (hour <= 10 or (hour >= 17 and hour <= 20)) -%}
              peak
            {%- else -%}
              off-peak
            {%- endif -%}
          {%- endif -%}
        unique_id: electricity_rate
2 Likes

Can somebody help?

I tried but I don’t understand what sections I need to change. I am trying to achieve where weekdays 22:00-07:00 is night price and 07:00-22:00 is day price. And weekends are always night price.

I am a noob when I need to read and change code :grinning:

state: >-
{% set hour = now().hour %}
  {%- if is_state('binary_sensor.workday_sensor', 'on') and (hour >= 7 and hour <= 22) -%}
    day
  {%- else -%}
    night
{%- endif -%}

Thanks.
But where i put prices for night and day tariff?

where it says day and night

Hi,
Since rates for me are just day(starting at 7:00) and night (starting at 23:00), I wonder if above can be further simplified? E.g. can I specify the above times as triggers instead of checking every hour if it is within the boundaries of a tariff? Also can a Schedule helper be used instead of hard coding the periods (easier to change when DST offsets the starting times)?

You could just use a simple sensor like this. Change the rates to suit your day and night values. Or you could just use ‘day’ and ‘night’ instead of numbers.

- platform: template
    sensors:
      current_electricity_rate:
        value_template: >-
          {% set t = strptime(states('sensor.time'), '%H:%M') %}
          {%- if t.hour >=7 and t.hour <22 and is_state('binary_sensor.workday_sensor', 'on') -%}0.30
          {%- else -%}0.17
          {%- endif -%}
        friendly_name: Current Electricity Rate
        unit_of_measurement: $/kWh

Thank you. It works perfectly.

@pavarnos What card are you using to show your energy consumption by color, there?

thats just a screenshot from our electricity provider