Assign current date and time to my input_datetime

Hello!

How can I assign current date and time to my input_datetime?

This works for static date and time:

type: picture
image: 'https://demo.home-assistant.io/stub_config/t-shirt-promo.png'
tap_action:
  action: call-service
  service: input_datetime.set_datetime
  service_data:
    entity_id: input_datetime.jacu
    datetime: '2001-10-11 10:20:11'

This does not work for current date and time:

type: picture
image: 'https://demo.home-assistant.io/stub_config/t-shirt-promo.png'
tap_action:
  action: call-service
  service: input_datetime.set_datetime
  data_template:
    entity_id: input_datetime.jacu
    datetime: '{{ (as_timestamp(now()) | timestamp_custom("%Y-%m-%d %H:%M:%S", true)) }}'

Thanks,

Botond

Hey Botond and welcome to the community :partying_face:

Seems like overlooked the examples in the documentation, there’s one for your exact use case (the second last one):

  # Sets date and time to date and time from datetime object (current date and time in this example)
  - service: input_datetime.set_datetime
    entity_id: input_datetime.date_and_time
    data_template:
      datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
1 Like

Hi fedot, thanks for your reply. I certainly tried it on the base of documentation, but did not work.

Expected a value of type {action,navigation_path,url_path,service,service_data} | undefined for tap_action.entity_id but received "input_datetime.jacu".

I tried this:

type: picture
image: 'https://demo.home-assistant.io/stub_config/t-shirt-promo.png'
tap_action:
  action: call-service
  service: input_datetime.set_datetime
  entity_id: input_datetime.jacu
  data_template:
    datetime: '{{ now().strftime(''%Y-%m-%d %H:%M:%S'') }}'

Any help would be appreciated.

Botond

Ah yes, the frontend sometimes requires an entity_id even if you don’t need it.
Try the one you’ve just posted, add entity_id: sun.sun (eg. below “type”)

Hi fedot,

I added sun entity below type, but it does not work either. My code is:

type: picture
entity_id: sun.sun
image: 'https://demo.home-assistant.io/stub_config/t-shirt-promo.png'
tap_action:
  action: call-service
  service: input_datetime.set_datetime
  entity_id: input_datetime.jacu
  data_template:
    datetime: '{{ now().strftime(''%Y-%m-%d %H:%M:%S'') }}'

Error message right below the code editor:
Expected a value of type undefined for entity_id but received "sun.sun".

Error message when clicked on the image:
Failed to call service input_datetime.set_datetime, must contain at least one of entity_id, area_id.

Oh, I’m sorry, it’s a picture card, not a picture entity card. Try this please:

type: picture
image: 'https://demo.home-assistant.io/stub_config/t-shirt-promo.png'
tap_action:
  action: call-service
  service: input_datetime.set_datetime
  data_template:
    datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
    entity_id: input_datetime.jacu

Thank you, fedot!

Unfortunately still does not work:

Expected a value of type {action,navigation_path,url_path,service,service_data} | undefined for tap_action.data_template but received {"datetime":"{{ now().strftime('%Y-%m-%d %H:%M:%S') }}","entity_id":"input_datetime.jacu"}.

Yes, you’re right.
I just looked it up again, you can’t template in Lovelace.
Create a new script that calls the service with the data_template and then call the service from the tap_action. Sorry for the inconvenience :frowning_face:

Dear fedot, thanks for your effort! :slight_smile:

1 Like

An alternate way to doing this:

    datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"

is this:

    datetime: "{{ now().timestamp() | timestamp_local }}"

or even this:

    datetime: "{{ (now() | string)[:19] }}"

4 Likes

Dear Taras,

unfortunately this does not work either.

Error message is the same as above.

Thank you! :grinning:

Yes, he’s talking about the template you have to use in your script. You can’t template in Lovelace, as I said.
Need a few hints for the script? Can write it for you if you want to

Dear Botond,

The templates I posted do work and the evidence is in the screenshot.

What does not work is attempting to use any template with tap_action. As fedot mentioned, it doesn’t support templating.

Simply use the template in a script then have tap_action call the script. It’s easy.

1 Like

Hello guys,

thanks for info, it works with script!

Good to know that templating does not work in Lovelace.

Have a nice day!

That’s a generalization and it doesn’t reflect the facts. The ability to support templating is not determined by Lovelace but by the card used.

The current set of standard cards don’t support templating for tap_action. However, there are various custom cards that do support templating. For example, the Auto-Entities card.

1 Like

I don’t know how it works, but it worked!! Thank you, search took very long.

Found this old thread.
I am trying to do the same thing but actually with a script.
Somehow it is 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

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

Thank you @123 , but that script also cannot be saved. :frowning: It does not show an error but it is not stored and hence cannot be run (not even in the UI editor).

So half a step closer but not there yet.

And why does it not throw an error for your code?
Mine is from the docs. Are the docs outdated or is my syntax wrong? I cannot see any problem.

I was able to create/save/store both examples (the one from the documentation and mine). I don’t know why you’re encountering a problem saving them.