To keep track of energy cost and consumption in Home Assistant, an excellent tool to use is the Energy Dashboard. One thing a bit tricky with this is to track daily subscription cost. Most subscriptions are monthly, and I have seen some solutions that divide monthly cost by e.g. 30, and set that as daily cost. Another solution is described in the article “Energy dashboard – How to add monthly electricity subscription?“.
I like to have as exact numbers as possible when I scroll through my energy dashboard (Daily, weekly, monthly and yearly). With a fixed number of 30 days per month, there are some minor discrepancies that annoys me. Here is how I have solved this:
- Install the integration Variable+History. Define 3 variables using the integration:
- 1 sensor to hold a running total of subscription fee (fee1). Set initial value to 0.
- 1 dummy sensor to help in adding daily subscription cost at midnight every day (fee2). Set initial value to 0.
- 1 sensor to hold monthly subscription fee. (fee3). Set initial value to monthly cost of subscription.
- Define a template helper (daysincurrentmonth) to hold number of days in current month. This helper can also be used for all subscription fees, etc.:
{% if now().month in [1,3,5,7,8,10,12] %}
31
{% elif now().month in [4,6,9,11] %}
30
{% elif now().month == 2 and ((now().year-2000) % 4 > 0) %}
28
{% elif now().month == 2 and ((now().year-2000) % 4 == 0) %}
29
{% endif %}
- Define a template helper (currentdayfee) that holds current day subscription fee. Set Unit of Measurement to EUR/kWh (or whichever unit that fits your need):
{{ states('sensor.fee3') | float / states('sensor.daysincurrentmonth') | float }}
- Define an Automation that will increase the running subscription fee (fee1):
alias: Update subscription fees
description: ""
triggers:
- at: "00:00:01"
trigger: time
conditions: []
actions:
- action: variable.update_sensor
target:
entity_id: sensor.fee2
data:
replace_attributes: false
value: "{{ states('sensor.fee1') }}"
- delay:
hours: 0
minutes: 0
seconds: 3
milliseconds: 0
- action: variable.update_sensor
target:
entity_id: sensor.fee1
data:
replace_attributes: false
value: >-
{{ states('sensor.fee2') | float +
states('sensor.currentdayfee') | float }}
mode: single
Define a template helper (energysubscription):
- State template: 0
- Unit of measurement: kWh
- Device class: Energy
- State class: Total increasing
In the Energy Dashboard; add an energy source. If you add e.g. electricity as type of source:
- In Consumed energy add the template helper energysubscription.
- Click the radio button “Use an entity to track the total costs”, and add the variable sensor.fee1
The subscription cost will now be accurate per day, per week, per month and per year.
The price for my gas-subscription is yearly, so I’m entering the yearly cost when defining the variable (see above), and dividing by 12 when defining the template sensor currentdayfee:
{{ states('sensor.fee3') | float / 12 / states('sensor.daysincurrentmonth') | float }}
Please see my post about energy sensors defined as template helper.