Script to calculate Solar Clipping

Hi,

I’m trying to use the forecast.solar API to forecast how much clipping I will experience the following day, so I can manage battery SOC and charge times.

This is my current setup:

REST sensor, gets tomorrows forecast in 15 minute intervals:


- platform: rest
  name: Energy Data
  resource: 'https://api.forecast.solar/xxxx/estimate/watts/51.xxxx/-1.xxxx/40/-87/5.28/40/93/4.4?limit=1'
  method: GET
  headers:
    Content-Type: application/json
  scan_interval: 300  # Update every 5 minutes
  value_template: "{{ now() }}"
  json_attributes:
    - result

The attributes of the sensor (some ommitted):

result:
  "2024-07-09 05:01:08": 0
  "2024-07-09 05:15:00": 109
  "2024-07-09 05:30:00": 186
  "2024-07-09 05:45:00": 267
  "2024-07-09 06:00:00": 350
  "2024-07-09 06:15:00": 441
  "2024-07-09 06:30:00": 531
...
  "2024-07-09 15:00:00": 1503
  "2024-07-09 15:15:00": 1460
  "2024-07-09 15:30:00": 1415
  "2024-07-09 15:45:00": 1369
  "2024-07-09 16:00:00": 1321
...
  "2024-07-09 21:23:32": 0
friendly_name: Energy Data

I then have this script, which I think iterates each pair and should give me a total kWh figure for clipped energy (anything over 5000 watts):

  calculate_clipped_energy:
    sequence:
      - variables:
          threshold: 5000
          energy_data_sensor: sensor.energy_data
          sensor_state: "{{ states(energy_data_sensor) }}"
          data: "{{ state_attr(energy_data_sensor, 'result') }}"
      - choose:
          - conditions:
              - condition: template
                value_template: "{{ sensor_state != 'unknown' and data is not none }}"
            sequence:
              - variables:
                  clipped_energy: >
                    {% set clipped_energy = 0 %}
                    {% set interval_hours = 15 / 60 %}
                    {% for timestamp, value in data.items() %}
                      {% set value = value | float %}
                      {% if value > threshold %}
                        {% set clipped_energy = clipped_energy + ((value - threshold) * interval_hours) %}
                      {% endif %}
                    {% endfor %}
                    {{ (clipped_energy / 1000) | float }}
              - service: input_number.set_value
                data:
                  entity_id: input_number.clipped_energy_kwh
                  value: "{{ clipped_energy }}"
              - service: notify.mobile_app_sm_g780f
                data:
                  message: "Total clipped energy for tomorrow: {{ clipped_energy }} kWh"
          - conditions:
              - condition: template
                value_template: "{{ sensor_state == 'unknown' or data is none }}"
            sequence:
              - service: input_number.set_value
                data:
                  entity_id: input_number.clipped_energy_kwh
                  value: 0.0
              - service: notify.mobile_app_sm_g780f
                data:
                  message: "Error: Energy data sensor state is 'unknown' or data is not available. Clipped energy set to 0 kWh."

The problem I’m having is even if I set the threshold to 1000 for testing purposes, I get 0.0kWh of clipped energy - any ideas?