#Restructuring a SPOT energy price sensor to be readable by Predbat.

1 messages · Page 1 of 1 (latest)

sleek mural
#

I wish to add a new sensor, that has the current spot price and the raw_today and raw_tomorrow data so it can be read by predbat.
Is something this complex possible with a template?

AI claims it can be done with a sensor.yaml file, but with no sucess.

"2025-07-01T00:00:00+02:00": 6.2239578
"2025-07-01T01:00:00+02:00": 5.8034949000000005
...
unit_of_measurement: Kč/kWh
icon: mdi:cash-minus
friendly_name: Current Buy Electricity Price```


```New sensor structure sensor.nordpool_kwh_dk1_eur_3_10_025:
state_class: total
average: 0.172875
off_peak_1: 0.1255
off_peak_2: 0.40049999999999997
peak: 0.12858333333333333
min: 0.053
max: 0.595
mean: 0.12
unit: kWh
currency: EUR
country: Denmark
region: DK1
low_price: true
price_percent_to_average: 0.6073752711496746
today:
  - 0.139
  - 0.119
  - 0.111
  - 0.11
  - 0.113
  - 0.121
  - 0.141
  - 0.15
  - 0.143
  - 0.114
  - 0.105
  - 0.087
  - 0.056
  - 0.053
  - 0.072
  - 0.098
  - 0.113
  - 0.128
  - 0.19
  - 0.384
  - 0.595
  - 0.509
  - 0.313
  - 0.185
tomorrow: []
tomorrow_valid: false
raw_today:
  - start: "2025-07-01T00:00:00+02:00"
    end: "2025-07-01T01:00:00+02:00"
    value: 0.139
  - start: "2025-07-01T01:00:00+02:00"
    end: "2025-07-01T02:00:00+02:00"
    value: 0.119
...
raw_tomorrow: []
current_price: 0.105
additional_costs_current_hour: 0
price_in_cents: false
unit_of_measurement: EUR/kWh
device_class: monetary
icon: mdi:flash
friendly_name: nordpool_kwh_dk1_eur_3_10_025```


Full sensor files:
https://gist.github.com/ziat007/286999e2d0ccd3dd4360756176419578

Predbat data reader script:
https://gist.github.com/ziat007/35d7e1ebe9db26d6b27019b0d4e57a50
Gist

GitHub Gist: instantly share code, notes, and snippets.

Gist

GitHub Gist: instantly share code, notes, and snippets.

obtuse bison
#

I can't make sense of any of those files but you probably want to use a template sensor

sleek mural
obtuse bison
#

I get that

#

but what does that have to do with the files you shared?

#

what data do you currently have availble in home assistant? so if you go to developer-tools -> states, what part here is relevant for you?

sleek mural
#

as it is a future reading, it is stored in attributes.

Predbat reads it from attributes, but it requires to be in a specifc format

#

so I need to read the attribute data -> round it down to 2 decimal points or so -> restructure the data -> write it to a new sensor into attributes. (raw_today:...)

#

the next days data gets added around 2pm each day (raw_tomorrow) in the new sensor

#

the other option would be creating a new script in pedbat that reads the data from the current sensor

obtuse bison
#

for example, this is energy zero:

#

which returns:

prices:
  - timestamp: "2025-07-02 22:00:00+00:00"
    price: 0.1
  - timestamp: "2025-07-02 23:00:00+00:00"
    price: 0.1
  - timestamp: "2025-07-03 00:00:00+00:00"
    price: 0.1
  - timestamp: "2025-07-03 01:00:00+00:00"
    price: 0.1
  - timestamp: "2025-07-03 02:00:00+00:00"
    price: 0.1
  - ...
sleek mural
# obtuse bison there's also: https://springfall2008.github.io/batpred/energy-rates/#energidatas...

that is the one I am trying to convert the prices into.

It does not provide spot prices for CZ.

current (only) integration I know of that reads data from CZ spot.
https://github.com/rnovacek/homeassistant_cz_energy_spot_prices

GitHub

Home Assistant integration that provides current Czech electricity spot prices based on OTE. - rnovacek/homeassistant_cz_energy_spot_prices

obtuse bison
sleek mural
#

yes, that is the test sensor

obtuse bison
#

Configuring Predbat to Use the Energidataservice Integration

The following configuration items in apps.yaml are used to configure Predbat to use the Energidataservice integration. These items must be set explicitly to ensure that Predbat retrieves the correct import and export rates.

metric_energidataservice_import - Import rates from the Energidataservice integration. This should point to the sensor that provides hourly import rates, such as sensor.energi_data_service.

metric_energidataservice_export - Export rates from the Energidataservice integration. This should point to the sensor that provides hourly export rates (e.g., solar feed-in rates), such as sensor.energi_data_service_export.

so this doesnt work?

sleek mural
#

I am trying to get my SPOT rates into predbat. Czech Republic. It is not avalable in Energidataservice or nordpool

obtuse bison
#

then why are you sharing a script for energydataservice?

sleek mural
obtuse bison
#

attributes are the wrong way to do it, but if that's what predbat wants then you have to make a template sensor like I said before

sleek mural
#

When I add this code it does not create a new entity.

`template:

  • sensor:
    • name: "Current Buy Electricity Price Raw"
      unique_id: current_buy_electricity_price_raw
      state: "{{ states('sensor.current_buy_electricity_price') }}" # Copies the current state of the original sensor
      unit_of_measurement: "{{ state_attr('sensor.current_buy_electricity_price', 'unit_of_measurement') }}"
      icon: "{{ state_attr('sensor.current_buy_electricity_price', 'icon') }}"`

location:
/templates/SpotElectricityRawSell.yaml

config:
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
sensor: !include_dir_merge_list sensors
template: !include_dir_merge_list templates

obtuse bison
#

this code looks very odd. Written by AI too?

#

if the template code is wrong it wont create a sensor

#

also I thought predbat needed the forecast values?

sleek mural
#

yeah, this was written by AI, I was troubleshooting it and removed the attributes part (the most complicated part) full code below, doubt it works though

#
  - sensor:
      - name: "Current Buy Electricity Price Raw"
        unique_id: current_buy_electricity_price_raw
        state: "{{ states('sensor.current_buy_electricity_price') }}" # Copies the current state of the original sensor
        unit_of_measurement: "{{ state_attr('sensor.current_buy_electricity_price', 'unit_of_measurement') }}"
        icon: "{{ state_attr('sensor.current_buy_electricity_price', 'icon') }}"
        attributes:
          raw_today: >
            {% set prices = state_attr('sensor.current_buy_electricity_price', '') %}
            {% set today_data = [] %}
            {% set current_date = now().date() %}

            {% for datetime_str, value in prices.items() %}
              {% set dt = as_datetime(datetime_str) %}
              {% if dt and dt.date() == current_date %}
                {% set end_dt = dt + timedelta(hours=1) %}
                {% set today_data = today_data + [{
                    'start': datetime_str,
                    'end': end_dt.isoformat(),
                    'value': value
                  }] %}
              {% endif %}
            {% endfor %}
            {{ today_data }}
          raw_tomorrow: >
            {% set prices = state_attr('sensor.current_buy_electricity_price', '') %}
            {% set tomorrow_data = [] %}
            {% set tomorrow_date = (now() + timedelta(days=1)).date() %}

            {% for datetime_str, value in prices.items() %}
              {% set dt = as_datetime(datetime_str) %}
              {% if dt and dt.date() == tomorrow_date %}
                {% set end_dt = dt + timedelta(hours=1) %}
                {% set tomorrow_data = tomorrow_data + [{
                    'start': datetime_str,
                    'end': end_dt.isoformat(),
                    'value': value
                  }] %}
              {% endif %}
            {% endfor %}
            {{ tomorrow_data }}```
obtuse bison
#

unfortunately I am not a template expert so I can't help you there. For templates help ask in #1284966664357810196

#

I recommend not including the AI generated code, it tends to piss people off

sleek mural
#

resolved here