- sensor:
- name: SHF Electricity price now
unique_id: shf_electricity_price_now
unit_of_measurement: "€/kWh" # Change this to c/kWh if you prefer cents
device_class: monetary
state: >
{% set data = state_attr("sensor.shf_data", "data") %}
{% set now_ts = now().timestamp() %}
{% set past_prices = data | selectattr("Timestamp", "lt", now_ts) | list %}
{% if past_prices | length > 0 %}
{{ past_prices[-1]["TotalPrice"] | round(4) }}
{% else %}
0.0
{% endif %}
availability: >
{% set data = state_attr("sensor.shf_data", "data") %}
{% set now_ts = now().timestamp() %}
{{ data | selectattr("Timestamp", "ge", now_ts) | list | length > 0 }}
attributes:
today_prices: >
{% set data = state_attr("sensor.shf_data", "data") %}
{% set start = today_at("00:00").timestamp() %}
{% set end = today_at("23:59").timestamp() %}
{{ data | selectattr("Timestamp", "ge", start) | selectattr("Timestamp", "lt", end) | map(attribute="TotalPrice") | list }}
tomorrow_prices: >
{% set data = state_attr("sensor.shf_data", "data") %}
{% set start = today_at("23:59").timestamp() %}
{{ data | selectattr("Timestamp", "ge", start) | map(attribute="TotalPrice") | list }}
today_min: '{{ this.attributes.today_prices | default([0]) | min }}'
today_avg: '{{ this.attributes.today_prices | default([0]) | average | round(4) }}'
today_max: '{{ this.attributes.today_prices | default([0]) | max }}'
tomorrow_min: '{{ this.attributes.tomorrow_prices | default([0], true) | min }}'
tomorrow_avg: '{{ this.attributes.tomorrow_prices | default([0], true) | average | round(4) }}'
tomorrow_max: '{{ this.attributes.tomorrow_prices | default([0], true) | max }}'