Add hours to input_datetime from sensor value

Hi all,

I’m hoping someone can help. I am trying to make an input_datetime entity, which should be the sum of another datetime entity + a sensor value. For example, I have input_datetime.cheapest_hours_car_charging which for argument sake gives a time of 02:00, and I have sensor.number_of_sequential_hours which lets say has a value of 2. I want to create a new input_datetime which gives the time 04:00.

I have tried using many of the existing posts in the template editor, but I keep getting errors.

Any help would be most appreciated.

The resulting entity doesn’t need to be a helper, because you won’t be changing the value manually directly. It can just be a template sensor.

{{ states('input_datetime.cheapest_hours_car_charging') | today_at + timedelta(hours=states('sensor.number_of_sequential_hours') | int(0)) 

Awesome, works great, thank you!

How do I only output the time in only hours and minutes? Right now, the output is as follows:

“2024-05-27 03:00:00+02:00”

Just to add a bit more info (as it might help you to understand what I’m trying to do with it), I have created an input boolean called ‘Charge Car Now’, which is turned on in an automation when the cheapest hours have been identified by another sensor. When this is on (as well as another condition), the car will charge. The time at which the cheapest hours have been identified, has been determined by the number of sequential hours (a time window), which the cheapest hours sensor looks for in the electricity prices sensor I have. I want this Charge Car Now input boolean to turn off at a time corresponding to the cheapest hours + sequential hours (hence the two sensors I gave in the original post).

When I add in the template sensor you provided, I’m struggling to see how I use that in an automation… Basically, I want to turn off the input boolean at the time of the sensor you created for me :slight_smile:

If I understand correctly what you’re trying to do, the automation is basically written for you at

replace + 2*60*60 by + states("sensor.number_of_sequential_hours")|int*60*60

That’s the one, thank you!

You got your answer from Koying, but just to wrap this up.

This will give the time as HH:MM:SS:

{{ (states('input_datetime.cheapest_hours_car_charging') | today_at + timedelta(hours=states('sensor.number_of_sequential_hours') | int(0))).time() }}

Or as HH:MM:

{{ (states('input_datetime.irrigation_start_time') | today_at + timedelta(hours=states('sensor.nighttime_number') | int(0))).strftime('%H:%M') }}
2 Likes