Using power grid data to automate EV charging

MISO, which operates the power grid for much of central North America, has a public API that you can use to access real-time production data. You can turn this into a Home Assistant sensor by adding the following code to your configuration.yaml:
rest:
- resource: 'https://api.misoenergy.org/MISORTWDDataBroker/DataBrokerServices.asmx?messageType=getfuelmix&returnType=json'
scan_interval: 300
sensor:
- name: "Total grid power"
value_template: "{{ value_json.TotalMW|int }}"
device_class: power
unit_of_measurement: "MW"
icon: mdi:transmission-tower
- name: "Grid coal"
value_template: "{{ value_json.Fuel.Type.0.ACT|int }}"
device_class: power
unit_of_measurement: "MW"
icon: mdi:factory
- name: "Grid gas"
value_template: "{{ value_json.Fuel.Type.1.ACT|int }}"
device_class: power
unit_of_measurement: "MW"
icon: mdi:gas-burner
- name: "Grid nuclear"
value_template: "{{ value_json.Fuel.Type.2.ACT|int }}"
device_class: power
unit_of_measurement: "MW"
icon: mdi:atom
- name: "Grid wind"
value_template: "{{ value_json.Fuel.Type.3.ACT|int }}"
device_class: power
unit_of_measurement: "MW"
icon: mdi:wind-power
- name: "Grid solar"
value_template: "{{ value_json.Fuel.Type.4.ACT|int }}"
device_class: power
unit_of_measurement: "MW"
icon: mdi:solar-power
- name: "Grid other"
value_template: "{{ value_json.Fuel.Type.5.ACT|int }}"
device_class: power
unit_of_measurement: "MW"
icon: mdi:hydro-power
I don't drive much, so unless I have a big trip coming up, I rarely need a full battery. I want to turn the tap on when the electricity is clean, and off again when it's dirty. After collecting data for a while, I decided on cutoffs for how much coal power is too much (or how much wind is enough) and created automations to switch on/off the EVSE (which, in my case, is just a smart plug connected to the wall outlet that the charger's plugged into). I haven't figured out how to "set and forget" this one because of all the seasonal variation in power generation, but there could be a role for the Statistics integration here (for example, a script to detect when coal power generation is in the 10th percentile for a rolling 30-day window).

alias: Turn on EVSE when grid coal usage is low
description: ""
trigger:
- platform: numeric_state
entity_id: sensor.grid_coal
below: 19999.9
condition:
- condition: numeric_state
entity_id: sensor.grid_wind
above: 10000
action:
- type: turn_on
device_id: bd7c040ad491346705181910a569143d
entity_id: switch.evse
domain: switch
mode: single
alias: Turn off EVSE when coal usage is high
description: ""
trigger:
- platform: numeric_state
entity_id: sensor.grid_coal
above: 24999.6
- platform: numeric_state
entity_id: sensor.grid_wind
below: 8000
condition: []
action:
- type: turn_off
device_id: bd7c040ad491346705181910a569143d
entity_id: switch.evse
domain: switch
mode: single