CCCPaste

Nordpool hourly averages with rank

template:
  sensor:
    - name: "Nordpool Hourly Average and Rank"
      unique_id: nordpool_hourly_ave_rank
      state: >-
        {% set raw_today = state_attr('sensor.nordpool', 'raw_today') %}
        {% if raw_today and raw_today|length > 0 %}
          {% set current_hour = now().hour %}
          {% set start_idx = current_hour * 4 %}
          {% if start_idx + 3 < raw_today|length %}
            {% set sum = raw_today[start_idx].value + raw_today[start_idx + 1].value + raw_today[start_idx + 2].value + raw_today[start_idx + 3].value %}
            {{ (sum / 4) | round(4) }}
          {% else %}
            {{ state_attr('sensor.nordpool', 'current_price') | float(0) }}
          {% endif %}
        {% else %}
          {{ state_attr('sensor.nordpool', 'current_price') | float(0) }}
        {% endif %}
      unit_of_measurement: "c/kWh"
      device_class: monetary
      icon: mdi:flash
      attributes:
        today: >-
          {% set raw_today = state_attr('sensor.nordpool', 'raw_today') %}
          {% if raw_today and raw_today|length >= 4 %}
            {% set ns = namespace(hours=[]) %}
            {% for h in range(24) %}
              {% set idx = h * 4 %}
              {% if idx + 3 < raw_today|length %}
                {% set sum = raw_today[idx].value + raw_today[idx+1].value + raw_today[idx+2].value + raw_today[idx+3].value %}
                {% set ns.hours = ns.hours + [(sum / 4) | round(4)] %}
              {% endif %}
            {% endfor %}
            {{ ns.hours }}
          {% else %}
            []
          {% endif %}
        
        tomorrow: >-
          {% set raw_tomorrow = state_attr('sensor.nordpool', 'raw_tomorrow') %}
          {% if raw_tomorrow and raw_tomorrow|length >= 4 %}
            {% set ns = namespace(hours=[]) %}
            {% for h in range(24) %}
              {% set idx = h * 4 %}
              {% if idx + 3 < raw_tomorrow|length %}
                {% set sum = raw_tomorrow[idx].value + raw_tomorrow[idx+1].value + raw_tomorrow[idx+2].value + raw_tomorrow[idx+3].value %}
                {% set ns.hours = ns.hours + [(sum / 4) | round(4)] %}
              {% endif %}
            {% endfor %}
            {{ ns.hours }}
          {% else %}
            []
          {% endif %}
        
        raw_today: >-
          {% set raw_today = state_attr('sensor.nordpool', 'raw_today') %}
          {% if raw_today and raw_today|length >= 4 %}
            {% set ns = namespace(periods=[]) %}
            {% for h in range(24) %}
              {% set idx = h * 4 %}
              {% if idx + 3 < raw_today|length %}
                {% set sum = raw_today[idx].value + raw_today[idx+1].value + raw_today[idx+2].value + raw_today[idx+3].value %}
                {% set ns.periods = ns.periods + [{
                  'start': raw_today[idx].start.isoformat(),
                  'end': raw_today[idx+3].end.isoformat(),
                  'value': (sum / 4) | round(4)
                }] %}
              {% endif %}
            {% endfor %}
            {% set sorted_periods = ns.periods | sort(attribute='value') %}
            {% set ns2 = namespace(result=[]) %}
            {% for item in ns.periods %}
              {% set ns2.result = ns2.result + [{
                'start': item.start,
                'end': item.end,
                'value': item.value,
                'rank': sorted_periods.index(item) + 1
              }] %}
            {% endfor %}
            {{ ns2.result }}
          {% else %}
            []
          {% endif %}
        
        raw_tomorrow: >-
          {% set raw_tomorrow = state_attr('sensor.nordpool', 'raw_tomorrow') %}
          {% if raw_tomorrow and raw_tomorrow|length >= 4 %}
            {% set ns = namespace(periods=[]) %}
            {% for h in range(24) %}
              {% set idx = h * 4 %}
              {% if idx + 3 < raw_tomorrow|length %}
                {% set sum = raw_tomorrow[idx].value + raw_tomorrow[idx+1].value + raw_tomorrow[idx+2].value + raw_tomorrow[idx+3].value %}
                {% set ns.periods = ns.periods + [{
                  'start': raw_tomorrow[idx].start.isoformat(),
                  'end': raw_tomorrow[idx+3].end.isoformat(),
                  'value': (sum / 4) | round(4)
                }] %}
              {% endif %}
            {% endfor %}
            {% set sorted_periods = ns.periods | sort(attribute='value') %}
            {% set ns2 = namespace(result=[]) %}
            {% for item in ns.periods %}
              {% set ns2.result = ns2.result + [{
                'start': item.start,
                'end': item.end,
                'value': item.value,
                'rank': sorted_periods.index(item) + 1
              }] %}
            {% endfor %}
            {{ ns2.result }}
          {% else %}
            []
          {% endif %}
        
        current_price: "{{ states('sensor.nordpool_hourly_average') }}"
        
        current_rank: >-
          {% set raw_today = state_attr('sensor.nordpool_hourly_average', 'raw_today') %}
          {% if raw_today and raw_today|length > 0 %}
            {% set current_hour = now().hour %}
            {% if current_hour < raw_today|length %}
              {{ raw_today[current_hour].rank }}
            {% else %}
              unavailable
            {% endif %}
          {% else %}
            unavailable
          {% endif %}
        
        average: >-
          {% set raw_today = state_attr('sensor.nordpool', 'raw_today') %}
          {% if raw_today and raw_today|length >= 4 %}
            {% set total = 0 %}
            {% set count = 0 %}
            {% for h in range(24) %}
              {% set idx = h * 4 %}
              {% if idx + 3 < raw_today|length %}
                {% set sum = raw_today[idx].value + raw_today[idx+1].value + raw_today[idx+2].value + raw_today[idx+3].value %}
                {% set total = total + (sum / 4) %}
                {% set count = count + 1 %}
              {% endif %}
            {% endfor %}
            {% if count > 0 %}
              {{ (total / count) | round(4) }}
            {% else %}
              0
            {% endif %}
          {% else %}
            0
          {% endif %}
        
        min: >-
          {% set raw_today = state_attr('sensor.nordpool', 'raw_today') %}
          {% if raw_today and raw_today|length >= 4 %}
            {% set ns = namespace(min_val=999999) %}
            {% for h in range(24) %}
              {% set idx = h * 4 %}
              {% if idx + 3 < raw_today|length %}
                {% set sum = raw_today[idx].value + raw_today[idx+1].value + raw_today[idx+2].value + raw_today[idx+3].value %}
                {% set avg = sum / 4 %}
                {% if avg < ns.min_val %}
                  {% set ns.min_val = avg %}
                {% endif %}
              {% endif %}
            {% endfor %}
            {{ ns.min_val | round(4) if ns.min_val < 999999 else 0 }}
          {% else %}
            0
          {% endif %}
        
        max: >-
          {% set raw_today = state_attr('sensor.nordpool', 'raw_today') %}
          {% if raw_today and raw_today|length >= 4 %}
            {% set ns = namespace(max_val=0) %}
            {% for h in range(24) %}
              {% set idx = h * 4 %}
              {% if idx + 3 < raw_today|length %}
                {% set sum = raw_today[idx].value + raw_today[idx+1].value + raw_today[idx+2].value + raw_today[idx+3].value %}
                {% set avg = sum / 4 %}
                {% if avg > ns.max_val %}
                  {% set ns.max_val = avg %}
                {% endif %}
              {% endif %}
            {% endfor %}
            {{ ns.max_val | round(4) }}
          {% else %}
            0
          {% endif %}
        
        mean: >-
          {% set raw_today = state_attr('sensor.nordpool', 'raw_today') %}
          {% if raw_today and raw_today|length >= 4 %}
            {% set total = 0 %}
            {% set count = 0 %}
            {% for h in range(24) %}
              {% set idx = h * 4 %}
              {% if idx + 3 < raw_today|length %}
                {% set sum = raw_today[idx].value + raw_today[idx+1].value + raw_today[idx+2].value + raw_today[idx+3].value %}
                {% set total = total + (sum / 4) %}
                {% set count = count + 1 %}
              {% endif %}
            {% endfor %}
            {% if count > 0 %}
              {{ (total / count) | round(4) }}
            {% else %}
              0
            {% endif %}
          {% else %}
            0
          {% endif %}
        
        tomorrow_valid: "{{ state_attr('sensor.nordpool', 'tomorrow_valid') }}"
        currency: "EUR"
        country: "Finland"
        region: "FI"
        unit: "kWh"
        price_in_cents: true