EV Charging automation help

Hey everyone. I have been trying hard to get this automation going but keep running down a rabbit hole and getting lost. Here is what I am trying to do:

I have a solar setup with home batteries, an EV charger, and a EV vehicle. My goal is to setup automation that will look at the state of charge of my home batteries. If they hit 99% then I want the ev charger to turn on (this part was easy). Once it starts charging I want to first set the charge current to be 6. Then after a minute I want it to read my home batteries power. If the battery power is 0 or above that means it is not dischargingā€¦it is either idle or charging. So I want it to increment from 6 to 10.

The idea here is that I have excess solar to use. Before you ask I know there are many automations for excess solar but everything I have found appears to be based on grid sell back. I do not have this on for my inverter so instead I figured the next best thing to determine if I have excess solar is to look at my battery power. If the batteries are charged to 100% then all that excess solar is lost. So as you can see I prioritize charging my house batteries then want to send anything else to my EV.

So I started to write the code for this and cannot for the life of me get it to work. As of right now it will run, set the initial ev charge current to 6 then it will bump it up by 4 to reach 10. After that it waits and checks the battery power again. But instead of going from 10 to 14 it reads as 6 and increments to 10. I also see this error in the trace:

Stopped because an error was encountered at January 2, 2025 at 1:48:16 PM (runtime: 16.23 seconds)

Error rendering data template: UndefinedError: ā€˜current_current_stateā€™ is undefined

Any help would be appreciated. Here is my code so far:

alias: Debugging EV Charger Automation - Streamlined
description: >-
  Enable the EV charger when the car is connected and Battery SOC is 100%;
  adjust charger current to keep Battery Power between -1000 and 1000
triggers:
  - entity_id: binary_sensor.car_connected
    to: "on"  # Trigger when the car is connected
    trigger: state
  - minutes: /2  # Trigger every 2 minutes
    trigger: time_pattern
conditions:
  - condition: state
    entity_id: binary_sensor.car_connected
    state: "on"  # Only run if the car is connected
  - condition: numeric_state
    entity_id: sensor.luxpower_lxp_battery_state_of_charge
    above: 99  # Only run if the battery SOC is above 99%
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: switch.evse
            state: "off"  # If the charger is off
        sequence:
          - data:
              message: Starting charger and setting initial charge current to 6
            action: notify.mobile_app_myphone  # Notify about starting the charger
          - entity_id: switch.evse
            action: switch.turn_on  # Turn on the charger
          - delay: "00:00:10"  # Wait for 10 seconds
          - data:
              current: 6
            target:
              entity_id: switch.evse
            action: emporia_vue.set_charger_current  # Set the initial charge current to 6
          - service: input_number.set_value
            data:
              entity_id: input_number.ev_charging_current
              value: 6  # Store the initial charging current in input_number
  - choose:
      - conditions:
          - condition: state
            entity_id: switch.evse
            state: "on"  # If the charger is on
        sequence:
          - choose:
              - conditions:
                  - condition: numeric_state
                    entity_id: sensor.luxpower_lxp_battery_power
                    above: 1000  # If the battery power is above 1000
                sequence:
                  - delay: "00:00:05"  # Wait for 5 seconds
                  - data:
                      message: >
                        Increasing charge current by 4. Current Battery SOC: {{ states('sensor.luxpower_lxp_battery_state_of_charge') }}%
                    action: notify.mobile_app_myphone  # Notify about increasing the charge current and report battery SOC
                  - data_template:
                      current: >
                        {% set current_current_state = states('input_number.ev_charging_current') %}
                        {{ [current_current_state | int + 4, 48] | min }}
                    target:
                      entity_id: switch.evse
                    action: emporia_vue.set_charger_current  # Increase the charge current
                  - data_template:
                      message: >
                        New charging current: {{ [current_current_state | int + 4, 48] | min }}. Battery SOC: {{ states('sensor.luxpower_lxp_battery_state_of_charge') }}%
                    action: notify.mobile_app_myphone  # Notify about the new charging current and report battery SOC
                  - service: input_number.set_value
                    data_template:
                      entity_id: input_number.ev_charging_current
                      value: >
                        {{ [current_current_state | int + 4, 48] | min }}  # Update the input_number with the new current
              - conditions:
                  - condition: numeric_state
                    entity_id: sensor.luxpower_lxp_battery_power
                    below: -1000  # If the battery power is below -1000
                sequence:
                  - delay: "00:00:05"  # Wait for 5 seconds
                  - data:
                      message: >
                        Decreasing charge current by 4. Current Battery SOC: {{ states('sensor.luxpower_lxp_battery_state_of_charge') }}%
                    action: notify.mobile_app_myphone  # Notify about decreasing the charge current and report battery SOC
                  - data_template:
                      current: >
                        {% set current_current_state = states('input_number.ev_charging_current') %}
                        {{ [current_current_state | int - 4, 6] | max }}
                    target:
                      entity_id: switch.evse
                    action: emporia_vue.set_charger_current  # Decrease the charge current
                  - data_template:
                      message: >
                        New charging current: {{ [current_current_state | int - 4, 6] | max }}. Battery SOC: {{ states('sensor.luxpower_lxp_battery_state_of_charge') }}%
                    action: notify.mobile_app_myphone  # Notify about the new charging current and report battery SOC
                  - service: input_number.set_value
                    data_template:
                      entity_id: input_number.ev_charging_current
                      value: >
                        {{ [current_current_state | int - 4, 6] | max }}  # Update the input_number with the new current
mode: single

I was looking at your automation in VSCode & i noticed something that might help. In the screen shot below there are two places where the #comment you included is not coloured green like the others. I wonder if that might explain the template error?

weird, I get the same in my editor. I wonder if it is relevant

Could be, since the error relates to ā€œcurrent_current_stateā€. What will happen if you take out those comments? Worth a try.

If that makes no difference, then i guess it means that on the next 2 minute cycle the current_current_state variable is not retained from the last run. In that case you could try saving that value out to a new helper to ensure its non-volatile.

Ok I got it working. I am not an expert so maybe someone can explain this but this is how I got it to work. Posting just the relvant bits that got it working:

 - choose:
      - conditions:
          - condition: state
            entity_id: switch.evse
            state: "on"
        sequence:
          - choose:
              - conditions:
                  - condition: numeric_state
                    entity_id: sensor.luxpower_lxp_battery_power
                    above: 1000
                sequence:
                  - delay: "00:00:05"
                  - data:
                      message: >
                        Increasing charge current by 4. Current Battery SOC: {{
                        states('sensor.luxpower_lxp_battery_state_of_charge')
                        }}%
                    action: notify.mobile_app_shanephone
                  - data_template:
                      current: >
                        {% set current_current_state =
                        states('input_number.ev_charger_current') | int %}  {{
                        [current_current_state + 4, 48] | min }}
                    target:
                      entity_id: switch.evse
                    action: emporia_vue.set_charger_current
                  - data_template:
                      entity_id: input_number.ev_charger_current
                      value: >
                        {% set current_current_state =
                        states('input_number.ev_charger_current') | int %}  {{
                        [current_current_state + 4, 48] | min }}
                    action: input_number.set_value
                  - data_template:
                      message: >
                        New charging current: {{
                        states('input_number.ev_charger_current') }}. Battery
                        SOC: {{
                        states('sensor.luxpower_lxp_battery_state_of_charge')
                        }}%
                 
1 Like