Live Energy, Cost Sensor

Hello!

Has anyone been able to convert their live energy usage to a live price? For example if i was using 1kWh as that time, it would show me on a sensor that using 1kWh is costing me $0.12c at that time? and would increase and decrease depending on live current usage?

Can someone please show me how this can be done? Thanks!

Hi
What you need to do is find an energy meter that had been integrated into the Home assistant, then you will get the grid consumption in your system.
The price calculation should be done by the HA , not the sensor.
The periodic grid consumption calculation is also done by home assistant
What you need to do is
1 find an energy meter integration in HA that can measure the energy kwh data.(a lot)
2 calculate the periodic energy consumption(daily, monthly) in HA.
3 calculate the periodic energy cost in HA,
(if the electricity price is only a fixed rate,what you need to do is just to multiply the price with the periodic energy consumption dat)

I do it this way. I studied my electricity contract to know which months are - summer vs. winter costs; and when are the peak, part-peak and off-peak hours and hard coded those. The actual price I setup using UI (changes annually).

sensor:
  - platform: template
    sensors:
       current_electricity_cost:
        device_class: monetary
        friendly_name: Current Electricity Cost
        unit_of_measurement: '¢/kWh'
        # summer / winter rates
        value_template: >-
          {% if is_state('sensor.current_electricity_season', 'summer') %} 
            {% if is_state('sensor.current_electricity_rate_type', 'off-peak')%} 
            {{states('input_number.summer_off_peak_rate')}}
            {% elif is_state('sensor.current_electricity_rate_type', 'part-peak')%} 
            {{states('input_number.summer_part_peak_rate')}}
            {% else %}
            {{states('input_number.summer_peak_rate') |float}}
            {% endif %}
          {% else %} 
            {% if is_state('sensor.current_electricity_rate_type', 'off-peak')%} 
            {{states('input_number.winter_off_peak_rate')}}
            {% elif is_state('sensor.current_electricity_rate_type', 'part-peak')%} 
            {{states('input_number.winter_part_peak_rate')}}
            {% else %}
            {{states('input_number.winter_peak_rate') |float}}
            {% endif %}
          {% endif %}
      current_electricity_cost_usd:
        device_class: monetary
        friendly_name: Current Electricity Cost (USD)  
        unit_of_measurement: '$/kWh'
        value_template: >-
            {{states('sensor.current_electricity_cost') |float /100}}
      current_electricity_season:
        friendly_name: Current Electricity Season  
        value_template: >-
          {% if now().month >= 6 and now().month < 10 %} 
          summer
          {% else %} 
          winter
          {% endif %}
      current_electricity_rate_type:
        friendly_name: Current Electricity Rate Type  
        value_template: >-
          {% if now() >= today_at("00:00:00") and now() < today_at("15:00:00")%} 
          off-peak
          {% elif (now() >= today_at("15:00:00") and now() < today_at("16:00:00")) or (now() >= today_at("21:00:00") and now() < today_at("23:59:59"))%} 
          part-peak
          {% else %} 
          peak
          {% endif %}

My UI:

square: false
columns: 1
type: grid
title: Electricity Costs
cards:
  - type: picture-elements
    image: /local/pge_graph.png
    elements:
      - type: conditional
        conditions:
          - entity: sensor.current_electricity_season
            state: summer
        elements:
          - type: state-badge
            entity: input_number.summer_off_peak_rate
            style:
              top: 81%
              left: 35%
              color: transparent
              '--label-badge-red': grey
          - type: conditional
            conditions:
              - entity: sensor.current_electricity_rate_type
                state: off-peak
            elements:
              - type: state-badge
                entity: input_number.summer_off_peak_rate
                style:
                  top: 81%
                  left: 35%
                  color: transparent
                  '--label-badge-red': green
          - type: state-badge
            entity: input_number.summer_part_peak_rate
            style:
              top: 81%
              left: 92.3%
              color: transparent
              '--label-badge-red': grey
          - type: conditional
            conditions:
              - entity: sensor.current_electricity_rate_type
                state: part-peak
            elements:
              - type: state-badge
                entity: input_number.summer_part_peak_rate
                style:
                  top: 81%
                  left: 92.3%
                  color: transparent
                  '--label-badge-red': green
          - type: state-badge
            entity: input_number.summer_peak_rate
            style:
              top: 60%
              left: 77%
              color: transparent
              '--label-badge-red': grey
          - type: conditional
            conditions:
              - entity: sensor.current_electricity_rate_type
                state: peak
            elements:
              - type: state-badge
                entity: input_number.summer_peak_rate
                style:
                  top: 60%
                  left: 77%
                  color: transparent
                  '--label-badge-red': green
      - type: conditional
        conditions:
          - entity: sensor.current_electricity_season
            state: winter
        elements:
          - type: state-badge
            entity: input_number.winter_off_peak_rate
            style:
              top: 81%
              left: 35%
              color: transparent
              '--label-badge-red': grey
          - type: conditional
            conditions:
              - entity: sensor.current_electricity_rate_type
                state: off-peak
            elements:
              - type: state-badge
                entity: input_number.winter_off_peak_rate
                style:
                  top: 81%
                  left: 35%
                  color: transparent
                  '--label-badge-red': green
          - type: state-badge
            entity: input_number.winter_part_peak_rate
            style:
              top: 81%
              left: 92.3%
              color: transparent
              '--label-badge-red': grey
          - type: conditional
            conditions:
              - entity: sensor.current_electricity_rate_type
                state: part-peak
            elements:
              - type: state-badge
                entity: input_number.winter_part_peak_rate
                style:
                  top: 81%
                  left: 92.3%
                  color: transparent
                  '--label-badge-red': green
          - type: state-badge
            entity: input_number.winter_peak_rate
            style:
              top: 60%
              left: 77%
              color: transparent
              '--label-badge-red': grey
          - type: conditional
            conditions:
              - entity: sensor.current_electricity_rate_type
                state: peak
            elements:
              - type: state-badge
                entity: input_number.winter_peak_rate
                style:
                  top: 60%
                  left: 77%
                  color: transparent
                  '--label-badge-red': green
  - type: entities
    entities:
      - entity: input_number.summer_off_peak_rate
        name: Off-Peak
      - entity: input_number.summer_part_peak_rate
        name: Part Peak
      - entity: input_number.summer_peak_rate
        name: Peak
    title: Summer (June...October)
  - type: entities
    entities:
      - entity: input_number.winter_off_peak_rate
        name: Off-Peak
      - entity: input_number.winter_part_peak_rate
        name: Part Peak
      - entity: input_number.winter_peak_rate
        name: Peak
    title: Winter (November...May)

In the looks like this…

The badge turns green to indicate the current electricity price.

I also setup notification in Node-Red to notify my phone 15mins before the off-peak ends.

Maybe this will give you or anybody else ideas…

EDIT: Forgot to include the setup for winter. But it’s the same as for the summer, fairly straightforward.

4 Likes

This is the best setup I’ve seen related to ease of use for differing tariffs. Digging into the sensor it looks like you have the exact same PG&E shenanigans as I do. Do you use this to display total costs on the energy dashboard, or just here in the UI you created?

I would LOVE a hand helping me implement this.

EDIT: Upon further investigation it doesn’t seem like you necessarily have these connected to any sensor monitoring actual energy use do you?

This allows to setup tariffs and provides UI for reference.

You can use sensor.current_electricity_cost_usd from the above when you setup the energy dashboard. For example, I have a clamp that monitors my subpanel, and it’s setup like so…

Where is your utility meter config? will you please mind sharing.
Also, I am having issue with the code, I took from yours and updated per my config.

          {% if is_state('sensor.current_electricity_season', 'summer') %} 
            {% if is_state('sensor.current_electricity_rate_type', 'offpeak')%} 
              {{(states('input_number.summer_offpeak'))}}
            {% elif is_state('sensor.current_electricity_rate_type', 'peak')%} 
              {{(states('input_number.summer_peak'))}}
            {% endif %}
          {% else %} 
            {% if is_state('sensor.current_electricity_rate_type', 'offpeak')%} 
              {{(states('input_number.winter_offpeak'))}}
            {% elif is_state('sensor.current_electricity_rate_type', 'peak')%} 
              {{(states('input_number.winter_peak'))}}
            {% endif %}
          {% endif %}

The output of above is “Unkown”. I think {{(states('input_number.summer/winter_offpeak/peak'))}} is not getting created/updated.
The sensors sensor.current_electricity_season and sensor.current_electricity_rate_type is working and I see the correct output.

My utility meter has tarriffs as peak, offpeak
and switching the tarriff using your code

      current_electricity_rate_type:
        friendly_name: Current Electricity Rate Type  
        value_template: >-
          {% if now() >= today_at("14:00:00") and now() < today_at("18:59:59")%} 
          peak
          {% else %} 
          offpeak
          {% endif %}

Thank You

utility meter is configured via ui in the helper section.

1 Like

Thank you. Your code and idea sparked interest and with community help I was able to make it work.