How to add time stored in a variable

I have created an variable input_datetime.sunrise
in which I store the time in the form %H:%M although the seconds in the template editor also displays, in the gui they can not be set, I need to add to this time say 12 hours.
How to convert the value of the variable to add this time ?

there is an example how to do it from now() function, but I couldn’t change it to work.
{{ (as_timestamp(now()) + 12*60*60) | timestamp_custom(“%H:%M:%S”) }}}

I believe you can do this:

{{ as_datetime(states('input_datetime.sunrise')) + timedelta(hours=12) }} 

just a question. Is input_datetime.sunrise the same as the sun.sun sunrise (three in a row!)

1 Like

The template editor gives an error

TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘datetime.timedelta’

input_datetime.sunrise is a variable where I store my sunrise it has nothing to do with sun.sun sunrise

Timedelta returns a datetime object so you want your starting time as_datetime

Also watch the curly quotes - they’ll get you.

I just confirmed @hellis’ suggestion is correct:

Mine works:

{{ as_datetime(states('input_datetime.last_cleaned_kitchen')) + timedelta(hours=12) }}

Returns last cleaned time + 12 hours.

so show the entire automation and we can help.

Also nothing to do with? I’d expect them to be the same because sun.sun shoujld contain sunrise at your lat/long per the setup… :wink:

Could it be that you get that error when it’s an input_datetime with only time?

1 Like


I cant understand where is the problem ?

Yes, I only keep the time, I don’t need the date.

first - please do not paste screenshots - its impossibly hard to read on a phone - paste text - using the preformat text control (under the gear in the editor or preceeded and ended with three backticks

And yes Hellis is correct again -

I just did it with an Input datetime that ONLY has time - and it blows up exactly the same way yours just did. SO the problem is you dont have a date and need to do date math.

So there’s multiple ways to handle it - what is your INTENT - what are you trying to do with this and what do you need as your end result?

So you just need a time or do you need a time based on that time TODAY? (Its different)

I pasted the screenshot just as confirmation, the error text was the same.

I just need a time.

in that case we need to use:

{{ today_at(states('input_datetime.sunrise')) + timedelta(hours=12) }}

This will give you both time and date.
Is this to be used in an automation then it will work but if you want to place it on the dashboard then it will not be great.
That is why Nathan asks for your intent. It’s a lot easier to help if we know what to help with

it works, but I’m getting the date in the form
2024-12-17 06:09:00+07:00
not just 06:09:00 like I want.

To tell you fully what I need it for, I’m going to use it to integrate Adaptive Lighting GitHub - basnijholt/adaptive-lighting: Adaptive Lighting custom component for Home Assistant
I’ll put the start of the day into this variable (just time) and I need to add 12 hours to that time, that’s the end of the day, I’ll put that into the sunrise and sunset parameters in the Adaptive Lighting integration.

I have never heard of that integration nor am I tempted to read the documentations.
But I’m surprised it can’t take both time and date. But I guess that is because it sets a schedule.

Anyways…

{{ (today_at(states('input_datetime.sunrise')) + timedelta(hours=12)).timestamp() | timestamp_custom("%H:%M:%S") }}
2 Likes

it’s all working now.

About this integration, if you have lights with variable temperature or color, you definitely need it or something similar, I would use it even if I only had lights with variable brightness, but this is certainly not my business you know better.

I’m fine with whatever light there is. Not to picky.
If I integrated something that controlled the lights here then there would be eyes starring at me for sure… Even in complete darkness I could feel that…

1 Like

Well just according to research if the light will follow the cycles of the sun, it raises people’s mood and increases efficiency, so why not, but I have not yet tested on myself.