Input_datetime history - potential bug?

Hi everyone,

I’m using an input_datetime to store the last time of a motion sensor trigger (due to some unique requirements, this is likely the best way to implement it).

The action looks like this:

action:
- service: input_datetime.set_datetime
  data_template:
    timestamp: '{{ now().timestamp() }}'
  entity_id: input_datetime.my_motion_triggered_time

When i check the device history tab however, while there are correct entries for each update (when the change happened), every entry indicates "Changed to " and then the current date time. Whenever the automation is triggered, every entry is updated to the current date and time.

I suspect that the literal string '{{ now().timestamp() }}' is being put into the database, and then only evaluated on retrieval (in the history tab).

I want to write a PR, but would like confirmation that there isn’t just something wrong with my configuration.

I’m running core-2021.2.1 on HASSOS 5.10.

Thanks.

This looks like a bug in the frontend.

I did a quick test. I created a script that set an input_datetime just like you do. If I run the script I see that the service is called with the timestamp being a number resulting from the template being evaluated. And I see the input_datetime state changing accordingly.

Next, if I open the “more info” dialog for the input_datetime, I see the input_datetime.set_datetime service being called, twice, and the input_datetime’s state changing when it shouldn’t be!

Here is what happens when the script is run:

2021-04-05 14:36:56 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=script, service=turn_on, service_data=entity_id=script.test>
2021-04-05 14:36:56 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event script_started[L]: name=test, entity_id=script.test>
2021-04-05 14:36:56 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=script.test, old_state=<state script.test=off; last_triggered=2021-04-05T14:35:04.873679-05:00, mode=single, current=0, friendly_name=test @ 2021-04-05T14:35:04.886389-05:00>, new_state=<state script.test=on; last_triggered=2021-04-05T14:36:56.067788-05:00, mode=single, current=1, friendly_name=test @ 2021-04-05T14:36:56.067920-05:00>>
2021-04-05 14:36:56 INFO (MainThread) [homeassistant.components.script.test] test: Running script sequence
2021-04-05 14:36:56 INFO (MainThread) [homeassistant.components.script.test] test: Executing step call service
2021-04-05 14:36:56 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=input_datetime, service=set_datetime, service_data=timestamp=1617651416.069873, entity_id=['input_datetime.test']>
2021-04-05 14:36:56 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=input_datetime.test, old_state=<state input_datetime.test=2021-04-05 14:35:00; editable=True, has_date=True, has_time=True, year=2021, month=4, day=5, hour=14, minute=35, second=0, timestamp=1617651300.0, friendly_name=test @ 2021-04-05T14:35:32.549968-05:00>, new_state=<state input_datetime.test=2021-04-05 14:36:56; editable=True, has_date=True, has_time=True, year=2021, month=4, day=5, hour=14, minute=36, second=56, timestamp=1617651416.069873, friendly_name=test @ 2021-04-05T14:36:56.077197-05:00>>
2021-04-05 14:36:56 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=script.test, old_state=<state script.test=on; last_triggered=2021-04-05T14:36:56.067788-05:00, mode=single, current=1, friendly_name=test @ 2021-04-05T14:36:56.067920-05:00>, new_state=<state script.test=off; last_triggered=2021-04-05T14:36:56.067788-05:00, mode=single, current=0, friendly_name=test @ 2021-04-05T14:36:56.079006-05:00>>

And, believe it or not, here is what happens when the “more info” window is opened on the input_datetime entity:

2021-04-05 14:37:08 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=input_datetime, service=set_datetime, service_data=entity_id=input_datetime.test, time=14:36, date=2021-04-05>
2021-04-05 14:37:08 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=input_datetime, service=set_datetime, service_data=entity_id=input_datetime.test, time=14:36, date=2021-04-05>
2021-04-05 14:37:08 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=input_datetime.test, old_state=<state input_datetime.test=2021-04-05 14:36:56; editable=True, has_date=True, has_time=True, year=2021, month=4, day=5, hour=14, minute=36, second=56, timestamp=1617651416.069873, friendly_name=test @ 2021-04-05T14:36:56.077197-05:00>, new_state=<state input_datetime.test=2021-04-05 14:36:00; editable=True, has_date=True, has_time=True, year=2021, month=4, day=5, hour=14, minute=36, second=0, timestamp=1617651360.0, friendly_name=test @ 2021-04-05T14:37:08.052903-05:00>>

Yes it is a bug, yes it is being worked on

Link to report so I don’t write a duplicate?

It is a complex issue involving multiple bug reports and PRs in both frontend and core