Help with SDG&E (San Diego) Electricity cost calculations

Hi!
I live in San Diego, so I have to deal with SDG&E. We have the highest electricity costs in america. One of the reasons why is because they change their price per kwh during the day, several times. There’s a weekday, and a weekend schedule, and also schedules for ‘winter’ and ‘summer’ where the prices change but the schedule stays the same. For example:

12am-6am: off peak (49 cents per kwh)
6am-10am: mid peak (55 cents per kwh)
10am-2pm: off peak (49 again)
2pm-4pm: mid-peak (55)
4pm-9pm: peak (81)
9pm-12am: mid-peak (55)

this makes it pretty challenging to compute how much power i “bought” during the day, and how much power i “sold” if my solar and battery system pushed power back into the grid.

I see lots of posts where folks who are lucky enough to have a single price per kwh can easily do cost calculations, but nothing to go after this sort of setup. Has anybody come across anything yet?

did you ever figure this out?

nope. sadly either nobody has noticed this post, or nobody has figured out how to do this with HA yet.

This is easily calculable using template sensors. For the variables, you can create input helper entities so that when the rates change, you can simply update the helpers as opposed to the sensors themselves. For me, the problem is reading the meter and sending the data to Home Assistant.

do you have example yaml?

Sure. You can use this template sensor. This is in my templates.yaml file:

  - name: SDGE Electricity Cost
    unit_of_measurement: "$"
    state_class: measurement
    icon: mdi:currency-usd
    state: >-
      {% set hour = now().hour %}
      {% if 12 <= hour < 6 or 10 <= hour < 14 %} {{ states('input_number.off_peak') }}
      {% elif 6 <= hour < 10 or 14 <= hour < 16 or 21 <= hour < 12 %} {{ states('input_number.mid_peak') }}
      {% else %} {{ states('input_number.on_peak') }}
      {% endif %}

Be sure to create the above three helpers.

1 Like

oh rad, thank you so much!

I’m on a more complex EV-TOU5 plan and I’ve been using automations to change the rate. It works well, except for Holidays which I haven’t really spent a lot of time trying to fix. I also have to manually update the cost when it changes between Summer and Winter rates.

I haven’t even bothered trying to calculate the NBCs which add up when charging an EV. I just know that there will be an extra charge of $0.027 for every imported kWh that can’t be offset by generation credits.

I often miss the change of Summer/Winter

How are you getting your power data from SDGE ? Or you are getting it from your solar system ?

i have a tesla energy gateway and 2 powerwalls, so I’m using two tesla integrations and pulling data from that.

SDG&E doesn’t have an API you can use to pull the information. You will need to install a power metering system. My Solar system added one, so I pull the data directly from the Enphase controller.

I have four automations that change the tariff based on time of day. Peak is easy, it’s always from 4 to 9 PM. Super Off Peak gets a little more complicated as it’s always after midnight but the end time depends on the day. Off Peak is the most complex one that depends on day of the week and if it is a holiday or not.

I defined three tariffs and added them to my Energy dashboard as Import and Export, three each for a total of 6

These were added in the configuration YAML under utility_meter and use the sensors energy.import and energy.export:

utility_meter:
 
  daily_energy:
    source: sensor.energy_import
    name: Daily Import Meter
    cycle: daily
    tariffs:
      - SDGE_OffPeak
      - SDGE_SuperOffPeak
      - SDGE_Peak

  daily_energy_export:
    source: sensor.energy_export
    name: Daily Export Meter
    cycle: daily
    tariffs:
      - SDGE_OffPeak
      - SDGE_SuperOffPeak
      - SDGE_Peak

The automations then select the corresponding tariff as the active one for export and import, and it also sets a helper I use to display the current price. The price used by each Tariff is defined in the Energy dashboard. I used to manually update them every time the rates changed, but now I am using a separate helper and have a dashboard with all the rates easily accessible.

image

I still have to manually select Winter vs Summer, but this should be easy to automate, just haven’t spent time on it yet.

I track the Holidays using the Holidays HACS integration which allows you to pick which holidays you care about, and then it puts them in a calendar where you get an entity with a number. If the number is 0, it means the holiday is today. This is used in my conditions below. HA added native Holiday support in 2024.1 so I need to look into moving this there as this integration will no longer be supported.

alias: Set Peak Tariff
description: ""
trigger:
  - platform: time
    at: "16:00:00"
condition: [ ]
action:
  - service: select.select_option
    data:
      option: SDGE_Peak
    target:
      entity_id: select.daily_energy_export
  - service: select.select_option
    data:
      option: SDGE_Peak
    target:
      entity_id: select.daily_energy
  - service: input_number.set_value
    data:
      value: 51.2
    target:
      entity_id: input_number.current_electricity_rate
mode: single
alias: Set Super Off Peak Tariff
description: ""
trigger:
  - platform: time
    at: "00:00:00"
condition: [ ]
action:
  - service: select.select_option
    data:
      option: SDGE_SuperOffPeak
    target:
      entity_id: select.daily_energy_export
  - service: select.select_option
    data:
      option: SDGE_SuperOffPeak
    target:
      entity_id: select.daily_energy
  - service: input_number.set_value
    data:
      value: 12.38
    target:
      entity_id: input_number.current_electricity_rate
mode: single
alias: Set Off Peak (every day after 2 and 9 PM)
description: ""
trigger:
  - platform: time
    at: "21:00:00"
  - platform: time
    at: "14:00:00"
condition: [ ]
action:
  - service: select.select_option
    data:
      option: SDGE_OffPeak
    target:
      entity_id: select.daily_energy_export
  - service: select.select_option
    data:
      option: SDGE_OffPeak
    target:
      entity_id: select.daily_energy
  - service: input_number.set_value
    data:
      value: 38.17
    target:
      entity_id: input_number.current_electricity_rate
mode: single
alias: Set Off Peak Conditional Weekday Mornings
description: ""
trigger:
  - platform: time
    at: "06:00:00"
condition:
  - condition: and
    conditions:
      - condition: time
        before: "16:00:00"
        after: "06:00:00"
        weekday:
          - mon
          - tue
          - wed
          - thu
          - fri
      - condition: not
        conditions:
          - condition: state
            entity_id: calendar.sdge_holidays
            state: "0"
action:
  - service: select.select_option
    data:
      option: SDGE_OffPeak
    target:
      entity_id: select.daily_energy_export
  - service: select.select_option
    data:
      option: SDGE_OffPeak
    target:
      entity_id: select.daily_energy
  - service: input_number.set_value
    data:
      value: 38.17
    target:
      entity_id: input_number.current_electricity_rate
mode: single
alias: Set Super Off Peak in March and April weekdays
description: ""
trigger:
  - platform: time
    at: "10:00:00"
condition:
  - condition: state
    entity_id: calendar.sdge_rates
    state: March and April Super Off Peak
    attribute: message
    enabled: false
  - condition: and
    conditions:
      - condition: template
        value_template: "{{ now().month in [3,4] }}"
      - condition: time
        weekday:
          - mon
          - tue
          - wed
          - thu
          - fri
      - condition: numeric_state
        entity_id: calendar.sdge_holidays
        above: 0
action:
  - service: select.select_option
    data:
      option: SDGE_SuperOffPeak
    target:
      entity_id: select.daily_energy_export
  - service: select.select_option
    data:
      option: SDGE_SuperOffPeak
    target:
      entity_id: select.daily_energy
  - service: input_number.set_value
    data:
      value: 12.38
    target:
      entity_id: input_number.current_electricity_rate
mode: single

After all that, I now get separate readings for each import/export tariff in my Energy dashboard:

I’ve also been putting power meters (mostly zigbee smart outlets) on large devices to track individual consumption. Using this as a starting guide to get the Unaccounted reading and using Powercalc to get a lot of the static measurements into sensors, I had not spent a lot of time with this integration and it’s really powerful.

3 Likes

Thank you for this! I am on the SDGE TOU5 EV plan with Tesla solar (no power wall). I’ve managed to get the basic Energy Dashboard working after creating a few helpers to get the power metric into energy. I’d like to follow your lead and get the proper SDGE rates set up, but I’m not quite following everything you did (I’m fairly new to HA). Would you mind provided just a bit more annotation to your already great post?

It’s not the most straightforward approach, and let me see if I can list everything properly.

  • You need to set up the import and export utility meters by editing your configuration.yaml. Look at the code in my post above.I called them “daily_energy” and “daily_energy_export” and used SDGE_OffPeak, SDGE_SuperOffPeak and SDGE_Peak as the names of the tariffs, but you can use any name. Put the proper source sensor in each one, they will be the increasing sensor of your system’s kWh. Import is what you pull from the grid, export is what you send to the grid. Reboot

  • If you did the above step correctly, you will now have three sensors for each import and export and you need to add those to your energy dashboard. That’s the screenshot right above the code.

  • Create a helper for the rate of each tariff. I created 6, three for summer and three for winter, but it’s probably best to do only three and update their values when the time comes. Otherwise you need to change the rate in six places every time the season changes. This is what I do now, but I might reconsider. Helpers are created in Settings → Devices → Helpers. Create them as input numbers with “input field” as the display mode (I just like this better)

  • Once you have your rate helpers, you need to tell the Energy Dashboard what to use for the cost of the energy, this is done by editing each of the six entries under Electricity Grid. I currently have them set to use an entity with the price. I might fix this up so I don’t have a Winter and Summer option but only one that I can update as we move between seasons. Assign the corresponding rate helper to each import and export tariff. In this example, I assigned Winter Peak Rate Helper to the Daily Import SDGE_Peak consumption entry. I do the same for the export, and the other two get assigned the Super Off Peak and Off Peak rate helpers

  • Define the rate of each helper. I created a dashboard that shows all 6 of my rate helpers so I can easily update them as needed

image

You will then need to create the automations that switch the tariffs depending on time of day, day, etc. The four I posted will do the basics. Peak from 4 to 9, Super Off Peak after midnight, Off Peak after 2 PM and after 9 PM, Off Peak after 6 and before 4 but only on Weekdays, and set Super Off Peak between 10 and 2 PM on weekdays but only in March and April. My automations include setting a value to “Current Electricity Rate” which I use to display the current cost in my dashboard. I should use the rate helpers here, but haven’t updated them yet, so I manually set the cosmetic number

image

I do have an additional component that tracks SDGE Holidays, you’ll see it as a condition in my automations. There were some recent changes on how Holidays are handled so I need to migrate this from the HACS integration I’m using.

It’s a lot, but if you have any questions, let me know. I should put this all on my github with clearer instructions and step by step

Thank you! That’s amazing. I’ll give it a try as soon as I get the Tesla integration working again. It stopped a couple of days ago because Tesla changed the token system (?). I was using Auth for Tesla to get the proper API token, but the app won’t let me log in to make a ‘fleet’ token, whatever that is (but appears to be required now). I’m hoping Auth for Tesla gets updated soon to fix the login bug.

heh, i got slammed by this too. been using the custom one out of hacs so it talks to the thing locally, and tesla ‘just changed the api again’, so all my automations and threshholds and triggers broke.