Hi all, I was hoping someone could help me with a bit of data sampling. I have a Huawei solar set up with a battery, and I’m investigating how I can charge the battery at the cheapest time (normally early in the morning), and sell back to the grid at the most expensive time (normally around 6-7am), but only if the electricity price is high enough at the most expensive time, so I can actually earn something from selling it. When I sell electricity back to the grid, I only get the raw electricity price (so not including taxes, transport etc).
For example, here in Denmark at the moment, it’s typically around 2dkk / kWh in the early hours of the morning, where about 1 dkk of that is taxes, transport of electricity etc. At the most expensive time, it can be around 4dkk / kWh, where it’s still about 1 dkk for taxes, transport of electricity etc. This means that there is a raw electricity price difference of 3 dkk, so I can in theory buy at the cheapest time (2 dkk) and sell back at the most expensive (4 dkk, or when I get 3 dkk / kWh). But this doesn’t happen everyday… Hence I would like to extract the prices, and create some automations to charge, and discharge the battery, only when it pays.
Currently I use the Energi data service integration, so I get the complete electricity price. This pulls in all the data, so I can calculate periods of where it’s cheap, expensive etc. But what I can’t figure out, is how to extract the price at a particular hour. For example, I have a template sensor that looks for the cheapest 3 hour in the morning, as described here (I assume everyone playing with the Nordpool integration has seen this).
But now I would like to extract a price at those cheapest hours, and so the same at the most expensive time. Here’s the code I use for extracting the cheapest period, just in case it helps someone.
#Cheapest energy prices at night
- platform: template
sensors:
cheapest_electricity_at_night:
device_class: timestamp
friendly_name: Cheapest electricity at night
value_template: >
{%- set numberOfSequentialHours = 3 -%}
{%- set lastHour = 7 -%}
{%- set firstHour = 0 -%}
{%- if state_attr('sensor.energi_data_service', 'tomorrow_valid') == true -%}
{%- set ns = namespace(counter=0, list=[], cheapestHour=today_at("00:00") + timedelta( hours = (24)), cheapestPrice=999.00) -%}
{%- for i in range(firstHour + numberOfSequentialHours, lastHour+1) -%}
{%- set ns.counter = 0.0 -%}
{%- for j in range(i-numberOfSequentialHours, i) -%}
{%- set ns.counter = ns.counter + state_attr('sensor.energi_data_service', 'tomorrow')[j] -%}
{%- endfor -%}
{%- set ns.list = ns.list + [ns.counter] -%}
{%- if ns.counter < ns.cheapestPrice -%}
{%- set ns.cheapestPrice = ns.counter -%}
{%- set ns.cheapestHour = today_at("00:00") + timedelta( hours = (24 + i - numberOfSequentialHours)) -%}
{%- endif -%}
{%- endfor -%}
{{ ns.cheapestHour }}
{%- set ns.cheapestPrice = ns.cheapestPrice / numberOfSequentialHours -%}
{%- endif -%}
Any help would be msot appreciated