How to track Italian electricity prices

UPDATE: Check the solution at the bottom of this post, as @andrix noted, there is a better (and correct) custom integration to check the italian energy price! I’m leaving the older post in case it could be helpful to someone trying to scrape data from various websites…

Hi everyone, i wanted to share how i track italian hourly energy price.

First you need to install through HACS the custom integration named “ha-multiscrape”. Once you installed it you need to add the following lines to your configuration.yaml

This will add 24 sensors reporting the energy price for every hour of the day:

multiscrape:
  - resource: 'https://www.mercatoelettrico.org/It/Esiti/MGP/TabellaEsitiMGPPrezzi.aspx'
    scan_interval: 3600
    name: PUN MPG 
    button:
      unique_id: aggiorna_manualmente_pun
      name: Aggiorna PUN 
    log_response: true
    form_submit:
      submit_once: false
      resource: 'https://www.mercatoelettrico.org/It/Tools/Accessodati.aspx?ReturnUrl=%2fIt%2fEsiti%2fMGP%2fTabellaEsitiMGPPrezzi.aspx'
      select: "#form1"
      input:
        'ctl00$ContentPlaceHolder1$CBAccetto1': 'on'
        'ctl00$ContentPlaceHolder1$CBAccetto2': 'on'
        'ctl00$ContentPlaceHolder1$Button1': 'Accetto'
      input_filter:
        - 'ctl00$ContentPlaceHolder1$Button2'
        - 'ctl00$vai'
        - 'ctl00$LinkButton2'
        - 'ctl00$LoginButton'
    sensor:
      - select: '#gvFabbisogno > tr:nth-child(2) > td:nth-child(2)'
        name: 'MGP PUN ore 1'
        unique_id: 'energy_pun_1'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(3) > td:nth-child(2)'
        name: 'MGP PUN ore 2'
        unique_id: 'energy_pun_2'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(4) > td:nth-child(2)'
        name: 'MGP PUN ore 3'
        unique_id: 'energy_pun_3'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(5) > td:nth-child(2)'
        name: 'MGP PUN ore 4'
        unique_id: 'energy_pun_4'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(6) > td:nth-child(2)'
        name: 'MGP PUN ore 5'
        unique_id: 'energy_pun_5'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(7) > td:nth-child(2)'
        name: 'MGP PUN ore 6'
        unique_id: 'energy_pun_6'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(8) > td:nth-child(2)'
        name: 'MGP PUN ore 7'
        unique_id: 'energy_pun_7'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(9) > td:nth-child(2)'
        name: 'MGP PUN ore 8'
        unique_id: 'energy_pun_8'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(10) > td:nth-child(2)'
        name: 'MGP PUN ore 9'
        unique_id: 'energy_pun_9'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(11) > td:nth-child(2)'
        name: 'MGP PUN ore 10'
        unique_id: 'energy_pun_10'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(12) > td:nth-child(2)'
        name: 'MGP PUN ore 11'
        unique_id: 'energy_pun_11'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(13) > td:nth-child(2)'
        name: 'MGP PUN ore 12'
        unique_id: 'energy_pun_12'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(14) > td:nth-child(2)'
        name: 'MGP PUN ore 13'
        unique_id: 'energy_pun_13'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(15) > td:nth-child(2)'
        name: 'MGP PUN ore 14'
        unique_id: 'energy_pun_14'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(16) > td:nth-child(2)'
        name: 'MGP PUN ore 15'
        unique_id: 'energy_pun_15'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(17) > td:nth-child(2)'
        name: 'MGP PUN ore 16'
        unique_id: 'energy_pun_16'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(18) > td:nth-child(2)'
        name: 'MGP PUN ore 17'
        unique_id: 'energy_pun_17'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(19) > td:nth-child(2)'
        name: 'MGP PUN ore 18'
        unique_id: 'energy_pun_18'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(20) > td:nth-child(2)'
        name: 'MGP PUN ore 19'
        unique_id: 'energy_pun_19'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(21) > td:nth-child(2)'
        name: 'MGP PUN ore 20'
        icon: 'mdi:currency-eur'
        unique_id: 'energy_pun_20'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(22) > td:nth-child(2)'
        name: 'MGP PUN ore 21'
        icon: 'mdi:currency-eur'
        unique_id: 'energy_pun_21'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(23) > td:nth-child(2)'
        name: 'MGP PUN ore 22'
        unique_id: 'energy_pun_22'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(24) > td:nth-child(2)'
        name: 'MGP PUN ore 23'
        unique_id: 'energy_pun_23'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: '#gvFabbisogno > tr:nth-child(25) > td:nth-child(2)'
        name: 'MGP PUN ore 0'
        unique_id: 'energy_pun_0'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'

This will add a sensor reporting the current price (thanks @aturri ) :

template:
    - sensor:
        - name: "MGP PUN attuale"
          unit_of_measurement: "€/kWh"
          unique_id: 'energy_pun_current'
          icon: 'mdi:currency-eur'
          state: >
            {{ states('sensor.energy_pun_' + (now().hour | string)) }}

If you want you can set up a min/max helper to get the average price for the current day.
Hope this will help you lowering the energy bill in these hard times.

changelog:

4 Likes

Thanks for sharing your project!
What I would like to achieve is similar.
I need to have the previous month average price for each slot (F1,F2,F3) cause that’s how the energy company calculate the energy price.
Do you have any suggestion? Is there another page to scrape having this kind of information?

Thanks for sharing your project. I made some modifications:

  • I’ve changed “hour 24” to “hour 0”
  • I’ve added unique id to sensors, such as unique_id: 'energy_pun_9'
  • I’ve created a template sensor reporting the current cost:
template:
  - sensor:
      - name: 'MGP PUN attuale'
        unique_id: 'energy_pun_current'
        unit_of_measurement: '€/kWh'
        icon: 'mdi:currency-eur'
        state: >
          {{ states('sensor.energy_pun_' + (now().hour | string)) }}
1 Like

Yes, great project.

I need the same like you…have you resolved?

Hi,
I’ve solved scraping another website to get the information I need.
This is the snippet I’m using:

  - resource: 'https://www.a2aenergia.eu/assistenza/tutela-cliente/indici/indice-pun'
    scan_interval: 3600
    name: PUN MPG Monthly
    sensor:
      - select: 'table > tbody > tr:nth-child(2) > td:nth-child(2)'
        name: 'MGP PUN Last Month F0'
        unique_id: 'energy_pun_last_month_F0'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") | float }}'
      - select: 'table > tbody > tr:nth-child(2) > td:nth-child(3)'
        name: 'MGP PUN Last Month F1'
        unique_id: 'energy_pun_last_month_F1'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") | float }}'
      - select: 'table > tbody > tr:nth-child(2) > td:nth-child(4)'
        name: 'MGP PUN Last Month F2'
        unique_id: 'energy_pun_last_month_F2'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") | float }}'
      - select: 'table > tbody > tr:nth-child(2) > td:nth-child(5)'
        name: 'MGP PUN Last Month F3'
        unique_id: 'energy_pun_last_month_F3'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") | float }}'
      - select: 'table > tbody > tr:nth-child(2) > td:nth-child(6)'
        name: 'MGP PUN Last Month F23'
        unique_id: 'energy_pun_last_month_F23'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") | float }}'
2 Likes

good project, i’m italian too… but there is a problem.
With mercatoelettrico we can know the current PUN, but the useful PUN is the “month” PUN.

Like this: PUN Oggi 2023: il Prezzo dell'Energia Elettrica quanto vale?

the current PUN is useful only for statistics.

Anyway i used your project and made it “better” :slight_smile:

# Energy (PUN)

# Multiscrape
multiscrape:
  # Costi
  # PUN
  - name: 'PUN Orario'
    resource: 'https://www.mercatoelettrico.org/It/Esiti/MGP/TabellaEsitiMGPPrezzi.aspx'
    method: 'GET'
    verify_ssl: true
    log_response: false
    timeout: 10
    scan_interval: 1800
    parser: 'lxml'
    list_separator: ','
    form_submit:
      resource: 'https://www.mercatoelettrico.org/It/Tools/Accessodati.aspx?ReturnUrl=%2fIt%2fEsiti%2fMGP%2fTabellaEsitiMGPPrezzi.aspx'
      select: '#form1'
      input:
        'ctl00$ContentPlaceHolder1$CBAccetto1' : 'on'
        'ctl00$ContentPlaceHolder1$CBAccetto2' : 'on'
        'ctl00$ContentPlaceHolder1$Button1' : 'Accetto'
      input_filter:
        - 'ctl00$ContentPlaceHolder1$Button2'
        - 'ctl00$vai'
        - 'ctl00$LinkButton2'
        - 'ctl00$LoginButton'
      submit_once: false
      resubmit_on_error: true
    sensor:
      - name: 'PUN Orario 1'
        unique_id: 'energy_pun_hour_cost_1'
        select: '#gvFabbisogno > tr:nth-child(2) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 2'
        unique_id: 'energy_pun_hour_cost_2'
        select: '#gvFabbisogno > tr:nth-child(3) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 3'
        unique_id: 'energy_pun_hour_cost_3'
        select: '#gvFabbisogno > tr:nth-child(4) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 4'
        unique_id: 'energy_pun_hour_cost_4'
        select: '#gvFabbisogno > tr:nth-child(5) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 5'
        unique_id: 'energy_pun_hour_cost_5'
        select: '#gvFabbisogno > tr:nth-child(6) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 6'
        unique_id: 'energy_pun_hour_cost_6'
        select: '#gvFabbisogno > tr:nth-child(7) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 7'
        unique_id: 'energy_pun_hour_cost_7'
        select: '#gvFabbisogno > tr:nth-child(8) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 8'
        unique_id: 'energy_pun_hour_cost_8'
        select: '#gvFabbisogno > tr:nth-child(9) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 9'
        unique_id: 'energy_pun_hour_cost_9'
        select: '#gvFabbisogno > tr:nth-child(10) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 10'
        unique_id: 'energy_pun_hour_cost_10'
        select: '#gvFabbisogno > tr:nth-child(11) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 11'
        unique_id: 'energy_pun_hour_cost_11'
        select: '#gvFabbisogno > tr:nth-child(12) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 12'
        unique_id: 'energy_pun_hour_cost_12'
        select: '#gvFabbisogno > tr:nth-child(13) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 13'
        unique_id: 'energy_pun_hour_cost_13'
        select: '#gvFabbisogno > tr:nth-child(14) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 14'
        unique_id: 'energy_pun_hour_cost_14'
        select: '#gvFabbisogno > tr:nth-child(15) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 15'
        unique_id: 'energy_pun_hour_cost_15'
        select: '#gvFabbisogno > tr:nth-child(16) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 16'
        unique_id: 'energy_pun_hour_cost_16'
        select: '#gvFabbisogno > tr:nth-child(17) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 17'
        unique_id: 'energy_pun_hour_cost_17'
        select: '#gvFabbisogno > tr:nth-child(18) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 18'
        unique_id: 'energy_pun_hour_cost_18'
        select: '#gvFabbisogno > tr:nth-child(19) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 19'
        unique_id: 'energy_pun_hour_cost_19'
        select: '#gvFabbisogno > tr:nth-child(20) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 20'
        unique_id: 'energy_pun_hour_cost_20'
        select: '#gvFabbisogno > tr:nth-child(21) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 21'
        unique_id: 'energy_pun_hour_cost_21'
        select: '#gvFabbisogno > tr:nth-child(22) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 22'
        unique_id: 'energy_pun_hour_cost_22'
        select: '#gvFabbisogno > tr:nth-child(23) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 23'
        unique_id: 'energy_pun_hour_cost_23'
        select: '#gvFabbisogno > tr:nth-child(24) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
      - name: 'PUN Orario 24'
        unique_id: 'energy_pun_hour_cost_24'
        select: '#gvFabbisogno > tr:nth-child(25) > td:nth-child(2)'
        value_template: '{{ ( ( value | replace (",", ".") | float(default=0) ) / 1000 | float(default=0) ) | round(6, default=0) | float(default=0) }}'
        force_update: true
        icon: mdi:currency-eur
        unit_of_measurement: '€/kWh'
        device_class: monetary
        state_class: total
    button:
      name: 'Aggiornamento Valori PUN Orario'
  # -------------------------------------------------------------------------------------------------------------------

# Sensors
sensor:
  # Costi
  - platform: template
    sensors:
      # PUN
      energy_pun_current_cost:
        friendly_name: 'PUN Attuale'
        value_template: >-
          {% set energy_pun_current_cost_value = ( states( "sensor.energy_pun_hour_cost_" + ( now().hour | string ) ) | float(default=0) ) %}
          
          {% if energy_pun_current_cost_value == "unknown" or energy_pun_current_cost_value == "unavailable" or ( energy_pun_current_cost_value | float(default=0) <= 0 ) %}
            0.0
          {% else %}
            {{ energy_pun_current_cost_value }}
          {% endif %}
        availability_template: >-
          {% set energy_pun_current_cost_availability = ( states( "sensor.energy_pun_hour_cost_" + ( now().hour | string ) ) | float(default=0) ) %}
          
          {% if energy_pun_current_cost_availability == "unknown" or energy_pun_current_cost_availability == "unavailable" or ( energy_pun_current_cost_availability | float(default=0) <= 0 ) %}
            false
          {% else %}
            true
          {% endif %}
        attribute_templates:
          Orario_1: >-
            {% if is_state("sensor.energy_pun_hour_cost_1", "unknown") or is_state("sensor.energy_pun_hour_cost_1", "unavailable") or ( states("sensor.energy_pun_hour_cost_1") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_1") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_2: >-
            {% if is_state("sensor.energy_pun_hour_cost_2", "unknown") or is_state("sensor.energy_pun_hour_cost_2", "unavailable") or ( states("sensor.energy_pun_hour_cost_2") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_2") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_3: >-
            {% if is_state("sensor.energy_pun_hour_cost_3", "unknown") or is_state("sensor.energy_pun_hour_cost_3", "unavailable") or ( states("sensor.energy_pun_hour_cost_3") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_3") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_4: >-
            {% if is_state("sensor.energy_pun_hour_cost_4", "unknown") or is_state("sensor.energy_pun_hour_cost_4", "unavailable") or ( states("sensor.energy_pun_hour_cost_4") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_4") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_5: >-
            {% if is_state("sensor.energy_pun_hour_cost_5", "unknown") or is_state("sensor.energy_pun_hour_cost_5", "unavailable") or ( states("sensor.energy_pun_hour_cost_5") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_5") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_6: >-
            {% if is_state("sensor.energy_pun_hour_cost_6", "unknown") or is_state("sensor.energy_pun_hour_cost_6", "unavailable") or ( states("sensor.energy_pun_hour_cost_6") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_6") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_7: >-
            {% if is_state("sensor.energy_pun_hour_cost_7", "unknown") or is_state("sensor.energy_pun_hour_cost_7", "unavailable") or ( states("sensor.energy_pun_hour_cost_7") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_7") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_8: >-
            {% if is_state("sensor.energy_pun_hour_cost_8", "unknown") or is_state("sensor.energy_pun_hour_cost_8", "unavailable") or ( states("sensor.energy_pun_hour_cost_8") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_8") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_9: >-
            {% if is_state("sensor.energy_pun_hour_cost_9", "unknown") or is_state("sensor.energy_pun_hour_cost_9", "unavailable") or ( states("sensor.energy_pun_hour_cost_9") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_9") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_10: >-
            {% if is_state("sensor.energy_pun_hour_cost_10", "unknown") or is_state("sensor.energy_pun_hour_cost_10", "unavailable") or ( states("sensor.energy_pun_hour_cost_10") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_10") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_11: >-
            {% if is_state("sensor.energy_pun_hour_cost_11", "unknown") or is_state("sensor.energy_pun_hour_cost_11", "unavailable") or ( states("sensor.energy_pun_hour_cost_11") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_11") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_12: >-
            {% if is_state("sensor.energy_pun_hour_cost_12", "unknown") or is_state("sensor.energy_pun_hour_cost_12", "unavailable") or ( states("sensor.energy_pun_hour_cost_12") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_12") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_13: >-
            {% if is_state("sensor.energy_pun_hour_cost_13", "unknown") or is_state("sensor.energy_pun_hour_cost_13", "unavailable") or ( states("sensor.energy_pun_hour_cost_13") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_13") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_14: >-
            {% if is_state("sensor.energy_pun_hour_cost_14", "unknown") or is_state("sensor.energy_pun_hour_cost_14", "unavailable") or ( states("sensor.energy_pun_hour_cost_14") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_14") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_15: >-
            {% if is_state("sensor.energy_pun_hour_cost_15", "unknown") or is_state("sensor.energy_pun_hour_cost_15", "unavailable") or ( states("sensor.energy_pun_hour_cost_15") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_15") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_16: >-
            {% if is_state("sensor.energy_pun_hour_cost_16", "unknown") or is_state("sensor.energy_pun_hour_cost_16", "unavailable") or ( states("sensor.energy_pun_hour_cost_16") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_16") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_17: >-
            {% if is_state("sensor.energy_pun_hour_cost_17", "unknown") or is_state("sensor.energy_pun_hour_cost_17", "unavailable") or ( states("sensor.energy_pun_hour_cost_17") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_17") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_18: >-
            {% if is_state("sensor.energy_pun_hour_cost_18", "unknown") or is_state("sensor.energy_pun_hour_cost_18", "unavailable") or ( states("sensor.energy_pun_hour_cost_18") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_18") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_19: >-
            {% if is_state("sensor.energy_pun_hour_cost_19", "unknown") or is_state("sensor.energy_pun_hour_cost_19", "unavailable") or ( states("sensor.energy_pun_hour_cost_19") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_19") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_20: >-
            {% if is_state("sensor.energy_pun_hour_cost_20", "unknown") or is_state("sensor.energy_pun_hour_cost_20", "unavailable") or ( states("sensor.energy_pun_hour_cost_20") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_20") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_21: >-
            {% if is_state("sensor.energy_pun_hour_cost_21", "unknown") or is_state("sensor.energy_pun_hour_cost_21", "unavailable") or ( states("sensor.energy_pun_hour_cost_21") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_21") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_22: >-
            {% if is_state("sensor.energy_pun_hour_cost_22", "unknown") or is_state("sensor.energy_pun_hour_cost_22", "unavailable") or ( states("sensor.energy_pun_hour_cost_22") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_22") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_23: >-
            {% if is_state("sensor.energy_pun_hour_cost_23", "unknown") or is_state("sensor.energy_pun_hour_cost_23", "unavailable") or ( states("sensor.energy_pun_hour_cost_23") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_23") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
          Orario_24: >-
            {% if is_state("sensor.energy_pun_hour_cost_24", "unknown") or is_state("sensor.energy_pun_hour_cost_24", "unavailable") or ( states("sensor.energy_pun_hour_cost_24") | float(default=0) <= 0 ) %}
              Non Disponibile
            {% else %}
              {{ states("sensor.energy_pun_hour_cost_24") | replace (".", ",") | default("Errore") }} €/kWh
            {% endif %}
        icon_template: mdi:chart-bar
        unit_of_measurement: '€/kWh'
        device_class: monetary
  # -------------------------------------------------------------------------------------------------------------------

3 Likes

is energy_pun_current_cost the monthly one?

only daily\hourly. There is no way to know the real monthly italian PUN at today.

ciao ragazzi, anche io vorrei avere il valore del PUN medio, in realtà sul sito c’è, ma non so come fare ad estrarlo :frowning:
la pagina dove trovarlo è questa:
https://www.mercatoelettrico.org/It/Statistiche/ME/DatiSintesi.aspx
cè la media mensile (parziale del mese corrente) e giornaliera.
quella oraria secondo me è abbastanza inutile.
se riuscite ad integrarla nei sensori sarebbe fantastico! o almeno spiegarmi come fare… grazie

Che ne pensate di questa integrazione?

1 Like

Ciao! Io ho provato ad installarla ma non la trovo da nessuna parte,non trovo dove configurarla…un fantasma in pratica :sweat_smile:
Tu ci sei riuscito a farla funzionare?

Ciao, si, l’ho installata e funziona a meraviglia.
Devi aggiungere in HACS la repository,come da istruzioni:

Installare usando HACS tramite il menu con i tre puntini nell’angolo in alto a destra e scegliendo Add custom repository e aggiungendo l’URL GitHub - virtualdj/pun_sensor: Prezzi PUN del mese - Home Assistant Integration alla lista.

Ciao
Andrea

Dopo aver installato con hacs, e riavviato, bisogna ancora andare nelle integrazioni e aggiungerla (con le opzioni che si desidera) da lì.
A quel punto compaiono le entità correlate.

esatto, era proprio questo passaggio che mi mancava
comunque neanche questo dà il valore che voglio io…
sto provando a sniffare la pagina che interessa a me e sembra funzionare :slight_smile:
sono riuscito a prendere tutte le medie mensili (creando 12 sensori), ora devo capire come creare un ulteriore sensore me mi vada a leggere il valore del sensore del mese corrente.
spero di essermi spiegato :sweat_smile:

Onestamente no, non ti seguo.
Sicuramente avrai un buon motivo per acquisire tutte le medie mensili passate (per curiosità chiedo: quale motivo?).
Peró la media corrente che vedo io è esattamente quella del sito che hai riportato tu.

Hai spuntato la casella in fase di configurazione?
Cito la pagina del dev:

Se la casella di controllo Usa solo dati reali ad inizio mese è attivata all’inizio del mese quando non ci sono i prezzi per tutte le fasce orarie questi vengono disabilitati (non viene mostrato quindi un prezzo in €/kWh finché i dati non sono sufficiente); nel caso invece la casella fosse disattivata (default) nel conteggio vengono inclusi gli ultimi giorni del mese precedente in modo da avere sempre un valore in €/kWh.

Il motivo che mi son preso tutti i mesi è per il semplice fatto che una volta che ho capito come fare ,me li sono portati tutti in HA, e poi li mi sarà più facile gestirli.
In realtà a me ne basterebbero due… O addirittura uno, ma non saprei come far cambiare la lettura al cambio mese.
Tipo adesso a me interessa avere la media di giugno (che ovviamente è parziale) per avere un idea di come sta andando il mercato , e la media di maggio per calcolare i costi del mese appena passato.

No, quella casella non ho provato a spuntarla… Questa sera ci provo.

A te escono esattamente le medie della mia pagina?

Confermo che una volta spuntata quella casella sia l’altro giono che oggi il sensore “sensor.pun_mono_orario” mi da lo stesso valore che leggo per il mese di Giugno alla pagina
https://www.mercatoelettrico.org/It/Statistiche/ME/DatiSintesi.aspx
Stessi valori fino all’ultimo decimale.

Per il fatto di avere il valore del mese precedente, forse si riesce a farlo semplicemente con un helper…ma non c’ho ancora pensato…

Ciao

1 Like

ottimo, allora abbandono la mia strada e uso quell’integrazione. grazie :slight_smile:

1 Like

Let me share for Turkish energy prices:

Water (for Istanbul): (in multiscrape.yaml)

- resource: https://www.iski.gov.tr/web/tr-TR/musteri-hizmetleri/su-birim-fiyatlari1
  scan_interval: 28800
  timeout: 15
  sensor:
    - unique_id: water_low_price
      name: Water Low Price
      unit_of_measurement: "TL/m3"
      icon: mdi-water
      select: "table > tbody > tr:nth-child(3) > td:nth-child(2) > p"
      value_template: '{{ value.split("\n\t\t\t\t\t")[1].replace(",",".") }}'
    - unique_id: water_high_price
      name: Water High Price
      unit_of_measurement: "TL/m3"
      icon: mdi-water
      select: "table > tbody > tr:nth-child(4) > td:nth-child(2) > p"
      value_template: '{{ value.split("\n\t\t\t\t\t")[1].replace(",",".") }}'

Electricity (all country across) (in multiscrape.yaml using multiscrape)

- resource: https://www.encazip.com/elektrik-fiyatlari
  scan_interval: 28800
  timeout: 15
  sensor:
    - unique_id: elk_aktif_enerji_dusuk_birim_fiyatı
      name: Elektrik Aktif Enerji Düsük Birim Fiyatı
      unit_of_measurement: "TL/kwh"
      icon: mdi-home-lightning-bolt
      select: ".content-table > tbody:nth-child(2) > tr:nth-child(1) > td:nth-child(2)"
      value_template: '{{ value.split(" TL/kWh")[0] }}'
    - unique_id: elk_aktif_enerji_yuksek_birim_fiyatı
      name: Elektrik Aktif Enerji Yüksek Birim Fiyatı
      unit_of_measurement: "TL/kwh"
      icon: mdi-home-lightning-bolt
      select: ".content-table > tbody:nth-child(2) > tr:nth-child(2) > td:nth-child(2)"
      value_template: '{{ value.split(" TL/kWh")[0] }}'
    - unique_id: elk_dagitim_bedeli_birim_fiyatı
      name: Elektrik Dağıtım Bedeli Birim Fiyatı
      unit_of_measurement: "TL/kwh"
      icon: mdi-home-lightning-bolt
      select: ".content-table > tbody:nth-child(2) > tr:nth-child(1) > td:nth-child(3)"
      value_template: '{{ value.split(" TL/kWh")[0] }}'

Gas ( for only igdas istanbul) (in sensor.yaml using rest)

make sure you enter your installation_number and contrat_number

- platform: rest
  resource_template: https://www.igdas.istanbul/WebServices/duzeltmekatsayisi.aspx?inst={installation_number}&contract={contrat_number}&date1={{(now()-timedelta(days=1)).strftime('%Y-%m-%d')}}&date2={{now().strftime('%Y-%m-%d')}}
  scan_interval: 3600
  name: Gaz Fiyatlar
  value_template: "Gas_Price"
  json_attributes:
    - UstIsilDeger
    - KFaktoru
    - OrtalamaBirimFiyat
    - ErrorCode

(Sorry for resuscitating an old thread :stuck_out_tongue: )

I’m using the integration and I really like it, but I have the need to pull the hourly PUN values to perform (through another add-on) some energy optimization.
So the multiscrape approach is what I needed and I found the example above really helpful.
If compared to that I’m using the XML approach as it allows me to easily query any day by relying on templates.
I’m pasting my solution here if anybody else needs it and linking the thread were I got some support on how to use the multiscrape (maybe there will be some evolution in the discussion and it may still be useful).

EDIT: I updated the resources to make the code work with the old GME site till 31st Dec.

multiscrape:
  - resource_template: 'https://storico.mercatoelettrico.org/It/WebServerDataStore/MGP_Prezzi/{{ as_timestamp(now()) | timestamp_custom("%Y%m%d", True) }}MGPPrezzi.xml'
    scan_interval: 3600
    parser: 'lxml'
    name: 'PUN oggi'
    button:
      unique_id: 'aggiorna_misure_pun_oggi'
      name: 'Aggiorna misure PUN oggi'
    log_response: true
    form_submit:
      submit_once: false
      resource: 'https://storico.mercatoelettrico.org/It/Tools/Accessodati.aspx'
      select: '#form1'
      input:
        'ctl00$ContentPlaceHolder1$CBAccetto1': 'on'
        'ctl00$ContentPlaceHolder1$CBAccetto2': 'on'
        'ctl00$ContentPlaceHolder1$Button1': 'Accetto'
      input_filter:
        - 'ctl00$ContentPlaceHolder1$Button2'
        - 'ctl00$vai'
        - 'ctl00$LinkButton2'
        - 'ctl00$LoginButton'
    sensor:
      - select: 'NewDataSet:nth-child(1) > Prezzi:nth-child(2) > PUN:nth-child(4)'
        name: 'PUN oggi 00'
        unique_id: 'pun_oggi_00'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'
      - select: 'NewDataSet:nth-child(1) > Prezzi:nth-child(3) > PUN:nth-child(4)'
        name: 'PUN oggi 01'
        unique_id: 'pun_oggi_01'
        icon: 'mdi:currency-eur'
        unit_of_measurement: '€/kWh'
        value_template: '{{ value | replace (",", ".") |float | int / 1000}}'

2 Likes