Dynamically adjust EVSE power based on grid fossil fuel percentage

Hi, I have the Chargepoint and Electricity Maps integration setup. Now I want to link the two together so that the Chargers rate tries to avoid times of high fossil fuel. I think a decent approach will be to look at the last week of electricity_maps_grid_fossil_fuel_percentage and do something to set cph50_charging_amperage_limit based on it. The possible range for cph50_charging_amperage_limit is 8 - 32 on my system, so when electricity_maps_grid_fossil_fuel_percentage is at the highest of the week, it should set it to 8 and when it is at the lowest of the week it should set it to 32. Any value in the middle should set it to something in the middle.

Any ideas how I would actually implement that? I’m still pretty new to templates, but feel like those will be involved.

This seems to be working pretty well:

{% set fossil_fuel_percentage = states('sensor.electricity_maps_grid_fossil_fuel_percentage') | float %}
{% set min_fossil_fuel = states('sensor.fossil_fuel_week_min') | float %}
{% set max_fossil_fuel = states('sensor.fossil_fuel_week_max') | float %}
{% if max_fossil_fuel != min_fossil_fuel %}
  {% set scale = (fossil_fuel_percentage - min_fossil_fuel) / (max_fossil_fuel - min_fossil_fuel) %}
  {% set amperage = 32 - (scale * (32-8)) %}
  {{ amperage | round(0) }}
{% else %}
  32
{% endif %}