Energy: Add fixed daily cost

It’s been a while since my original post and it seems a few people are having trouble implementing my/others solution particularly for differing periods, so I thought I’d revisit it hopefully making things a bit easier. My config is below.

# Minimal configuration of the standard time and date sensor
sensor:
  - platform: time_date
    display_options:
      - 'date_time_iso'

template:
  - sensor: 
    - name: "Supply Charge"
      unit_of_measurement: "Wh"
      device_class: energy
      state_class: total_increasing
      state: >-
        {{ int(as_timestamp(now()) | timestamp_custom("%d")) + 1 - int(as_timestamp("yyyy-mm-dd") | timestamp_custom("%d")) }}

To make this work you need to know your supply charge cost and period.
In the example above the %d is for daily. If you have a differing period you can use any one of the
python date format codes here Python Dates
eg if your charge period is

  • hourly use %H (Hour (00-23))
  • daily use %d or %j
  • weekly use %V (ISO 8601 weeknumber)
  • monthly use %m (Month as a number 01-12)
  • yearly use %y or %Y
    etc…

Make the change to the code for your charge period (note it must be changed in 2 places)
Change the yyyy-mm-dd to todays date eg 2023-07-05 for 5th July 2023.
Insert the above code into your configuration.yaml
Goto Developer Tools - YAML and click All YAML Configuration to reload the config
Goto Settings - Dashboards - Energy - Add Consumption
Select Supply Charge sensor from the drop down list
Select Use a static price and enter your supply charge for the period multiplied by 1000

That’s it. The supply charge should show up in the Energy dashboard and change every supply period.
Note the data may take a while to show up in the dashboard

Hope this helps people

4 Likes