andreas-berg / hass-entsoe-dayahead

Integration for Home Assistant to fetch day ahead energy prices from European countries via ENTSO-e Transparency Platform
14 stars 1 forks source link

Attributes for prices_today not updating exactly at 00:00:00 #5

Open quenthal opened 5 months ago

quenthal commented 5 months ago

Long explanation: I have template sensor, that calculates predefined cheapest hours for the current day. After I switched from nordpool sensor to this more stable one, I started noticing some calculation errors at midnight and little after that. Attributes for today are not updated exactly at 00:00:00, but instead somewhere between midnight and 01:00:01. I.e. there are "yesterdays" attributes for today around then.

Short: With sensor.entsoe_prices_today, attributes are for wrong day (yesterday) up to until around 1AM.

andreas-berg commented 5 months ago

Thank you for bringing this to my attention, I'll try to reproduce the bug asap and provide an fix.

quenthal commented 4 months ago

Thanks. In the meantime, I have made following template sensors to go around this.

First one is sensor to get current hour's price: {% set current_hour = now().strftime('%Y-%m-%d %H:00:00+03:00') %} {% set prices_today = state_attr('sensor.entsoe_prices_today', 'prices_today') %} {% set prices_tomorrow = state_attr('sensor.entsoe_prices_tomorrow', 'prices_tomorrow') %} {% set all_prices = prices_today + prices_tomorrow %} {% set current_price = all_prices | selectattr('time', 'eq', current_hour) | map(attribute='price') | first | default(0) %} {{ ((current_price | float * 1.24) + 0.45) | round(3) }} As you can see, for this template I merge today and tomorrow, so getting current hour is not missed after midnight. This would be nice addition to be value of prices_today, if you somehow make the attributes and sensor to change value at 00:00:00, maybe this could then be updated every hour..?

Second one is sensor to get X (=input_number.kytkentaraja_tunnit) cheapest hour of current day. For this I now have automation, that stores tomorrow's values to input_helper at 23:59:59: service: input_text.set_value target: entity_id: input_text.huominen_kytkentaraja_entsoe data: value: " {{ (((state_attr('sensor.entsoe_prices_tomorrow', 'prices_tomorrow') | sort(attribute='price'))[int(states('input_number.kytkentaraja_tunnit')) - 1].price)*1.24+0.45) | round(3) }}" I can then use this in this template sensor for calculating X cheapest hours (you can choose it from the UI by changing value of input_number.kytkentaraja_tunnit) of the day starting exactly at 00:00:00 like with old nordpool sensor. If current time is below 01:00 or exactly at 01:00:00, I use the value stored in input_text.huominen_kytkentaraja_entsoe stored just before midnight, and if it is over 01:00:00 I can calculate it from the attributes of prices_today:

{{ states('input_text.huominen_kytkentaraja_entsoe') | float if now().hour < 1 or (now().hour == 1 and now().minute == 0 and now().second == 0) else (((state_attr('sensor.entsoe_prices_today', 'prices_today') | sort(attribute='price'))[int(states('input_number.kytkentaraja_tunnit')) - 1].price)*1.24+0.45) | round(3) }}

Other things I manage with these templates is adding VAT and 0.45 cents as additional price of my electricity company.

quenthal commented 2 months ago

Could this be same issue: https://github.com/JaccoR/hass-entso-e/issues/157 ?