Belgium: Elia current price in Home Assistant

Belgium: Elia current price in Home Assistant

SITUATION
I want to activate some adaptors when prices are negative.
I try to build 3 sensors
will do the rest = triggers later

NEEDED
I want to have 3 sensors based on data in api url
- marginalincrementalprice
- marginaldecrementalprice
- imbalanceprice
Both 3 sensors should have value “Positive”, “Negative”, “Unknown”

BASE INFORMATION
from url
https://opendata.elia.be/api/explore/v2.1/catalog/datasets/ods161/records?order_by=datetime%20DESC&limit=1
the typical output is:
{
“total_count”: 622,
“results”: [
{
“datetime”: “2024-09-25T08:21:00+00:00”,
“resolutioncode”: “PT1M”,
“quarterhour”: “2024-09-25T08:15:00+00:00”,
“qualitystatus”: “DataIssue”,
“ace”: -7.901,
“systemimbalance”: -282.251,
“alpha”: 6.996,
“alpha_prime”: 0,
“marginalincrementalprice”: 228.9,
“marginaldecrementalprice”: -416.985,
“imbalanceprice”: 235.896
}
]
}

AS IS
This is what I have

# Elia current price
#
# /homeassistant/configuration.yaml updated with
# Load self made sensor for elia from the sensor folder
#sensor: !include_dir_merge_list sensor/
#
# Location of file: /homeassistant/sensor/elia.yaml

- platform: rest
  scan_interval: 60
  resource: https://opendata.elia.be/api/explore/v2.1/catalog/datasets/ods161/records?order_by=datetime%20DESC&limit=1
  name: "Elia current price"
  json_attributes_path: "$.results"
  json_attributes:
    - marginalincrementalprice
    - marginaldecrementalprice
    - imbalanceprice

- platform: template
  sensors:
    elia_current_marginalincrementalprice:
      icon_template: "mdi:tree-outline"
      friendly_name: "marginalincrementalprice"
      value_template: >-
        {% set state = state_attr('sensor.elia_current_marginalincrementalprice', 'marginalincrementalprice') %}
        {% if state | float(0) >= 0 %}Positive
        {% elif state | float(0) < 0 %}Negative
        {% else %}Unknown
        {% endif %}

- platform: template
  sensors:
    elia_current_marginaldecrementalprice:
      icon_template: "mdi:nature"
      friendly_name: "marginaldecrementalprice"
      value_template: >-
        {% set state = state_attr('sensor.elia_current_marginaldecrementalprice', 'marginaldecrementalprice') %}
        {% if state | float(0) >= 0 %}Positive
        {% elif state | float(0) < 0 %}Negative
        {% else %}Unknown
        {% endif %}

- platform: template
  sensors:
    elia_current_imbalanceprice:
      icon_template: "mdi:grass"
      friendly_name: "imbalanceprice"
      value_template: >-
        {% set state = state_attr('sensor.elia_current_imbalanceprice', 'imbalanceprice') %}
        {% if state | float(0) >= 0 %}Positive
        {% elif state | float(0) < 0 %}Negative
        {% else %}Unknown
        {% endif %}

PROBLEM
All is looking good in the “Developper tools > Template”
I can’t get the values (in state) correctly evaluated.
What could be the fix to do so?

meanwhile I changed the icons to:

icon_template: "mdi:currency-eur"

First debugging step is to check the logs.
You will see a message stating that the state of an entity cannot be larger than 255 chars.
Also, “results” is an array, so:

...
  value_template: "{{ 'ok' }}"
  json_attributes_path: "$.results[0]"
...

Next your “state_attr” should refer sensor.elia_current_price

Currently no success. As so far I have (with a little tweaks/help of chagGPT)

# Elia current price
#
# /homeassistant/configuration.yaml updated with
# Load self made sensor for elia from the sensor folder
#sensor: !include_dir_merge_list sensor/
#
# Location of file: /homeassistant/sensor/elia.yaml

- platform: rest
  scan_interval: 60
  resource: https://opendata.elia.be/api/explore/v2.1/catalog/datasets/ods161/records?order_by=datetime%20DESC&limit=1
  name: "Elia current price"
  value_template: "{{ 'ok' }}"
  json_attributes_path: "$.records[0]"  # Adjusted to access the first record
  json_attributes:
    - marginalincrementalprice
    - marginaldecrementalprice
    - imbalanceprice

- platform: template
  sensors:
    elia_current_marginalincrementalprice:
      icon_template: "mdi:currency-eur"
      friendly_name: "Marginal Incremental Price"
      value_template: >-
        {% set state = state_attr('sensor.elia_current_price', 'marginalincrementalprice') %}
        {% if state is not none %}
          {% if state | float >= 0 %}Positive
          {% elif state | float < 0 %}Negative
          {% else %}Unknown
          {% endif %}
        {% else %}None
        {% endif %}

    elia_current_marginaldecrementalprice:
      icon_template: "mdi:currency-eur"
      friendly_name: "Marginal Decremental Price"
      value_template: >-
        {% set state = state_attr('sensor.elia_current_price', 'marginaldecrementalprice') %}
        {% if state is not none %}
          {% if state | float >= 0 %}Positive
          {% elif state | float < 0 %}Negative
          {% else %}Unknown
          {% endif %}
        {% else %}None
        {% endif %}

    elia_current_imbalanceprice:
      icon_template: "mdi:currency-eur"
      friendly_name: "Imbalance Price"
      value_template: >-
        {% set state = state_attr('sensor.elia_current_price', 'imbalanceprice') %}
        {% if state is not none %}
          {% if state | float >= 0 %}Positive
          {% elif state | float < 0 %}Negative
          {% else %}Unknown
          {% endif %}
        {% else %}None
        {% endif %}

#for testing purposes
- platform: template
  sensors:
    elia_full_state:
      friendly_name: "Full Elia State"
      value_template: "{{ states('sensor.elia_current_price') }}"

this is what I get in the 'States" tab of Developer tools:

Give your brain a shot :slight_smile:

off course
but no luck yet!
:wink:

Did you see the mistake, “records” in place of “results”?

yes
still no succes.
tried a lot already

Well, works for me

- platform: rest
  scan_interval: 60
  resource: https://opendata.elia.be/api/explore/v2.1/catalog/datasets/ods161/records?order_by=datetime%20DESC&limit=1
  name: "Elia current price"
  value_template: "{{ 'ok' }}"
  json_attributes_path: "$.results[0]"  # Adjusted to access the first record
  json_attributes:
    - marginalincrementalprice
    - marginaldecrementalprice
    - imbalanceprice

image

1 Like