Help to calculate how much charge my car needs in template or automation

I am trying to automate sending the amount of charge my car requires to the energy company for them to schedule the charge of my car. I already have all of these variables.
image
I want to calculate the amount of charge required (target charge - current charge) in a template that is called by an automation when the car is plugged to send that to the energy company (by updating “octopus charge to add”

I created a template that appears to do the calculation correctly, but is unhappy with the device class. Can anyone advise me on the correct settings that will work?

the template

    value_template: >-
      {% set t = states('sensor.polestar_0126_battery_charge_level') | int %}
      {% set u = states('input_number.car_target_charge') | int %}
      {{ (((u - t)/5 | int) | round(0) ) * 5 }} 

The other parameters
image

and the error message
image
It has calculated the number correctly as then rounded to 30, but is not recognising it. *error message shows 28 because I added the rounding as I was editing the post, I now get 30

I am assuming there is something in the parameters that is causing this to be an issue. I would be grateful if anyone can help.

Potentially I don’t even need the helper. what I am looking for is this automation

alias: car charge required update
description: Update car charge required
trigger:
  - platform: state
    entity_id:
      - sensor.myenergi_zappi_22070359_plug_status
    to: EV Connected
condition: []
action:
  - device_id: 7ce85253f41981b291aa568fd05104b0
    domain: select
    entity_id: 9c99ad497a003c6f54191ab11b315eb1
    type: select_option
    option: "40"
mode: single 

to put in the calculated value rather than default to 40. the options in the field are increments of 5 between 0 and 100. I just want to insert the calculated amount.

I would appreciate any help that I can get. I am far deeper into the code here than I have ever been before and am out of my depth already.

thanks in advance

Paul

The Device Action in your automation doesn’t support templates so you will need to use a service call.

I assume it’s a select entity, not an input_select entity, so the service call is select.select_option. If I am mistaken and it’s an input_select then you should modify the example below to use input_select.set_option.

Obviously you will need to change the name of select.your_octopus_select_entity_id to match what you have.

alias: car charge required update
description: Update car charge required
trigger:
  - platform: state
    entity_id:
      - sensor.myenergi_zappi_22070359_plug_status
    to: EV Connected
condition: []
action:
  - service: select.select_option
    target:
      entity_id: select.your_octopus_select_entity_id
    data:
      option: >
        {% set bc = states('sensor.polestar_0126_battery_charge_level') | int(0) %}
        {% set tc = states('input_number.car_target_charge') | int(0) %}
        {{ (tc - bc) // 5 * 5 }}
mode: single

If you want to test the template before you actually use it in an automation, simply copy-paste the following template into the Template Editor and confirm it reports the correct value.

{% set bc = states('sensor.polestar_0126_battery_charge_level') | int(0) %}
{% set tc = states('input_number.car_target_charge') | int(0) %}
{{ (tc - bc) // 5 * 5 }}
1 Like

Taras

thank you for your help. I just updated and tested and got the right results. The only thing is it appears to be rounding down e.g. based on the current parameters
image
90% - 52% = 38% it has rounded down to 35% is there a way that it can always round up? I would rather have more charge by a couple of % than be under.

thanks again for the help

edited to add, never mind I used the formula from the original which seems to do the rounding up.

For anyone reading I now have

alias: car charge required update
description: Update car charge required
trigger:
  - platform: state
    entity_id:
      - sensor.myenergi_zappi_22070359_plug_status
    to: EV Connected
condition: []
action:
  - service: select.select_option
    target:
      entity_id: select.octopus_target_state_of_charge
    data:
      option: >
        {% set bc = states('sensor.polestar_0126_battery_charge_level') | int(0)
        %} {% set tc = states('input_number.car_target_charge') | int(0) %} {{
        (((tc - bc)/5 | int) | round(0) ) * 5 }}
mode: single

which may not be the tidiest, but it seems to be working and giving me the results I want.

Thanks again Taras, a coffee on it’s way
image

Copy-paste the template portion of the example shown below into the Template Editor and confirm it rounds up the way you want. I designed it so 77 rounds down to 75 but 78 rounds up to 80. In other words, the threshold is when the remainder, of dividing the difference by 5, is greater than 2. Feel free to adjust the threshold value to your preference.

alias: car charge required update
description: Update car charge required
trigger:
  - platform: state
    entity_id:
      - sensor.myenergi_zappi_22070359_plug_status
    to: EV Connected
condition: []
action:
  - service: select.select_option
    target:
      entity_id: select.octopus_target_state_of_charge
    data:
      option: >
        {% set bc = states('sensor.polestar_0126_battery_charge_level') | int(0) %}
        {% set tc = states('input_number.car_target_charge') | int(0) %}
        {% set x = tc - bc %}
        {{ (x // 5 * 5) + iif(x % 5 > 2, 5, 0) }}
mode: single

You’re welcome and thank you for the coffee.