How to make a card in Lovelace that tells you How much You’ll pay on the electricity bill

Yes the utility meter was only just created.

But your off peak is reading zero which seems unrealistic to me. So is the automation working? I see the peak graph flatlining at the right times, but the off peak is not changing.

Im not sure what I am doing wrong, it shows a cost, but doesnt seem to be tracking it correctly, and doesnt reset at midnight. I am in the UK, trying to implement this for a daily cost:

input_number:
  kwh_cost:
    name: kwh_cost
    initial: 0.35
    min: 0.34
    max: 0.35
  standing_charge:
    name: standing_charge
    initial: 0.42
    min: 0.41
    max: 0.42

utility_meter:
  electricity_imported_billing_day:
    source: sensor.house_power_power
    name: Electricity Imported Billing Day
    cron: 0 0 * * *

sensor:
  - platform: template
    sensors:
      monthly_cost_tracked:
         friendly_name: "Energy Cost Daily"
         unique_id: energy_cost_daily
         unit_of_measurement: "£"
         value_template: >-
           £{{((states('sensor.electricity_imported_billing_day')|float(default=0)*states('input_number.kwh_cost')|float(default=0)) + (states('input_number.standing_charge')|float(default=0)*now().day|int(default=0)))|round(2)}}

i dont think the automation is running properly checking the YAML has errors

alias: switch peak offpeak
description: ""
trigger:
  - platform: time
    at: "05:30:00"
  - platform: time
    at: "23:30:00"
  - platform: homeassistant
    event: start
condition: []
action:
  - service: select.select_option
    target:
      entity_id:
        - select.electricity_imported_power_daily
    data: "{% set t = now() %}  {%- if t.hour < 5 or (t.hour = 5 and t.minute < 30) or (t.hour = 23 and t.minute >= 30) %}\n  off-peak\n{%- else -%}\n  peak\n{%-endif -%}"
mode: single

Yes you need to fix your data line. See the example I gave you earlier.

You have a daily utility meter but your template sensor multiplies the daily charge by the day of the month so it’s increasing every day and not going back to 0

Thank you, but i’m unsure of how to fix that, as the line is quite confusing. Could you point me in the right direction please?

Remove this. It’s multiplying your daily charge by the day of the month which doesn’t make sense for a daily sensor. Today for example that is equal to 30 as it’s the 30th or March.

if i use your code of

action:
  - service: select.select_option
    data:
      option: >-
        {% set t = now() %}  {%- if t.hour < 5 or (t.hour = 5 and t.minute < 30) or (t.hour = 23 and t.minute >= 30) %}
          off-peak
        {%- else -%}
          peak
        {%- endif -%}

into my code which would become

alias: switch peak offpeak
description: ""
trigger:
  - platform: time
    at: "05:30:00"
  - platform: time
    at: "23:30:00"
  - platform: homeassistant
    event: start
condition: []
action:
  - service: select.select_option
    target:
      entity_id:
        - select.electricity_imported_power_daily
      data:
      option: >-
        {% set t = now() %}  {%- if t.hour < 5 or (t.hour = 5 and t.minute < 30) or (t.hour = 23 and t.minute >= 30) %}
          off-peak
        {%- else -%}
          peak
        {%- endif -%}
mode: single

i get the error of

Message malformed: extra keys not allowed @ data[‘action’][0][‘target’][‘data’]

Here’s mine - works perfectly (and has been for over a year!)

  - platform: template
    sensors:
      daily_cost_tracked3:
         friendly_name: "Electricity Cost Daily - Raw"
         unique_id: electricity_cost_daily_raw
         unit_of_measurement: "£"
         value_template: >-
           {{((states('sensor.energy_usage_daily')|float(default=0)*states('input_number.kwh_cost')|float(default=0)) + states('input_number.standing_charge')|float(default=0))|round(2)}}

…and here’s the monthly calculation (gas and electric)

  - platform: template
    sensors:
      monthly_cost_tracked:
         friendly_name: "Energy Cost Monthly"
         unique_id: energy_cost_monthly
         value_template: >-
           £{{((states('sensor.energy_usage_monthly')|float(default=0)*states('input_number.kwh_cost')|float(default=0)) + (states('input_number.standing_charge')|float(default=0)*now().day|int(default=0))+ (states('sensor.gas_usage_monthly')|float(default=0)*states('input_number.gas_kwh')|float(default=0)) + (states('input_number.gas_standing_charge')|float(default=0)*now().day|int(default=0)))|round(2)}}

Data is indented one tab too far to the right. It should line up with target.

This is what tells you to look for data. It says under action, target you have a key data which shouldn’t be there.

I’m basically tryiung to get a daily electricity usage costing.
I am using the below config (with a Zigbee frient Electricity Meter Interface). In the interface, card, if i use ‘Electricity Cost Daily - Raw’, it just shows 42p constantly. If I use ‘sensor Cost’, the cost increases, but doesnt reset. In the config, I dont really want a min / max, just an electricity cost per unit and a standing charge. What am I doing wrong please?

input_number:
  kwh_cost:
    name: kwh_cost
    initial: 0.35
    min: 0.34
    max: 0.35

  standing_charge:
    name: standing_charge
    initial: 0.42
    min: 0.41
    max: 0.42

utility_meter:
  electricity_imported_billing_day:
    source: sensor.house_power_power
    name: Electricity Imported Billing Day
    cron: 0 0 * * *
    
sensor:
  - platform: template
    sensors:
      daily_cost_tracked3:
         friendly_name: "Electricity Cost Daily - Raw"
         unique_id: electricity_cost_daily_raw
         unit_of_measurement: "£"
         value_template: >-
           {{((states('sensor.energy_usage_daily')|float(default=0)*states('input_number.kwh_cost')|float(default=0)) + states('input_number.standing_charge')|float(default=0))|round(2)}}

Any help would be greatly appreciated.

I can’t see a lot different in how you’re doing it compared to mine apart from a missing device class perhaps?

Sample of mine below (3 tariff periods) - formatting a bit different as it’s in a separate sensors.yaml file.

# Current Daily Cost
     energy_current_daily_kwh_running_cost:
       friendly_name: "Current Daily Cost"
       unit_of_measurement: "$"
       device_class: monetary
       value_template: >-
         {{ (
         ( states( 'sensor.energy_import_kwh_daily_off_peak_usage' ) | float * states( 'input_number.energy_charges_price_import_off_peak_usage' ) | float )
         +
         (states( 'sensor.energy_import_kwh_daily_peak_usage' ) | float * states( 'input_number.energy_charges_price_import_peak_usage' ) | float )
         +
         (states( 'sensor.energy_import_kwh_daily_shoulder_usage' ) | float * states( 'input_number.energy_charges_price_import_shoulder_usage' ) | float )
         ) | round(2) }}
       attribute_templates:
         state_class: measuring

You seem to be using some other input here.

Your daily imported power utility meter is electricity_imported_billing_day

Thank you, It now seems to count correctly, but does noit reset every night, I have:

utility_meter:
  electricity_imported_billing_day:
    source: sensor.house_power_power
    name: Electricity Imported Billing Day
    cron: 0 0 * * *

Am I missing something somehwere?

@Neil_Brownlee thanks for this, this is exactly what I am looking for but I am not sure where you would use this code? Does this need to be created as a helper? I tried to put it in my config.yaml but it doesn’t like it? Unless I am putting it in the wrong place? Obviously I know I need to update it with my own sensors.

You can do it as template helper now - just paste the value_template value into the helper.

perfect! thanks, I got it working… Thanks for your help!

1 Like