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

I found the error, the initial condition IF Holiday Sensor if OFF never returns true because the Attributes has workdays and workdays contain mon, tue, etc.
if attributes is empty, then the condition match the sensor status ON/OFF

please confirm

Let me check! Thanks

Confirmed it works. So final code should be:

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

First post updated with new energy tariff automation code that also tracks holidays.
Thanks to brembly!

I still think if the default is needed, just in case there is a failure at least there is a tariff selection

why have my costs dropped?

@AndDG85 please elaborate a little bit
it’s the first time? you just implemented or how log are you using it?
what’s your sensor configuration, do you have and automation?

since now the discussion here was about change the tariff, it seems to me that you add an additional logic

Ciao.
Scusa se riprendo questo post ma sto cercando di capire la logica dietro questa configurazione perchè c’è qualcosa che mi sfugge.
A parte che non mi è chiaro il motivo di utilizzare il calcolo dei giorni del mese corrente per la parte delle spese fisse, te utilizzi delle entità di utility meter per applicarci il prezzo.
Ma il prezzo di ENEL è al kwh (cioè ENERGIA) mentre l’utility meter ragiona per w (ovvero POTENZA).
Sei sicuro che il calcolo fatto sia corretto ? Hai un riscontro reale sui valori della tua bolletta ?

Approfitto per un’altra domanda.
Se io visualizzo i valori sensor.consumo_mensile_f1/2/3, non sono valori corretti.
Presumo che dovrebbe passare in un RIEMANN (left) per tirare fuori valori in kwh, per ottenere dati utili per qualsiasi tipo di analisi.
Ma così facendo perdo la possibilità di resettarne i valori come avviene invece con i “cycle” delle entità di utility meter.
Non so se sono riuscito a spiegare la problematica :sweat_smile:

Qualcuno potrebbe aiutarmi a capire meglio ?
Grazie in anticipo

Ti confermo che l’errore è nell’ordine di 1€ su 2 mesi.
Questo è dovuto alla differente rilevazione di consumi da parte di ENEL rispetto alla mia rilevazione.
Del tutto trascurabile
Volevo monitorare la spesa in tempo reale. Per questo divido le spese fisse sui giorni del mese.
A mezzanotte il conteggio parte dalla quota fissa e poi aggiunge le spese per consumo in tempo reale.
E’ una scelta personale, non so se sia la più sensata, ma pare funzionare.

Ad ogni modo, l’utility meter prende l’unità di misura della sorgente che nel mio caso è in Kwh.

Per quanto riguarda i tuoi sensor.consumo_mensile_f1/2/3 non posso aiutarti, i miei dati arrivano da una Powerwall (in Kwh)

Hi yashijoe,
I’m writing in English just to help anyone else with the same “issue”.
You have created a utility_meter that doesn’t reset at any cycle. Don’t you find that this is a cost that will always accumulate month after month and never resets? Was this decision made on purpose?
Thanks!

Hi aribasadme, yes it’s done on purpose.
In the energy dashboard, data is correctly managed
Thanks

1 Like

are you sure the state of the energy_fixed_costs sensor must include the sensor itself?
shouldn’t it be:

      state: "{{
                 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)
              }}"

Hello brook,

I confirm the code is working.
Most probably there are smarter way to reach the same result. Feel free to test alternative and post your feedbacks

Thanks

what i mean is that this looks like a loop to me:

firstly thank you for this post as it got me started and allowed me to work things out!
I’ve mainly used your code and integrated it with the scraping from here Homeassistant package per tracciamento prezzo tariffa bioraria Servizio Elettrico Nazionale · GitHub , which I further modified to get all the needed values as this is my provider.
then I created all the needed sensors to calculate my electricity bill :wink:

note that I have 2 tariffs (F1 and F23), not 3
so:

created the utility meter from user interface under Helpers as you mentioned in your first post (called “Energy Tariff Bioraria zone” in the following scripts), that gave me:

  • select.energy_tariff_bioraria_zone
  • sensor.energy_tariff_bioraria_zone_f1
  • sensor.energy_tariff_bioraria_zone_f23

then use these 3 files:

  1. the scraping to get all the costs
# ---------------------------------------------------      
# scraping all electricity bill costs

scrape:
  - resource: https://www.servizioelettriconazionale.it/it-IT/tariffe/uso-domestico/biorarie/residente
    sensor:
      - name: "Quota Fissa"
        select: "div.table:nth-child(4) table:nth-child(1) tr:nth-child(2) td:nth-child(2)"
        value_template: >
          {{ value | replace (",",".") }}
        unit_of_measurement: €/mese
      - name: "Tariffa F1"
        select: "div.table:nth-child(6) table:nth-child(1) tr:nth-child(2) td:nth-child(2)"
        value_template: >
          {{ value | replace (",",".") }}
        unit_of_measurement: €/kWh
      - name: "Tariffa F23"
        select: "div.table:nth-child(6) table:nth-child(1) tr:nth-child(3) td:nth-child(2)"
        value_template: >
          {{ value | replace (",",".") }}
        unit_of_measurement: €/kWh
      - name: "Quota Fissa Trasporto"
        select: "div.table:nth-child(9) table:nth-child(1) tr:nth-child(2) td:nth-child(2)"
        value_template: >
          {{ value | replace (",",".") }}
        unit_of_measurement: €/mese
      - name: "Quota Fissa Potenza"
        select: "div.table:nth-child(11) table:nth-child(1) tr:nth-child(2) td:nth-child(2)"
        value_template: >
          {{ value | replace (",",".") }}
        unit_of_measurement: €/kW di potenza
      - name: "Quota Variabile Trasporto"
        select: "div.table:nth-child(13) table:nth-child(1) tr:nth-child(2) td:nth-child(2)"
        value_template: >
          {{ value | replace (",",".") }}
        unit_of_measurement: €/kWh
      - name: "Quota Variabile Oneri"
        select: "div.table:nth-child(13) table:nth-child(1) tr:nth-child(2) td:nth-child(3)"
        value_template: >
          {{ value | replace (",",".") }}
        unit_of_measurement: €/kWh

binary_sensor:
  - platform: workday
    country: IT
    exclude: holiday

sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'date_time_utc'
      - 'date_time_iso'
      - 'time_date'
      - 'time_utc'

# ---------------------------------------------------      
# defining these 2 sensors
# sensor.actual_arera_price = costo tariffa attuale
# sensor.actual_arera_rate = fascia attuale (F1/F23)

  - platform: template
    sensors:
      actual_arera_price:
        value_template: >-
          {% set t = strptime(states('sensor.time'), '%H:%M') %}
          {%- if t.hour >=8 and t.hour <19 and is_state('binary_sensor.workday_sensor', 'on') -%}
            {{ states("sensor.tariffa_f1") | float }}
          {%- elif (t.hour >=19 or t.hour <8) and is_state('binary_sensor.workday_sensor', 'on') -%}
            {{ states("sensor.tariffa_f23") | float }}
          {%- elif is_state('binary_sensor.workday_sensor', 'off') -%}
            {{ states("sensor.tariffa_f23") | float }}
          {%- endif -%}
        friendly_name: Actual ARERA Price
        unit_of_measurement: €/kWh
      actual_arera_rate:
        value_template: >-
          {% set cur_rate = states('sensor.actual_arera_price') | float %}
          {%- if cur_rate == states("sensor.tariffa_f23") | float -%}F23
          {%- elif cur_rate == states("sensor.tariffa_f1") | float -%}F1
          {%- endif -%}
        friendly_name: Actual ARERA Rate
  1. assign the correct tariff to the utility meter and count days in current month
# change the state of "Energy Tariff Bioraria" to F1/F23
# depending on current time and day

automation:
  - alias: Energy Tariff Bioraria
    description: "Cambio fascia F1/F23 in select.energy_tariff_bioraria_zone"
    trigger:
      - platform: time
        id: "8"
        at: "08:00:00"
        variables:
          tariff: F1
      - platform: time
        id: "19"
        at: "19:00:00"
        variables:
          tariff: F23
    condition: []
    action:
      - if:
          - condition: state
            entity_id: binary_sensor.workday_sensor
            state: "off"
        then:
          - service: select.select_option
            target:
              entity_id: select.energy_tariff_bioraria_zone
            data:
              option: F23
        else:
          - choose:
              - conditions:
                  - condition: trigger
                    id: "8"
                sequence:
                  - condition: time
                    weekday:
                      - mon
                      - tue
                      - wed
                      - thu
                      - fri
                  - service: select.select_option
                    target:
                      entity_id: select.energy_tariff_bioraria_zone
                    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_bioraria_zone
                    data:
                      option: "{{ tariff }}"
    mode: single
    
# ---------------------------------------------------      
# count days in current month

command_line:
    - 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
  1. calculate all electricity bill costs and create utility meters
# ---------------------------------------------------      
# create all necessary sensors for energy bill and utility meters

template:

# sensor.bolletta_costi_fissi_giornalieri = Fixed daily prices
# extrapolated from monthly fixed prices
  - trigger:
      - platform: time
        at: "00:00:00"
    sensor:
        - name: Bolletta costi fissi giornalieri
          unique_id: bolletta_costi_fissi_giornalieri
          device_class: "monetary"
          unit_of_measurement: "€"
          state_class: "total"
          icon: mdi:cash
          # quota fissa al mese: sensor.quota_fissa
          # quota fissa trasporto al mese: sensor.quota_fissa_trasporto
          # quota fissa potenza al mese (6 kw)
          # Sconto Bollett@Online: -0.5
          state: >
            {{ (
              states('sensor.quota_fissa') | float(0) / states('sensor.days_in_current_month') | float(0) +
              states('sensor.quota_fissa_trasporto') | float(0) / states('sensor.days_in_current_month') | float(0) +
              (states('sensor.quota_fissa_potenza') | float(0) * 6) / states('sensor.days_in_current_month') | float(0) -
              0.5 / states('sensor.days_in_current_month') | float(0)) *
              1.1
            }}

# sensor.bolletta_tariffa_attuale_costi_v = real current cost
  - sensor:
      - name: "Bolletta tariffa attuale + costi variabili"
        unique_id: bolletta_tariffa_attuale_costi_v
        device_class: "monetary"
        unit_of_measurement: "€/kWh"
        state_class: "total"
        icon: mdi:cash
          # costo materia energia a kWh della fascia attuale: sensor.actual_arera_price
          # quota variabile trasporto a kWh: sensor.quota_variabile_trasporto
          # quota variabile oneri a kWh: sensor.quota_variabile_oneri
          # accisa su materia energia a kwh: 0.022700
          # IVA al 10% sul totale: 1.1 ok
        state: >
          {{ (
            states('sensor.actual_arera_price ') | float(0) +
            states('sensor.quota_variabile_trasporto') | float(0) +
            states('sensor.quota_variabile_oneri') | float(0) +
            0.022700) *
            1.1
          }}

# sensor.bolletta_spesa_f1_costi_v = energy paid in F1
  - sensor:
      - name: 'Bolletta spesa F1 inclusi costi variabili'
        unique_id: bolletta_spesa_f1_costi_v
        device_class: "monetary"
        unit_of_measurement: "€"
        state_class: "total"
        icon: mdi:cash
          # energia consumaga nella fascia F1: sensor.energy_tariff_bioraria_zone_f1
          # costo energia a kWh della fascia F1: sensor.tariffa_f1
          # quota variabile trasporto a kWh: sensor.quota_variabile_trasporto
          # quota variabile oneri a kWh: sensor.quota_variabile_oneri
          # accisa su materia energia a kwh: 0.022700
          # IVA al 10% sul totale: 1.1 ok
        state: >
          {{
            states('sensor.energy_tariff_bioraria_zone_f1') | float(0) * 
            states('sensor.tariffa_f1') | float(0) * 
            (states('sensor.quota_variabile_trasporto') | float(0) +
            states('sensor.quota_variabile_oneri') | float(0) +
            0.022700) *
            1.1
          }}

# sensor.bolletta_spesa_f23_costi_v = energy paid in F23
  - sensor:
      - name: 'Bolletta spesa F23 inclusi costi variabili'
        unique_id: bolletta_spesa_f23_costi_v
        device_class: "monetary"
        unit_of_measurement: "€"
        state_class: "total"
        icon: mdi:cash
          # energia consumaga nella fascia F23: sensor.energy_tariff_bioraria_zone_f23
          # costo energia a kWh della fascia F23: sensor.tariffa_f23
          # quota variabile trasporto a kWh: sensor.quota_variabile_trasporto
          # quota variabile oneri a kWh: sensor.quota_variabile_oneri
          # accisa su materia energia a kwh: 0.022700
          # IVA al 10% sul totale: 1.1 ok
        state: >
          {{
            states('sensor.energy_tariff_bioraria_zone_f23') | float(0) * 
            states('sensor.tariffa_f23') | float(0) * 
            (states('sensor.quota_variabile_trasporto') | float(0) +
            states('sensor.quota_variabile_oneri') | float(0) +
            0.022700) *
            1.1
          }}

# sensor.bolletta_spesa_f1_f23_costi_v = total energy paid in F1 + F23
# (does not include daily fixed costs)
  - sensor:
      - name: 'Bolletta spesa F1 + F23 inclusi costi variabili'
        unique_id: bolletta_spesa_f1_f23_costi_v
        device_class: "monetary"
        unit_of_measurement: "€"
        state_class: "total"
        icon: mdi:cash
        state: >
          {{
            states('sensor.bolletta_spesa_f1_costi_v') | float(0) +
            states('sensor.bolletta_spesa_f23_costi_v') | float(0)
          }}

# sensor.bolletta_spesa_totale = Total energy paid
# (includes fixed daily costs)
  - sensor:
      - name: "Bolletta spesa totale"
        unique_id: bolletta_spesa_totale
        device_class: "monetary"
        unit_of_measurement: "€"
        state_class: "total"
        icon: mdi:cash
        state: >
          {{
            states('sensor.bolletta_spesa_f1_costi_v') | float(0) +
            states('sensor.bolletta_spesa_f23_costi_v') | float(0) +
            states('sensor.bolletta_costi_fissi_giornalieri') | float(0)
          }}


# utility meters for the above sensors
utility_meter:

  bolletta_spesa_f1_costi_v_daily:
    source: sensor.bolletta_spesa_f1_costi_v
    name: Bolletta spesa f1 + costi v daily
    periodically_resetting: false
    cycle: daily
  bolletta_spesa_f1_costi_v_monthly:
    source: sensor.bolletta_spesa_f1_costi_v
    name: Bolletta spesa f1 + costi v monthly
    periodically_resetting: false
    cycle: monthly

  bolletta_spesa_f23_costi_v_daily:
    source: sensor.bolletta_spesa_f23_costi_v
    name: Bolletta spesa f23 + costi v daily
    periodically_resetting: false
    cycle: daily
  bolletta_spesa_f23_costi_v_monthly:
    source: sensor.bolletta_spesa_f23_costi_v
    name: Bolletta spesa f23 + costi v monthly
    periodically_resetting: false
    cycle: monthly

  hourly_bolletta_spesa_totale:
    source: sensor.bolletta_spesa_totale
    name: Bolletta spesa totale hourly
    periodically_resetting: false
    cycle: hourly
  daily_bolletta_spesa_totale:
    source: sensor.bolletta_spesa_totale
    name: Bolletta spesa totale daily
    periodically_resetting: false
    cycle: daily
  monthly_bolletta_spesa_totale:
    source: sensor.bolletta_spesa_totale
    name: Bolletta spesa totale monthly
    periodically_resetting: false
    cycle: monthly

  daily_bolletta_spesa_f1_f23_costi_v:
    source: sensor.bolletta_spesa_f1_f23_costi_v
    name: Bolletta spesa F1 + F23 + costi v daily
    periodically_resetting: false
    cycle: daily
  monthly_bolletta_spesa_f1_f23_costi_v:
    source: sensor.bolletta_spesa_f1_f23_costi_v
    name: Bolletta spesa F1 + F23 + costi v monthly
    periodically_resetting: false
    cycle: monthly

Here is an example of the collected data in a dashboard (modded a layout found online)

Let me know if I messed up something :laughing:
edit: already found a few errors as doing this post

Hello brook, thank you for posting your code. I guess I’m getting inspired by it and will try to make some modifications to mine
I found the automatic update of the costs quite interesting, as I currently revise them manually every month.
Is the service provided by the site stable? Or do they occasionally change the table formatting, requiring the code to be reviewed?
Thanks

I’ve just set it up so cannot answer your question on reliability of the http://servizioelettriconazionale.it webpage.
An alternative could be scraping the https://www.mercatoelettrico.org website as this integration does
GitHub - virtualdj/pun_sensor: Prezzi PUN del mese - Home Assistant Integration

I am with ENEL as my provider. The only costs I modify or control every month are those I have defined as fixed in my code. Meanwhile, the cost of energy itself is always the same for me (zero in F3 and 0.46064 for F1/F2). I didn’t know about the scrape function, and I think I will try to use it.

I will try to create a test sensor with your code, monitor it for a few days, and if everything goes well, I will change the code at the beginning of this post.
Thanks