Complex automation to power on car charger

I have a plug-in hybrid car that takes about 6 hours to charge. I am looking for advice on how to structure some automation/s to turn the charger on in a smart way that will balance environmental concerns with convenience. Even if the grid is running on mostly fossil fuels, it is better for the environment for me to charge the car than for it to switch to its petrol motor. So this automation is just about being smart about when to delay charging.

Components to work with:
HALO charger that HA can switch on and off.
RF433 button next to the front door that sends an MQTT message. This button has the label “charge card now”. I will press it if I know I am going out again soon.
Solar power - I have a sensor that reports current solar generation from my rooftop.
CO2 signal sensor that reports on current fossil fuel % in grid power.
Battery charge % sensor which gets data from the car’s hotspot. This is only about 80% reliable.

Logic:
If the Charge Car Now button is pressed, it should turn the charger on for 6 hours or until the car reports the battery is full, regardless of any other conditions.
If I am generating >2000W then the charger should be on.
I always want the car to be charged by about 8am.
I want to charge when the grid has maximal renewable power but not sure exactly how to do this optimisation. It could just be as simple as “if renewable are >50% of grid power, turn charger on”.

What is the logical way to structure this automation? One option would be to have a single automation with a time trigger that fires every 5 minutes, runs through all of the logic and either turns the charger on or off. Another option would be to split into many automations that can each turn the charger on or off.’

Either way, it seems pretty complex. Any ideas welcome.

I think the easiest thing to do would be to create two automations - one to turn it on & the other off.

Since I don’t have any idea what your entities are I will give you pseudo-code for the triggers:

- alias: on
  trigger:
    - platform: mqtt
      topic: button_pressed
    - platform: numeric_state
      entity_id: sensor.solar
      above: 2000
    - platform: time
      at: '00:02:00' # 8am minus 6 hours
    - platform: numeric_state
      entity_id: sensor.grid_renewables
      above: 50
  action:
    - service: switch.turn_on
      entity_id: switch.car_charger

- alias: off
  trigger:
    - platform: state
      entity_id: binary_sensor.car_charged
      to: 'on'
    - platform: numeric_state
      entity_id: sensor.solar
      below: 2000
    - platform: numeric_state
      entity_id: sensor.grid_renewables
      below: 50
  condition:
    - condition: time
      before: '00:02:00' # so it doesn't switch off after 2am based on 8am use time
  action:
    - service: switch.turn_off
      entity_id: switch.car_charger

you may need other things for the logic but that should get you started.

This is great. A few extra bits to add in so that it doesn’t turn off when it shouldn’t and I will be good. Thank you.

1 Like