Set input_datetime to now() +1hr help

Good morning. I’m trying to create an automation that turns a switch off after 1hr. Originally I just put a 1hr delay into my automation, after the switch is turned on but, after reading various topics on here, I realised that if Home Assistant where to restart during this period then the switch would not be turned off.

Instead I have created an input_datetime helper and I’m trying to set the it to now() + 1hr and have that as a trigger. I found this example from different topics.

{{ (now().strftime("%s") | int + (3600)) | timestamp_custom("%H:%M:%S", false) }}

This displays the time + 1hr in the Templating tool but when I trigger the automation the date/time state is 00:00:00

Here’s the action from my automation:-

- service: input_datetime.set_datetime
  data_template:
    entity_id: input_datetime.switch
      time: >
        {{ (now().strftime("%s") | int + (3600)) | timestamp_custom("%H:%M:%S", false) }}

I set up the input_datetime from the Helper GUI and only set it for Time, not for Date.

For completeness, I’ve also tried now().timestamp() but that threw up an ‘unknown error rendering template’.

Any help would be much appreciated.

Still no joy with this. I’ve started again with the Automation Editor. Can anyone let me know if this is the correct way to write in the Templating section please i.e the correct information and indenting?

image

I’ve tried this as well but no joy.

{{ ((as_timestamp(now()) + 3600) | timestamp_custom("%H:%M")) }}

I solved it. There’s a couple of problems actually.

I found this issue that’s been raised #34132 that highlights a problem with the Automation UI. Basically

action: 
- data:
    data_template:
      time: >-

is generated by the UI but it needs to be altered using the File Editor to:-

action: 
- data_template:
      time: >-

The other problem was that I used the Helper UI to generate the input_datetime helper, so even thought I originally wrote the automation in the yaml file, it wouldn’t work. Once I manually created the helper and adjusted the auto-generated automation code, it finally worked.

I need to chill out after a full day of head scratching, so I’ll look at raising these as issues tomorrow :man_facepalming:t2: