Input_datetime not able to set values

I have created a sensor for storing the last boot date and time:
input_datetime.boot_time

But I am unable to load the time and date using the service “set_datetime”

  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'time_date'
      - 'time_utc'

 - service: input_datetime.set_datetime
    data_template:
      entity_id: input_datetime.boot_time
      datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"

could someone tell me what i am doing wrong?

There’s a last boot sensor in system monitor:

- platform: systemmonitor
  resources:
  - type: last_boot

Yes, it is true, but the information reported by last_boot is the last cold-start. If you perform a manual reboot, th value is not updated.

Anyway, I would like to know why the service as I’ve configured is not working.
Thanks,
eHc

Did you create the input_datetime in the Helper UI? I had this same problem last week, where I couldn’t change the state. When I cleared the browser cache though, it was working fine!

Worth a shot :+1:

This part here:

 - service: input_datetime.set_datetime
    data_template:
      entity_id: input_datetime.boot_time
      datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"

is that the way it appears in configuration.yaml? Alone and not part of any script or automation? Because if that’s the case, it’s incorrect.

Actually is part of an automation. For testing purposes I’ve configured a input_boolean sensor:

- id: '1590487207514'
  alias: ultimo_reboot
  description: guarda fecha y hora del ultimo reboot
  trigger:
  - entity_id: input_boolean.swi_1
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - service: input_datetime.set_datetime
    data_template:
      entity_id: input_datetime.boot_time
      datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"

Was input_datetime.boot_time created via Configuration > Helpers? If not, please post its configuration.

Yes, boot_time was configured via helpers.

After modifying the automation, did you run Configuration > Server Controls > Reload Automations?

I don’t see any syntax or logic errors in your automation. I just created and tested the following automation and it works. It’s fundamentally the same as your automation.

- id: '1590782020381'
  alias: Test
  description: ''
  trigger:
  - entity_id: input_boolean.test
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - service: input_datetime.set_datetime
    data_template:
      datetime: '{{ now().strftime("%Y-%m-%d %H:%M:%S") }}'
      entity_id: input_datetime.test

Screenshot from 2020-05-29 16-05-45

1 Like

Thanks for your tests. I’ve made some more tries, with the following results:

  • I cleared the navigator cache, as Adam suggested, with no noticeable result.
  • If I set the datetime as a string like datetime: '2020-05-30 21:20:05', it works, and the datetime is set.
  • The service input_datetime.set_datetime performs a check of the datetime format, and must be set according the format above, if not, it won’t work.
  • My impression is that there is a problem with the format that the automation is passing to the service, but I don´t know how set it up in other way.
    eHc

I can’t see anything wrong with what you have setup and the fact that Taras replicated your code without fault and given it’s basically exactly the same as the examples in the documentation, shows that there’s a hidden problem here.

I would recommend creating an input_datetime in the configuration.yaml and see if the problem goes away.

One other query… Is the code you posted exactly what is in you automations.yaml? If you try and put a data_template in to the Automations UI, you will inevitable end up with: -

  action:
  - service: input_datetime.set_datetime
    data:
      data_template:
        datetime: '{{ now().strftime("%Y-%m-%d %H:%M:%S") }}'
        entity_id: input_datetime.test

and you need to remove the data: bit.

It seems clear from your code that you have this covered but I thought I’d mention, just in case.

I doubt that but you can check by pasting the following template in the Template Editor and confirm its result:

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

When I execute the script, I get the following error in the log:
Error executing script. Invalid data for call_service at pos 1: Invalid datetime specified: {{ now().strftime("%Y-%m-%d %H:%M:%S") }} for dictionary value @ data[‘datetime’]

Any idea of why it is happening?.

I am now seeing the same problem as ehc… I had a working script which updated an input_datetime when motion was detected (an MQTT message with a specific value), but this stopped working and was giving all sorts of duplication - maybe after the latest update - and my database shot up to 1Gb with thousands of motion notifications, but these were not real.

Anyway, tried to fix it and recreated the automation, but despite the templating showing the correct format, I cannot manage to update the input_datetime, even using “services” via Developer Tools and get the same error as ehc (Error executing script. Invalid data for call_service at pos 1:, etc.)

Any ideas?

Had the same challenge for me was the sollution minus the seconds like this

action:
  - service: input_datetime.set_datetime
    data_template:
      datetime: '{{ now().strftime("%Y-%m-%d %H:%M") }}'
      entity_id: input_datetime.test

it works also for me, thanks for your help.