Hello all,
Probably a stupid question but I couldn’t find any fix so far.
I created two binary sensors (one for cheapest hour in a day and one for consecutive cheapest 3 hours in a day) to be used to trigger various scenes. The one hour one works nicely but the one for 3 hours (nordpool_cheapest_3_hours) is always unavailable. Any idea why?
It looks like that in HA:
I can see each time I reloaded HA it got to status off but then unavailable.
The 1 hour sensor is from there (and works nicely)
The code for the 3 hours one is an adaptation of the one there
Here is the sensor code:
binary_sensor:
- platform: template
sensors:
nordpool_cheapest_1_hour:
value_template: >-
{% set l=state_attr('sensor.nordpool_kwh_se4_sek_3_10_025', 'raw_today')|sort(attribute='value') %}
{{ (now() >= l[0].start and now() <= l[0].end) }}
friendly_name: Nordpool cheapest 1 hour
nordpool_cheapest_3_hours:
value_template: >-
{% set nordpoolentity = 'sensor.nordpool_kwh_se4_sek_3_10_025' %}
{% set timemapper = {0: '00:00', 1 : '01:00', 2 : '02:00', 3 : '03:00', 4 : '04:00',
5 : '05:00', 6 : '06:00', 7 : '07:00', 8 : '08:00', 9 : '09:00', 10 : '10:00',
11 : '11:00', 12 : '12:00', 13 : '13:00', 14 : '14:00', 15 : '15:00', 16 : '16:00',
17 : '17:00', 18 : '18:00', 19 : '19:00', 20 : '20:00', 21 : '21:00', 22 : '22:00',
23 : '23:00', 24: '00:00', } %}
{% set num_hours = 3 | int %}
{% set iterativesum = namespace(iter=[]) %}
{% set lowestiter = namespace(kr=2) %}
{% set timelowest = namespace(hr=2) %}
{% set highestiter = namespace(kr=0) %}
{% set timehighest = namespace(hr=0) %}
{% set prices = namespace(price=[]) %}
{% set prices.price = prices.price + state_attr(nordpoolentity, 'today') %}
{%- for n in range(prices.price|length -num_hours +1) -%}
{%- set tempsum=namespace(temp=0) -%}
{%- for i in range(num_hours) -%}
{%- set tempsum.temp = tempsum.temp + prices.price[n+i] -%}
{% endfor -%}
{% set iterativesum.iter = iterativesum.iter + [tempsum.temp] -%}
{% endfor -%}
{% for iter in iterativesum.iter -%}
{%- if loop.index > now().hour -%}
{%- if iter < lowestiter.kr | float -%}
{%- set lowestiter.kr = iter | float -%}
{%- set timelowest.hr = loop.index -1 -%}
{%- endif -%}
{%- if iter > highestiter.kr | float -%}
{%- set highestiter.kr = iter | float -%}
{%- set timehighest.hr = loop.index -1 -%}
{%- endif -%}
{%- endif -%}
{% endfor -%}
{% set ontime = (now() >= timemapper[timelowest.hr] and now() <= timemapper[timelowest.hr + num_hours]) %}
{{ 'on' if ontime else 'off' }}
friendly_name: Nordpool cheapest 3 hours