Hi,

I’m trying to develop an script that automatically calculates the intensity of my EV charger to have the vehicle charged at a “fixed” time.

To do this I need to calculate the remaining hours between now() and the next fixed hour. So if the hour is less than actual hour, it is the next day hour.

Quick sample that works on the template editor:

Departure time is set to 7:20am.

```
{% set departure_time = strptime("07:20","%H:%M") %}
{% set battery_capacity = 64 %}
{% set target_battery_level = 70 %}
Remaining hours (when actual hour is less than departure_hour):
{{ ((now().replace(hour=departure_time.hour, minute=departure_time.minute, second=0, microsecond=0) - now()).total_seconds() / 3600) }}
Remaining hours (when actual hour is big than departure_hour):
{{ ((now() - now().replace(hour=departure_time.hour, minute=departure_time.minute, second=0, microsecond=0)).total_seconds() / 3600) }}
```

As I told, this works on the Developer tools → Templates, but when I add it into the script on a step, it raises an error:

```
Error: TypeError: 'LoggingUndefined' object cannot be interpreted as an integer
```

Any idea how can I do it?

The final formulas that works on the Templates section of Developer tools:

```
{{ (((target_battery_level - int(states('sensor.niro_ev_battery_level'))) *
battery_capacity / 100 ) * 1000 / (((now() -
now().replace(hour=departure_time.hour, minute=departure_time.minute, second=0,
microsecond=0)).total_seconds() / 3600) * 230)) | round(0,'ceil') }}
{{ (((target_battery_level - int(states('sensor.niro_ev_battery_level'))) *
battery_capacity / 100 ) * 1000 / (((now().replace(hour=departure_time.hour,
minute=departure_time.minute, second=0, microsecond=0) - now()).total_seconds()
/ 3600) * 230)) | round(0,'ceil') }}
```

Basically, it calculates the percent of battery to charge (target percent - actual percentage) and also based on the car max capacity, calculates the Wh to charge. Then divides by the time to charge (my above question) multiplied by 230. The final result should be a integer so an upper round to ensure that the battery is charged next morning.

It should work if I plug the car at 10pm or at 1am.