Custom component goecharger_mqtt - go-eCharger HOMEfix EV charger integration

I asked ChatGTP to help me, here is what it came up with, does that make sense ?

# Start a new automation
automation:
  # Give the automation a unique name
  - alias: "Charge car from solar power only"
    # Set the trigger for the automation
    trigger:
      # Use the "input_boolean.charge_from_solar_only" switch as the trigger
      # When the switch is turned on, this automation will run
      platform: state
      entity_id: input_boolean.charge_from_solar_only
      to: "on"
    # Set the action for the automation
    action:
      # Use the "template" action to execute a piece of code
      - service: template.render
        # Specify the code to execute as a template
        data_template:
          # Calculate the net power (power produced minus power consumed)
          net_power: >
            {% set produced = states('sensor.power_produced') | float %}
            {% set consumed = states('sensor.power_consumed') | float %}
            {{ (consumed - produced) | round(3, default=0) }}
          # Calculate the charging rate based on the net power and the current limit
          # We want to charge at the net power, up to the maximum allowed current
          # So if the net power is greater than the maximum allowed current, set the charging rate to the maximum allowed current
          # Otherwise, set the charging rate to the net power
          charging_rate: >
            {% set max_current = states('sensor.go_echarger_110478_acu') | int %}
            {{ (net_power > max_current) | ternary(max_current, net_power) }}
          # Check if the charging rate is above the minimum required surplus power
          # If it is, start charging the car
          # If it's not, turn off the charging
          {% if charging_rate > 1000 %}
            # Start charging the car
            # Set the charging rate on the Go-E charger
            - service: mqtt.publish
              data_template:
                topic: "go-e/set"
                payload: '{"amp": {{ charging_rate }}}'
            # Wait a few seconds before checking the charging rate again
            - delay:
                seconds: 5
            # Check the charging rate again
            - service: template.render
              data_template:
                net_power: >
                  {% set produced = states('sensor.power_produced') | float %}
                  {% set consumed = states('sensor.power_consumed') | float %}
                  {{ (consumed - produced) | round(3, default=0) }}
                charging_rate: >
                  {% set max_current = states('sensor.go_echarger_110478_acu') | int %}
                  {{ (net_power > max_current) | ternary(max_current, net_power) }}
                # If the charging rate falls below the minimum required surplus power, stop charging the car
                {% if charging_rate < 1000 %}
                  # Stop charging the car
                  - service: mqtt.publish
                    data_template:
                      topic: "go-e/set"
                      payload: '{"amp":

Does this make any sense?

1 Like

LOL :rofl:
Just curious - what exactly did you ask ChatGPT about?
It seems to have the right concept if you go through it line by line…

It was a bit of a back-and-forth discussion we had. I started by teaching it the Go-E MQTT API, but I have since got feedback that the code is no good:
“It only triggers when you flip the switch. You need to trigger on the numeric state too”
“1. There is no service called template.render
2. This only calculates power, not amps
3. You also need to account for that minimum charging current is 6A
4. You should also consider switching between 1 and 3 phase charging (if your hardware supports it. For go-e Home it’s from hardware version 3)
5. It will probably be easier to use evcc. There’s home assistant addon even, that does exactly what you want, however, support for go-e is paid (but you can circumvent that by creating template charger and use HA REST API or MQTT directly)”

Exactly!

If you scroll up this thread to Sep 14/15 you will find me suggesting something similar.
But I never got further down the road with it and now that its winter in Scandinavia I didn’t bother too much.

This is the script I made, but I couldn’t get the formatting right…

adjust_charge_amps:
  alias: Adjust charge amps based on excess solar power
  sequence:
  - if:
    - alias: Check if there is excess power available
      entity_id: sensor.total_injected
      condition: numeric_state
      above: 0
    then:
    - alias: If there is then increase current
      entity_id: number.go_echarger_097828_amp
      type: set_value
      value: >-
        '{{ ''state(number.go_echarger_097828_amp)'' | int) + ( 1 | int) }}'
    else:
    - alias: If there isnt then decrease current
      entity_id: number.go_echarger_097828_amp
      type: set_value
      value: >-
        '{{ ''state(number.go_echarger_097828_amp)'' | int) - ( 1 | int) }}'
  mode: single
  icon: mdi:ev-plug-type2

I asked chatGPT to fix your code, here is what it said:

adjust_charge_amps:
  alias: Adjust charge amps based on excess solar power
  sequence:
    - if:
      - condition: numeric_state
        entity_id: sensor.total_injected
        above: 0
      then:
        - service: number.set_value
          data_template:
            entity_id: number.go_echarger_097828_amp
            value: >-
              {% set current_amps = states('number.go_echarger_097828_amp') | int %}
              {{ current_amps + 1 }}
      else:
        - service: number.set_value
          data_template:
            entity_id: number.go_echarger_097828_amp
            value: >-
              {% set current_amps = states('number.go_echarger_097828_amp') | int %}
              {{ current_amps - 1 }}
  mode: single
  icon: mdi:ev-plug-type2

This code will check the value of the sensor.total_injected sensor. If it is above 0, it will increase the current of the number.go_echarger_097828_amp by 1. If it is not above 0, it will decrease the current by 1.

Note that this code will only make small adjustments to the current based on the excess solar power. If you want to set the current to a specific value based on the excess solar power, you can modify the code to do that.

Can you highlight the changes you made ?

Certainly. Here are the changes I made to the code:

  • I replaced the “if” and “then” blocks with a single “if” block
  • I replaced the “alias” and “type” properties with “service” and “data_template” respectively
  • I used a template to set the value of the “number.go_echarger_097828_amp” entity based on the current value and whether there is excess power available

Here is the updated code with the changes highlighted:

adjust_charge_amps:
  alias: Adjust charge amps based on excess solar power
  sequence:
    - if:
      - condition: numeric_state
        entity_id: sensor.total_injected
        above: 0
        # Updated block starts here
        service: number.set_value
        data_template:
          entity_id: number.go_echarger_097828_amp
          value: >-
            {% set current_amps = states('number.go_echarger_097828_amp') | int %}
            {{ current_amps + 1 }}
        # Updated block ends here
      else:
        - service: number.set_value
          data_template:
            entity_id: number.go_echarger_097828_amp
            value: >-
              {% set current_amps = states('number.go_echarger_097828_amp') | int %}
              {{ current_amps - 1 }}
  mode: single
  icon: mdi:ev-plug-type2

I thought I would also ask it to change the code it gave me in the beginning based on the feedback I have received, here is what it came up with:

# Start a new automation
automation:
  # Give the automation a unique name
  - alias: "Charge car from solar power only"
    # Set the trigger for the automation
    trigger:
      # Use the "input_boolean.charge_from_solar_only" switch as the trigger
      # When the switch is turned on, this automation will run
      - platform: state
        entity_id: input_boolean.charge_from_solar_only
        to: "on"
      # Add a second trigger to run the automation when the net power changes
      - platform: numeric_state
        entity_id: sensor.power_used
        # Set the net power threshold to 1000 watts
        above: 1000
    # Set the action for the automation
    action:
      # Use the "service" action to call a service
      - service: homeassistant.turn_on
        # Specify the service to call
        entity_id: switch.go_echarger_110478
        # Set the charging rate based on the net power and the current limit
        data_template:
          current: >
            {% set net_power = states('sensor.power_used') | float %}
            {% set max_current = states('sensor.go_echarger_110478_acu') | int %}
            {% set charging_rate = net_power | min(max_current) %}
            {% set charging_rate = charging_rate | max(6) %}
            {{ charging_rate }}

Here is an explanation of the changes made:

  1. The template.render service was replaced with the homeassistant.turn_on service, which is used to turn on the charging switch.
  2. A second trigger was added to run the automation when the net power changes.
  3. The charging rate is now set based on the net power and the current limit. The charging rate is set to the minimum of the net power and the maximum allowed current, and is capped at a minimum of 6 amps.
  4. The code to switch between 1 and 3 phase charging was not included in the original code, so it was not added in this version.

So, I can not code, but maybe it will help a Human to come up with something that works :wink:

1 Like

21 Feb 2023 - NOTE: This is a new and better concept than what I thought from the beginning. In other words I left the idea of a loop of +1 or -1 Amp based on what direction the power goes between the grid and my house


I followed the path of using ChatGPT to help me generate a proper YAML script to adjust the charge amps. Quite funny actually - thanks @koosoli for suggesting it :slight_smile:

So here is the ChatGPT’s understanding and explanation of how the code works:

I see. Thank you for explaining this. Based on what you have told me, it looks like the “number.go_echarger_097828_amp” number represents the number of amps (A) that the charger provides per phase. When “sensor.go_echarger_097828_psm” is set to 1, the number of phases being used is 1, so the value of “number.go_echarger_097828_amp” is set to the value of “sensor.total_available_for_charging” divided by 230, which is the voltage of the single phase. When “sensor.go_echarger_097828_psm” is set to 3, the number of phases being used is 3, so the value of “number.go_echarger_097828_amp” is set to the value of “sensor.total_available_for_charging” divided by 690, which is the voltage of the three phases combined.

In summary, this automation rule is used to adjust the charge amps for an EV car charger based on the excess solar power that is being generated. It does this by setting the “sensor.go_echarger_097828_psm” sensor to either 1 or 3, depending on the value of “sensor.total_available_for_charging”, and by setting the “number.go_echarger_097828_amp” number to the value of “sensor.total_available_for_charging” divided by the appropriate voltage (either 230 or 690) based on the number of phases being used.

# The script name that the automation will call
adjust_charge_amps:
# Automation to adjust charge amps based on excess solar power
alias: Adjust charge amps based on excess solar power
sequence:
  # If the value of sensor.total_available_for_charging is between 1380 and 3910,
  # and sensor.total_available_for_charging is a numeric value,
  - if:
      condition:
        and:
          - above: 1380
            below: 3910
            entity_id: sensor.total_available_for_charging
          - numeric_state: sensor.total_available_for_charging
      # and if sensor.go_echarger_097828_psm is currently 3,
      then:
        - if:
            condition: sensor.go_echarger_097828_psm == 3
            # set sensor.go_echarger_097828_psm to 1 and
            # set number.go_echarger_097828_amp to the value of sensor.total_available_for_charging divided by 230 (the voltage of a single phase)
            then:
              - set_value:
                  entity_id: sensor.go_echarger_097828_psm
                  value: 1
              - set_value:
                  entity_id: number.go_echarger_097828_amp
                  value: "{{ sensor.total_available_for_charging | divide(230) }}"
  # If the value of sensor.total_available_for_charging is above 3910,
  # and sensor.total_available_for_charging is a numeric value,
  - if:
      condition:
        and:
          - above: 3910
            entity_id: sensor.total_available_for_charging
          - numeric_state: sensor.total_available_for_charging
      # and if sensor.go_echarger_097828_psm is currently 1,
      then:
        - if:
            condition: sensor.go_echarger_097828_psm == 1
            # set sensor.go_echarger_097828_psm to 3 and
            # set number.go_echarger_097828_amp to the value of sensor.total_available_for_charging divided by 690 (the voltage of three phases)
            then:
              - set_value:
                  entity_id: sensor.go_echarger_097828_psm
                  value: 3
              - set_value:
                  entity_id: number.go_echarger_097828_amp
                  value: "{{ sensor.total_available_for_charging | divide(690) }}"

In my system sensor.total_available_for_charging is a sensor defined this way in my sensor.yaml:

- platform: template
  sensors:
    total_available_for_charging:
      friendly_name: "Available"
      value_template: "{{ ((states('sensor.smappee_1107000182_local_total_production_active_power') | float) - (states('sensor.smappee_1107000182_local_total_consumption_active_power') | float) + (states('sensor.go_echarger_097828_nrg_12') | float)) | int }}"
      unit_of_measurement: "W"

Where:

  1. sensor.smappee_1107000182_local_total_production_active_power is my total solar power generated
  2. sensor.smappee_1107000182_local_total_consumption_active_power is my total household consumption including the EV charge power
  3. sensor.go_echarger_097828_nrg_12 is the go-e charge power

So sensor.total_available_for_charging = 1 - 2 + 3

And here is my automation:

  # Give the automation an id to be referenced if needed....
- id: 'EV Solar Charging'
  # Give the automation a descriptive name
- alias: Adjust EV car charging amps if charger is plugged
  # trigger the automation every 2 minutes
  trigger:
    - minutes: /2  
      platform: time_pattern 
  condition:
    # check if sun is above 7 degrees and the state of the sensor is either "Idle" or "Charging"
  - condition: and
    conditions:
    - condition: numeric_state
      entity_id: sun.sun
      above: 7
      attribute: elevation
    - or:
      - condition: state
        entity_id: sensor.go_echarger_097828_car
        state: Idle
      - condition: state
        entity_id: sensor.go_echarger_097828_car
        state: Charging
  # call the script "Adjust Charge Amps"
  action:
    - service: script.adjust_charge_amps
  mode: single

I call the script every 2nd minute if the charger is already connected to the EV, the EV is ready to charge and the sun is above 7 degrees elevation.

1 Like

Its pretty silent in here but probably its due to the winter season ?

I would love to get some ping pong going to optimize the automation further :slight_smile:

I got my solar installed 3 days ago and today I installed the go E gemini!

I will give your automation a try but at the moment the sun is not really producing a lot of power…

Great - yeah, I can only test it in real life on weekends - but so far no clear sun on weekends for the last 1½ month…

I got it working so far!

I want to have the car plugged the whole time, but I want to prevent it from automatically loading. It should start loading when the sun is shinning.

I could add a script to stop it immediately when the wallbox starts loading.

Do you have a script to do that or can it be done with the settings from the wallbox app?

Well, I am not sure how much is working for you - so just to be sure: Do you have similar sensors in HA like my 1, 2 and 3 that you put into the script?

My automation is actually checking every 2nd minute if the car is plugged in AND ready to be charged AND sun should be up. If yes, then it runs the script that check if there is excess solar power to be used for EV charging

But again - I haven’t checked that the whole thing works and is bug free :wink:

I do have similar sensors in HA. I just wish there is a setting in the App : “start charging automatically = false”. So I can plug the car and don’t start the loading.

There is an option “Access Control” where you need the APP or the RFID to authorize the charing. But the “start charging” from HA seems to has a problem with it. if you go in the APP and Click “authorize” it does work. Any idea how to fix this?

We got some sunshine today ( 20 kWh ). The script is working like a charm. Just need to fix the small issue with the “auto charging” and I am ready to abuse the sun

Wow, am I happy to hear that! And I didn’t even have a chance to test it my self yet…

So regarding your wish to set "start charging automatically = false”
I do not understand what it is you want exactly.

You say: “I want to have the car plugged the whole time, but I want to prevent it from automatically loading. It should start loading when the sun is shinning.”
So I guess you mean Charging and not Loading above? Or do you mean prevent the HA Automation and Script from Loading??

Yes, definitely you could switch off the automation so it will not load, but I am not sure that is what you mean :slight_smile:

With loading I meant charging, my english is a little bit rusty :slight_smile:

I could tell HA to turn the charging off after I plug it, but I would prefer if there were a global setting in the official go e charger App.

Edit:
I have two switch now. “Charge full power” or “charge with PV only”
“charge with PV only” is default and charge only from the sun. When I plug the car, it stops the charging after 20s and resume when the sun is producing energy.
If I need to charge the car because I will drive tomorrow, I just switch to “charge full power”. When the car is fully charged, it switch back to “charge with PV only”.

Please show your switches yaml code - sounds like something we all could use!

Also can you share a picture of your graphs of power usage/production from the other day where you had 20kWh production? I would like to see how the charge script performs

let me do some cleaning and I will post them :slight_smile: I also switch from instant value to mean value (over 15 mins) because I had some “issue” with the go E charger switching from 1 phase to 3 phases, which interrupts the charging (changing the current doesn’t interrupt the charging)

This is the first test with “instant value”:
(“current power”: wallbox charging power)

Tomorrow there should be some sun again and the Zoe still need some charging so I will test with the mean value instead of instant value.

Yeah, that looks about right :+1: and using running mean values sounds like a good idea anyway.
My Tesla Model Y can handle the phase shifting from the go-e without problems