Home Assistant Dashboard card daily energy cost

I posted this Dashboard on Mastodon and one my followers asked how I made it and more importantly the sensors required. This is how it was done.

It took some research to get this to work. Mainly to stop the value resetting on a reboot. I got some advice on the Home Assistant Community.

Home Assistant Dashboard card showing today's daily electricity cost. The minus is a profit

Before you do anything else you will need to find the sensors that hold the data you require. In the UK we have a daily standing charge on top of the unit cost. I also have an export rate.

To calculate the cost I need to add standing charge to the daily cost and then subtract the export profit.

I use these sensors

  • sensor.electricity_import_daily_cost

    • This comes via the Energy Dashboard. It is hidden in my Home Assistant. Make sure the Energy Dashboard is configured.

  • sensor.electricity_29424_19879_current_standing_charge

    • This comes from my Octopus Energy Integration. I have edited this to hide my meter number

  • sensor.lux_power_to_grid_daily

    • This comes from my Luxpowertek inverter.

sensor.electricity_import_daily_cost sensor hidden in my Home Assistant

sensor.electricity_export_daily_compensation sensor hidden in my Home Assistant

These sensors reset on a Home Assistant restart. This means you need a Template Sensor. I name my Template Sensors with a TS prefix. This code goes in the config.yaml in the template section.

template:
  # Electricity Profit and Cost Template Sensors
  # Today's Agile Export Compensation      
  - trigger:
      - platform: state
        entity_id:
          - sensor.electricity_export_daily_compensation
        to:
      - platform: time
        at: '00:01:00'
    sensor:
      - name: "TS Todays Electricity Export Compensation"
        unique_id: ts_electricity_export_compensation
        state: >
          {{ [this.state | float(0), states('sensor.electricity_export_daily_compensation') | float(0)] | max
            if trigger.platform == 'state' else 0 }}
        unit_of_measurement: 'GBP' 
        device_class: "monetary"
    
    # Today's Import costs
  - trigger:
      - platform: state
        entity_id:
          - sensor.electricity_import_daily_cost
        to:
      - platform: time
        at: '00:00:00'
    sensor:
      - name: "TS Todays Electricity Import Costs"
        unique_id: ts_todays_electricity_import_costs
        state: >
          {{ [this.state | float(0), states('sensor.electricity_import_daily_cost') | float(0)] | max
            if trigger.platform == 'state' else 0 }}
        unit_of_measurement: 'GBP' 
        device_class: "monetary"
    
    # Standing Charge
  - trigger:
      - platform: state
        entity_id:
          - sensor.electricity_29424_19879_current_standing_charge
        to:
    sensor:
      - name: "TS Todays Electricity Standing Charge"
        unique_id: ts_todays_electricity_standing_charge
        state: "{{ states('sensor.electricity_29424_19879_current_standing_charge') | float(0) }}"
        unit_of_measurement: 'GBP' 
        device_class: "monetary"
        
    # Today's Electricity Import Cost with Standing Charge
  - trigger:
      - platform: state
        entity_id:
          - sensor.ts_todays_electricity_import_costs
          - sensor.ts_todays_electricity_standing_charge
        to:
    sensor:
      - name: "TS Todays Total Electricity Cost"
        unique_id: ts_todays_total_electricity_cost
        state: >-
           {{ states('sensor.ts_todays_electricity_import_costs') | float(0) + states('sensor.ts_todays_electricity_standing_charge') | float(0) }}
        unit_of_measurement: 'GBP' 
        device_class: "monetary"
     
    # Today's Fixed Export Cost
  - trigger:
      - platform: state
        entity_id:
          - sensor.lux_power_to_grid_daily
        to:
      - platform: time
        at: '00:01:00'
    sensor:
      - name: "TS Todays Electricity Fixed Export Compensation"
        unique_id: ts_electricity_fixed_export_compensation
        state: >
          {{ states('sensor.lux_power_to_grid_daily') | float(0) *0.15 }}
        #    if trigger.platform == 'state' else 0 }}
        unit_of_measurement: 'GBP' 
        device_class: "monetary"
        
    # Today's Electricity Credit or Debit
  - trigger:
      - platform: state
        entity_id:
          - sensor.ts_todays_total_electricity_cost
          - sensor.ts_todays_electricity_fixed_export_compensation
        to:
    sensor:
      - name: "TS Todays Electricity Credit or Debit"
        unique_id: ts_todays_electricity_credit_or_debit
        state: >-
          {{ states('sensor.ts_todays_total_electricity_cost') | float(0) - states('sensor.ts_todays_electricity_fixed_export_compensation') | float(0) }}
        unit_of_measurement: 'GBP' 
        device_class: "monetary"

This is how it works. I used ChatGPT to make this description.

It sets up an automation trigger.

  • The trigger occurs when the state of sensor.electricity_export_daily_compensation changes or when the time is 00:01:00.

  • When triggered, it updates a sensor entity.

    • The sensor is named "TS Todays Electricity Export Compensation".

    • It has a unique ID of ts_electricity_export_compensation.

    • The state of this sensor is calculated dynamically based on the current state of sensor.electricity_export_daily_compensation.

      • If the trigger is from a state change event (trigger.platform == 'state'), it takes the maximum value between the previous state and the current state of sensor.electricity_export_daily_compensation.

      • If the trigger is from a time event (trigger.platform == 'time'), it sets the state to 0.

    • The unit of measurement for this sensor is set to 'GBP' (British Pound).

    • It is classified as a monetary device class.

In summary, this automation updates a sensor with today's electricity export compensation value. If the compensation changes during the day, it updates the sensor with the maximum compensation value encountered so far. However, if it's triggered by time (00:01:00), it resets the compensation to 0, likely preparing it for the next day.

Notice that TS Todays Electricity Fixed Export Compensation is a calculation using the sensor.lux_power_to_grid_daily. This provides a kWH value. I then multiply that with the static 15p export rate.

You can see the calculation sensors use two sensors. They work like this. I used ChatGPT to make this description.

Trigger: This automation is triggered by changes in the states of two sensors: sensor.ts_todays_total_electricity_cost and sensor.ts_todays_electricity_fixed_export_compensation.

  • Sensor Definition: When triggered, it creates a new sensor named "TS Todays Electricity Credit or Debit" with a unique ID of ts_todays_electricity_credit_or_debit.

    • State Calculation: The state of this sensor is calculated dynamically based on the states of two other sensors:

      states('sensor.ts_todays_total_electricity_cost') | float(0) - states('sensor.ts_todays_electricity_fixed_export_compensation') | float(0)

      This expression subtracts the value of sensor.ts_todays_electricity_fixed_export_compensation from the value of sensor.ts_todays_total_electricity_cost. If either sensor's state is not available, it defaults to 0.

    • Unit of Measurement: The unit of measurement for this sensor is set to 'GBP' (British Pound).

    • Device Class: It's classified as a monetary device class, which indicates that the sensor represents a monetary value.

In summary, this automation calculates the difference between the total electricity cost and the fixed export compensation for today and creates a new sensor to represent this value. It's likely used to track whether there's a credit or debit for electricity usage for the current day.

I then use those sensors to make the cards shown above.

The simple cost card is made like this.

type: entity
entity: sensor.ts_todays_electricity_credit_or_debit
name: Today's Electricity Cost

The doughnut is made with a apexcharts-card. It can’t handle a minus number. If we have not made a profit you will see the cost as a third section

type: custom:apexcharts-card
chart_type: donut
apex_config:
  chart:
    height: 300px
header:
  show: true
  show_states: true
  colorize_states: true
  title: Live Electricity Costs
show:
  last_updated: true
series:
  - entity: sensor.ts_todays_total_electricity_cost
    color: red
    name: Import cost
    type: column
    unit: GBP
    float_precision: 2
  - entity: sensor.ts_todays_electricity_fixed_export_compensation
    show:
      in_chart: true
    color: green
    name: Export profit
    type: column
    float_precision: 2
    unit: GBP
  - entity: sensor.ts_todays_electricity_credit_or_debit
    show:
      in_chart: true
    color: purple
    name: Debit or Credit
    type: column
    float_precision: 2
    unit: GBP

The third card is a simple apexcharts-card graph.

type: custom:apexcharts-card
graph_span: 12h
apex_config:
  chart:
    height: 200px
header:
  show: true
  title: Export profit template
  show_states: true
  colorize_states: true
show:
  last_updated: true
yaxis:
  - decimals: 2
    apex_config:
      tickAmount: 5
series:
  - entity: sensor.ts_todays_electricity_fixed_export_compensation
    show:
      in_chart: true
    color: green
    name: Export profit
    type: column
    float_precision: 2
    unit: GBP
    group_by:
      func: max
      duration: 1h
 
Michael Curtis

My introduction to computers started at my middle school in 1981 when our maths teacher brought in a ZX80. That led the computer club being founded and using a Research Machine 380Z

My first computer was a 48K ZX Spectrum which I loved to programme. Once I left school I worked as a photocopier engineer, then a fax engineer and finally moving on the Apple computers.

For the next 30 years I worked as a system administrator. I now work in the cyber security industry as a Sophos Professional Services consultant

https://www.bazmac.me
Previous
Previous

Recording maximum watts per solar panel string with Home Assistant - Update

Next
Next

How cold affects home battery charging