Trying to add minutes to sunset time via helper number

Hi there,
I have a number helper I created, which stores the minutes the closing of a chicken hen door should be delayed after sunset. That helper is input_number.klappe1_offset_su_helper. That number is stored as a string, I believe. At least that’s what most forum entries say.

I’m trying to add those minutes to the next sunset time. I tried to to create a template like this:

  - trigger:
      - platform: time
        at: '00:00:00'
      - platform: event
        event_type: event_template_reloaded
      - platform: homeassistant
        event: start
    sensor:
      - name: "Klappe1 mit Offset"
        unique_id: "klappe1_sunrise_with_offset"
        state: "{{ state_attr('sun.sun', 'next_setting') | as_datetime + (states('input_number.klappe1_offset_su_helper')|int * 60))|timestamp_custom('%H:%M:%S', false)}}"

This throws me an error: “TypeError: can only concatenate str (not “int”) to str”. I tried to find a solution in the forums, but after a day I’m stuck. Does anyone have an idea and can point me in the right direction? How do i convert the string to an integer and add it to the datetime?

Any help would be greatly appreciated, thanks in advance!
Jan

        state: >
          {{ (state_attr('sun.sun', 'next_setting') | as_datetime + 
             timedelta(minutes = states('input_number.klappe1_offset_su_helper') | int(0))).timestamp() 
             | timestamp_custom('%H:%M:%S', false) }}

Copy-paste it into the Template Editor and confirm it doesn’t contain any errors and reports the desired result.

2 Likes

wow, great. Thanks a lot, that’s a good start. It does add the minutes to the UTC time.

This does show the local sunset, 2022-07-03 21:49:16.111203+02:00

state: >
          {{ state_attr('sun.sun', 'next_setting') | as_datetime | as_local  }}

This shows 20:19:16 as a result, I’m adding 30 Minutes.

state: >
          {{ (state_attr('sun.sun', 'next_setting') | as_datetime | as_local  + 
             timedelta(minutes = states('input_number.klappe1_offset_su_helper') | int(0))).timestamp() 
             | timestamp_custom('%H:%M:%S', false) }}

any idea why?

Have you tried setting this to ‘true’? Nevermind, dumb idea, haven’t read the complete posts, sorry for confusion! :slight_smile:

1 Like

Do you want to show the time as local or in UTC?

That’s what is controlled by the second argument in your timestamp_custom function (true/false).

1 Like

Thanks guys, you all made my day, that’s that. This was driving me crazy and now I feel i should have somehow found that out myself… :slight_smile:

For future reference, the full code is:

state: >
          {{ (state_attr('sun.sun', 'next_setting') | as_datetime | as_local  + 
             timedelta(minutes = states('input_number.klappe1_offset_su_helper') | int(0))).timestamp() 
             | timestamp_custom('%H:%M:%S', true)}}
2 Likes

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information refer to guideline 21 in the FAQ.