Calculate remaining time for the next fixed hour

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.

i’m not 100% sure I understand all of what you’re asking. it seems like you may have 2 or 3 questions combined into 1.

however I think the first part is asking about how you can calculate the time delta to a fixed time… today if that day is before that time, and tomorrow if after…

{% set departure_time_str = "07:20" %}
{% set battery_capacity = 64 %}
{% set target_battery_level = 70 %}
{% set time_delta = today_at(departure_time_str) - now()  %}
{% if time_delta.total_seconds() > 0 %}
  {{ time_delta }}
{% else %}
  {{ time_delta + timedelta(hours=24) }} 
{% endif %}

i’ve simplified your initial code in the process here. I’m sure it can get even more simplified.

you mentioned error that you’re getting when you put it into your code. please share your actual code that is getting the error. It’s hard to diagnose it without seeing the offending code.

1 Like