Timestamp formatting

Hi,

I am playing around with an alarm clock automation and I want the automation to start 15 minutes before the wake up time set (slowly increasing the light).

I would have used the following format when using the time set to start the automation action:

- id: '7'
  alias: Wake up
  trigger:
  - platform: time
    minutes: /1
    seconds: 0
  condition:
  - condition: template
    value_template: '{{ now().strftime("%H:%M") == states.sensor.wake_up.state }}'

When offsetting the time fifteen minutes I simply add fifteen minutes to the current time when testing it against the alarm time I use the following instead:

- id: '7'
  alias: Wake up
  trigger:
  - platform: time
    minutes: /1
    seconds: 0
  condition:
  - condition: template
    value_template: '{{ (now().strftime("%s") | int + (15*60)) | timestamp_custom("%H:%M") == states.sensor.wake_up.state }}'

Surprisingly, the second example renders a time to test against the sensor state that is not 15 minutes later than the first but 1 hour and 15 minutes later.

This means that if I test the template

{{ now().strftime("%H:%M") }}

and it renders 20:00

then the template

{{ (now().strftime("%s") | int + (15*60)) | timestamp_custom("%H:%M") }}

renders 21:15.

Can anyone explain why?

I can of course offset this by using

{{ (now().strftime("%s") | int - (45*60)) | timestamp_custom("%H:%M") }}

which renders in the same case 20:15, which is the desired time.

I just want to make sure that the difference is not an effect of time zone and that it will change if we move from summer to winter time.

works fine on mine:

This is my output:

20

The hour in difference is clear…strange.

I really don’t know other than you tell you I just exactly replicated your code above again and the output was exactly correct.

Have you checked your time and time zone info to make sure that HA matches your computer and your real world time?

Tried on my iPhone

Same result.

Thank you for trying.

{{ (now().strftime("%s") | int + (15*60)) | timestamp_custom("%H:%M", false) }}

Without the false, it’s based on unix_time not local time. I’m guessing your timezone is +1? If not, you’re time is screwed up on your system and you need to make sure your OS time is set properly and your timezone is set properly in your config.

1 Like

Perfect! Solved!