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?
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:
type: history-graph
title: Elia Imbalance Price
entities:
- sensor.elia_current_price_imbalance
hours_to_show: 24 # Adjust the time range as needed
refresh_interval: 60 # Refresh interval in seconds