Total cost energy dashboard configuration (F1 F2 F3 time slots)

I want to have a real time monitoring of the energy costs.
I’ve studied the detailed bill of my provider (ENEL Italy), with the fixed and the variable costs.
Here what you have to do.

A) Create the utility meter from user interface under Helpers.
Call it: Energy Tariff
Cycle: none
Net consumption: no
Delta values: no
Periodically resetting: no

and create 3 tariffs:

F1
F2
F2

Once done you should have 4 new entities:
select.energy_tariff
sensor.energy_tariff_f1
sensor.energy_tariff_f2
sensor.energy_tariff_f3

B) Create the binary sensor for the working days simply by adding native integration.
As exclusion just choose “Holidays”.

C) Create the automation that switches between the 3 tariffs (thanks to website indomus.it and to the user bremby)

automation.yaml

alias: Energy Tariff
description: ""
trigger:
  - platform: time
    id: "7"
    at: "07:00:00"
    variables:
      tariff: F2
  - platform: time
    id: "8"
    at: "08:00:00"
    variables:
      tariff: F1
  - platform: time
    id: "19"
    at: "19:00:00"
    variables:
      tariff: F2
  - platform: time
    id: "23"
    at: "23:00:00"
    variables:
      tariff: F3
condition: []
action:
  - if:
      - condition: state
        entity_id: binary_sensor.workday_sensor
        state: "off"
    then:
      - service: select.select_option
        data:
          option: F3
        target:
          entity_id: select.energy_tariff
    else:
      - choose:
          - conditions:
              - condition: trigger
                id: "7"
            sequence:
              - condition: time
                weekday:
                  - mon
                  - tue
                  - wed
                  - thu
                  - fri
                  - sat
              - service: select.select_option
                target:
                  entity_id: select.energy_tariff
                data:
                  option: "{{ tariff }}"
          - conditions:
              - condition: trigger
                id: "8"
            sequence:
              - condition: time
                weekday:
                  - mon
                  - tue
                  - wed
                  - thu
                  - fri
              - service: select.select_option
                target:
                  entity_id: select.energy_tariff
                data:
                  option: "{{ tariff }}"
          - conditions:
              - condition: trigger
                id: "19"
            sequence:
              - condition: time
                weekday:
                  - mon
                  - tue
                  - wed
                  - thu
                  - fri
              - service: select.select_option
                target:
                  entity_id: select.energy_tariff
                data:
                  option: "{{ tariff }}"
          - conditions:
              - condition: trigger
                id: "23"
            sequence:
              - service: select.select_option
                target:
                  entity_id: select.energy_tariff
                data:
                  option: "{{ tariff }}"
mode: single

D) Now you need to know how many days are there in the current month:
Create a command line with this code (thanks to user kartcon)

command_line.yaml

- sensor:
    name: Days In Current Month
    unique_id: days_in_current_month
    command: cal $(date +"%m %Y") | awk 'NF {DAYS = $NF}; END {print DAYS}'
    scan_interval: 60

E) Create a template sensor that calculates the progressive aggregate cost of your energy.
You need to check your bill details and adjust the values according to what you pay for each time slot and fixed costs.
E.G.: F3 is free for me, while F1 and F2 have the same cost. Also I have 8Kw contract.

template.yaml

- trigger:
    - platform: time
      at: "00:00:00"
  sensor:
    - name: Energy fixed costs
      unique_id: energy_fixed_costs
      device_class: "monetary"
      unit_of_measurement: "EUR"
      state_class: "total"
      icon: mdi:cash
      # commercializzazione al mese: 9.0-0.89765=8.10235
      # quota fissa al mese: 1.72
      # quota potenza impegnata al mese a Kw (8): 1.71*8
      state: "{{
                 states('sensor.energy_fixed_costs') |float(0) +
                 8.10235 / states('sensor.days_in_current_month') |float(0) +
                 1.72 / states('sensor.days_in_current_month') |float(0) +
                 1.71*8 / states('sensor.days_in_current_month') |float(0)
              }}"

- sensor:
    - name: "Energy bill"
      unique_id: 'sensor.energy_bill'
      device_class: "monetary"
      unit_of_measurement: "EUR"
      state_class: "total"
      icon: mdi:cash
      # costo materia energia meno sconto del 30% a kwh in F1: 0.418 * 0.7
      # costo materia energia meno sconto del 30% a kwh in F2: 0.418 * 0.7
      # costo materia energia in F3: 0
      # dispacciamento + sbilanciamento a kwh: 0.01541
      # quota energia a kwh: 0.00848
      # quota energia -meccanismi perequativi- a kwh: 0.00095
      # Asos+Arim a kwh: 0.025014+0.003846=0.02886
      # accisa su materia energia a kwh: 0.0227
      # IVA al 10% sul totale: 1.1
      state: "{{ ((
                 states('sensor.energy_fixed_costs') |float(0) +
                 states('sensor.energy_tariff_f1') |float(0) * 0.418 * 0.7 +
                 states('sensor.energy_tariff_f2') |float(0) * 0.418 * 0.7 +
                 states('sensor.energy_tariff_f3') |float(0) * 0 +
                 (states('sensor.energy_tariff_f1') |float(0) + states('sensor.energy_tariff_f2') |float(0) + states('sensor.energy_tariff_f3') |float(0)) * 0.01541 +
                 (states('sensor.energy_tariff_f1') |float(0) + states('sensor.energy_tariff_f2') |float(0) + states('sensor.energy_tariff_f3') |float(0)) * 0.00848 +
                 (states('sensor.energy_tariff_f1') |float(0) + states('sensor.energy_tariff_f2') |float(0) + states('sensor.energy_tariff_f3') |float(0)) * 0.00095 +
                 (states('sensor.energy_tariff_f1') |float(0) + states('sensor.energy_tariff_f2') |float(0) + states('sensor.energy_tariff_f3') |float(0)) * 0.02886 +
                 (states('sensor.energy_tariff_f1') |float(0) + states('sensor.energy_tariff_f2') |float(0) + states('sensor.energy_tariff_f3') |float(0)) * 0.0227) *
                 1.1) |round(2)
              }}"

F) In the energy dashboard select sensor.energy_bill as the entity that tracks the Total cost

That’s it

Hi @yashijoe, I’m Antonio and I write from Italy. Reading the article in InDomus, I’ve found your work and I’m trying it (some condition for F1, F2, F3). I’ve receive an error when I control the Configuration: The error is located in the last part (the cost): Home Assistant say this:

Configurazione non valida!
Invalid config for [sensor.template]: expected dictionary for dictionary value @ data[‘sensors’][‘device_class’]. Got ‘monetary’ expected dictionary for dictionary value @ data[‘sensors’][‘name’]. Got ‘Costo energia’ expected dictionary for dictionary value @ data[‘sensors’][‘state’]. Got '" {{ ((\n 7.471517 / states(‘sensor.giorni_nel_mese_corrente’) |float(0) * now().day |float(0) +\n 1.72 / states(‘sensor.giorni_nel_mese_corrente’) |float(0) * now().day |float(0) +\n 1.71*8 / states(‘sensor.giorni_nel_mese_corrente’) |float(0) * now().day |float(0) +\n states(‘sensor.consumo_mensile_f1’) |float(0) * 997 +\n states(‘sensor.consumo_mensile_f2’) |float(0) * 998 +\n states(‘sensor.consumo_mensile_f3’) |float(0) * 999 +\n (states(‘sensor.consumo_mensile_f1’)… expected dictionary for dictionary value @ data[‘sensors’][‘state_class’]. Got ‘total_increasing’ expected dictionary for dictionary value @ data[‘sensors’][‘unit_of_measurement’]. Got ‘€’. (See ?, line ?).

Can you help me?

P.S. I’ve put “997”, “998”, “999” only because, verifying the template, I received an error on “XXX”…

Ciao Antonio,
please post your sensor template configuration.
Please use “preformatted text” when pasting the code here.
Thanks

Here the code:

- platform: template
  sensors:
    name: "Costo energia"
    unit_of_measurement: '€'
    device_class: monetary
    state_class: total_increasing
  # commercializzazione al mese: 7.471517
    # quota fissa al mese: 1.72
    # quota potenza impegnato al mese a Kw (8): 1.71*8
    # costo materia energia in F1 a kwh: XXXXX
    # costo materia energia in F2 a kwh: YYYYY
    # costo materia energia in F3 a kwh: ZZZZZ
    # dispacciamento a kwh: 0.017258
    # quota energia a kwh: 0.00848
    # quota energia -meccanismi perequativi- a kwh: 0.000095
    # accisa su materia energia a kwh: 0.0227
    # IVA al 10% sul totale: 1.1
    state: > 
      " {{ ((
        7.471517 / states('sensor.giorni_nel_mese_corrente') |float(0) * now().day |float(0) +
        1.72 / states('sensor.giorni_nel_mese_corrente') |float(0) * now().day |float(0) +
        1.71*8 / states('sensor.giorni_nel_mese_corrente') |float(0) * now().day |float(0) +
        states('sensor.consumo_mensile_f1') |float(0) * 997 +
        states('sensor.consumo_mensile_f2') |float(0) * 998 +
        states('sensor.consumo_mensile_f3') |float(0) * 999 +
        (states('sensor.consumo_mensile_f1') |float(0) + states('sensor.consumo_mensile_f2') |float(0) + states('sensor.consumo_mensile_f3') |float(0)) * 0.017258 +
        (states('sensor.consumo_mensile_f1') |float(0) + states('sensor.consumo_mensile_f2') |float(0) + states('sensor.consumo_mensile_f3') |float(0)) * 0.00848 +
        (states('sensor.consumo_mensile_f1') |float(0) + states('sensor.consumo_mensile_f2') |float(0) + states('sensor.consumo_mensile_f3') |float(0)) * 0.000095 +
        (states('sensor.consumo_mensile_f1') |float(0) + states('sensor.consumo_mensile_f2') |float(0) + states('sensor.consumo_mensile_f3') |float(0)) * 0.0227) *
        1.1) |round(2) 
      }} "

Hope I made in correct way: it’s the first time.
Thank you

Replace

- platform: template
  sensors:

With:

template:
  - sensor:

Anyway the best solution would be to split your configuration file.
e.g. you could add this line to your configuration.yaml:

template: !include templates.yaml

Then you need to create a templates.yaml file and put there the template code:

- sensor:
    - name: "Energy cost"
      unit_of_measurement: '€'
      device_class: monetary
      state_class: total_increasing
etc…

Thank you!!! I have already splatted configuration.yaml, but I haven’t create templates.yaml and I had put the “Energy cost” in sensors.yaml. Now I have the “bill”!
Surely I must study more, I’m at the beginning.
Again, thank you very much.

I’ve fixed the first post codes to adapt them to latest home assistant upgrades

Few codes upgrade and fixes

HI,

I thin that there is some missing conditions in the logic; I don’t understand how you managed sun, sat and holidays. if for sun/sat the easiest way I can imagine is to add a default action, you still need another condition to cover the holidays. so probably the logic should be:

if holidays → F3
else {existing conditions} default action → F3

any comments?

Saturday and Sunday are already in the “fasce orarie energia” automation

About holidays you are right.
I’ve not considered holidays as in Italy we got just 8 days in a year. Not that much.
But I will think about that and adjust the code.

what about something like that

alias: Energy Tariff
description: ""
trigger:
  - platform: time
    id: "7"
    at: "07:00:00"
    variables:
      tariff: F2
  - platform: time
    id: "8"
    at: "08:00:00"
    variables:
      tariff: F1
  - platform: time
    id: "19"
    at: "19:00:00"
    variables:
      tariff: F2
  - platform: time
    id: "23"
    at: "23:00:00"
    variables:
      tariff: F3
condition: []
action:
  - if:
      - condition: state
        entity_id: binary_sensor.holyday_sensor
        attribute: workdays
        state: "Off"
    then:
      - service: select.select_option
        data:
          option: F3
        target:
          entity_id: select.energy_tariff
    else:
      - if:
          - condition: time
            weekday:
              - sat
          - condition: and
            conditions:
              - condition: trigger
                id:
                  - "7"
        then:
          - service: select.select_option
            data:
              option: F2
            target:
              entity_id: select.energy_tariff
        else:
          - choose:
              - conditions:
                  - condition: trigger
                    id: "7"
                sequence:
                  - condition: time
                    weekday:
                      - mon
                      - tue
                      - wed
                      - thu
                      - fri
                      - sat
                  - service: select.select_option
                    target:
                      entity_id: select.energy_tariff
                    data:
                      option: "{{ tariff }}"
              - conditions:
                  - condition: trigger
                    id: "8"
                sequence:
                  - condition: time
                    weekday:
                      - mon
                      - tue
                      - wed
                      - thu
                      - fri
                  - service: select.select_option
                    target:
                      entity_id: select.energy_tariff
                    data:
                      option: "{{ tariff }}"
              - conditions:
                  - condition: trigger
                    id: "19"
                sequence:
                  - condition: time
                    weekday:
                      - mon
                      - tue
                      - wed
                      - thu
                      - fri
                  - service: select.select_option
                    target:
                      entity_id: select.energy_tariff
                    data:
                      option: "{{ tariff }}"
              - conditions:
                  - condition: trigger
                    id: "23"
                sequence:
                  - service: select.select_option
                    target:
                      entity_id: select.energy_tariff
                    data:
                      option: "{{ tariff }}"
            default:
              - service: select.select_option
                data:
                  option: F3
                target:
                  entity_id: select.energy_tariff
mode: single

holyday sensor

workdays:
  - mon
  - tue
  - wed
  - thu
  - fri
  - sat
  - sun
excludes:
  - holiday
days_offset: 0
friendly_name: Holyday Sensor

I think Italy has 12 national holiday days by year + 2 or 3 based on the region/city…

Thank you! Let me test your code for a week, once sure I’ll replace mine in the first post
Next week there will be 1 day of holiday so this is the perfet time to test it.
Few notes:

  • there is already an official integration for the working days
  • F3 holidays are just national holidays according to internet, but please let me know if you find something else
  • italian national holidays are 01.01, 06.01, monday after easter, 01.05, 02.06, 15.08, 01.11, 08.12, 25.12, 26.12

Thanks!

I’m testing your code and also this variation:

alias: Energy Tariff test02
description: ""
trigger:
  - platform: time
    id: "7"
    at: "07:00:00"
    variables:
      tariff: F2
  - platform: time
    id: "8"
    at: "08:00:00"
    variables:
      tariff: F1
  - platform: time
    id: "19"
    at: "19:00:00"
    variables:
      tariff: F2
  - platform: time
    id: "23"
    at: "23:00:00"
    variables:
      tariff: F3
condition: []
action:
  - if:
      - condition: state
        entity_id: binary_sensor.workday_sensor
        attribute: workdays
        state: "off"
    then:
      - service: select.select_option
        data:
          option: F3
        target:
          entity_id: select.energy_tariff_test02
    else:
      - choose:
          - conditions:
              - condition: trigger
                id: "7"
            sequence:
              - condition: time
                weekday:
                  - mon
                  - tue
                  - wed
                  - thu
                  - fri
                  - sat
              - service: select.select_option
                target:
                  entity_id: select.energy_tariff_test02
                data:
                  option: "{{ tariff }}"
          - conditions:
              - condition: trigger
                id: "8"
            sequence:
              - condition: time
                weekday:
                  - mon
                  - tue
                  - wed
                  - thu
                  - fri
              - service: select.select_option
                target:
                  entity_id: select.energy_tariff_test02
                data:
                  option: "{{ tariff }}"
          - conditions:
              - condition: trigger
                id: "19"
            sequence:
              - condition: time
                weekday:
                  - mon
                  - tue
                  - wed
                  - thu
                  - fri
              - service: select.select_option
                target:
                  entity_id: select.energy_tariff_test02
                data:
                  option: "{{ tariff }}"
          - conditions:
              - condition: trigger
                id: "23"
            sequence:
              - service: select.select_option
                target:
                  entity_id: select.energy_tariff_test02
                data:
                  option: "{{ tariff }}"
mode: single

In the meantime, I’ve translate the code in the first post.

hi @yashijoe, I’m using the default integration indeed :smiley: ,

I think you are still missing the default action…

Up to now both codes are giving same results.
Let’s wait the 15th of August

Your code is “Energy Tariff test01”


Please confirm, both codes don’t work
The tariff at 7.00 this morning changed to F2 while it should be F3

Copy that, same error. Checking…

This code seems to work, please check it (change time trigger)

alias: Energy Tariff
description: ""
trigger:
  - platform: time
    id: "7"
    at: "07:00:00"
    variables:
      tariff: F2
  - platform: time
    id: "8"
    at: "08:00:00"
    variables:
      tariff: F1
  - platform: time
    id: "19"
    at: "19:00:00"
    variables:
      tariff: F2
  - platform: time
    id: "23"
    at: "23:00:00"
    variables:
      tariff: F3
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: "7"
        sequence:
          - if:
              - condition: state
                entity_id: binary_sensor.workday_sensor
                state: "off"
            then:
              - service: select.select_option
                data:
                  option: F3
                target:
                  entity_id: select.energy_tariff
            else:
              - condition: time
                weekday:
                  - mon
                  - tue
                  - wed
                  - thu
                  - fri
                  - sat
              - service: select.select_option
                target:
                  entity_id: select.energy_tariff
                data:
                  option: "{{ tariff }}"
      - conditions:
          - condition: trigger
            id: "8"
        sequence:
          - if:
              - condition: state
                entity_id: binary_sensor.workday_sensor
                state: "off"
            then:
              - service: select.select_option
                data:
                  option: F3
                target:
                  entity_id: select.energy_tariff
            else:
              - condition: time
                weekday:
                  - mon
                  - tue
                  - wed
                  - thu
                  - fri
              - service: select.select_option
                target:
                  entity_id: select.energy_tariff
                data:
                  option: "{{ tariff }}"
      - conditions:
          - condition: trigger
            id: "19"
        sequence:
          - if:
              - condition: state
                entity_id: binary_sensor.workday_sensor
                state: "off"
            then:
              - service: select.select_option
                data:
                  option: F3
                target:
                  entity_id: select.energy_tariff
            else:
              - condition: time
                weekday:
                  - mon
                  - tue
                  - wed
                  - thu
                  - fri
              - service: select.select_option
                target:
                  entity_id: select.energy_tariff
                data:
                  option: "{{ tariff }}"
      - conditions:
          - condition: trigger
            id: "23"
        sequence:
          - service: select.select_option
            target:
              entity_id: select.energy_tariff
            data:
              option: "{{ tariff }}"
mode: single
initial_state: true