How to set input helper 'datetime' to current time/now()

Hello everyone,
I have created an input_datetime and would like the initial state to be the current time.

Is this possible somehow?
And would it also be possible to somehow reset it to now using a button (e.g. a service call or something)?

Simple input_datetime where I would like the end_time to be current time.

input_datetime:
  start_datetime:
    name: start_datetime
    has_date: true
    has_time: true
  end_datetime:
    name: end_datetime
    has_date: true
    has_time: true

Thank you
Alex

If I recall correctly, the initial field is not templatable. A workaround would be to use an automation that resets the helper on every HA restart and/or a specific time.
Create a small script and call it in both automation action and button action:


service: input_datetime.set_datetime
data:
  datetime: "{{ now() }}"
target:
  entity_id: input_datetime.xyz

2 Likes

I stumbled across this old thread because I could not get the fix examples working.

Does your code work for you?

Tested, not working:

alias: New Script
sequence:
  - service: input_datetime.set_datetime
    data:
      datetime: "{{ now().timestamp() | timestamp_local }}"
    target:
      entity_id: input_datetime.end_datetime
mode: single

and

alias: New Script
sequence:
  - service: input_datetime.set_datetime
    data:
      timestamp: "{{ now().timestamp() }}"
    target:
      entity_id: input_datetime.end_datetime
mode: single

I’ve tested successfully with:


alias: 1 Test set datetime now
sequence:
  - service: input_datetime.set_datetime
    data:
      datetime: "{{ now() }}"
    target:
      entity_id: input_datetime.both_date_and_time
mode: restart

1 Like

Since


    data:
      timestamp: "{{ now().timestamp() }}"

is also working in my setup, maybe something went wrong with your script. Where did you create it? UI or packages/ extra file?

1 Like

UI only so far. And it does not save.

Does the entity id already exist?

Same as with mine. I gut save and nothing happens.

Starting to think there is a system problem.

Rename your script in scripts.yaml, reload and try again.

Deleted scripts. yaml (I used packages approach for hand made scripts), restarted and created script via ui again. Scripts.yaml gets created with new script but does not show in ui.

Very, very strange.

Again: Have you tried to rename your script in scripts.yaml and then reloaded?

Tried all kinds of names including yours :slight_smile:
Will try creating dedicated yaml in packages next. Maybe the mix is no longer working properly.

Could be worth to raise an issue.

Depends on if this occurs for others also. You have no issues via UI?

Working as a package. Will have to test more to know if it is a problem with my machine or not.

script:
  end_datetime_reset:
    alias: End Datetime Reset
    sequence:
      - service: input_datetime.set_datetime
        data:
          timestamp: "{{ now().timestamp() }}"
        target:
          entity_id: input_datetime.end_datetime
    mode: single

No. I’m running Supervised - but I jumped from 2022.10.4 to 2022.11 beta skipping core-2022.10.5. Maybe 10.5 has a problem?

You’re not alone:

Thanks. Kind of surprising