frontend icon indicating copy to clipboard operation
frontend copied to clipboard

Add relative timestamp state_content for input_datetime

Open MrEbbinghaus opened this issue 10 months ago • 7 comments

Proposed change

Currently the timestamp attribute of input_datetime entities is displayed as a simple number: image

While sensors with device_class: timestamp display a relative time string: image

This PR adds a relative timestamp to the state_content selector of the input_datetime entity: image image

Note that it doesn't replace the timestamp, it just adds another option. A freely configurable "format selector" per datatype would be better, but that would mean bigger changes. (see https://github.com/home-assistant/frontend/discussions/20618)

I implemented this by adding a map that allows an attribute to add multiple additional options to the option list.

While I was at it, I removed the digit grouping of the timestamp and year attributes, as they didn't make sense. I thought about using the hour, minute, second, day, month, year to render a single nicely formatted option, but that would be beyond the scope of this PR.

Type of change

  • [ ] Dependency upgrade
  • [x] Bugfix (non-breaking change which fixes an issue)
  • [x] New feature (thank you!)
  • [ ] Breaking change (fix/feature causing existing functionality to break)
  • [ ] Code quality improvements to existing code or addition of tests

Example configuration

type: tile
entity: input_datetime.bjorn_nachster_wecker
name: Wecker
state_content:
  - timestamp_relative
  - timestamp
  - year

Checklist

  • [x] The code change is tested and works locally.
  • [x] There is no commented out code in this PR.
  • [ ] Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

MrEbbinghaus avatar Feb 08 '25 20:02 MrEbbinghaus

Hi @MrEbbinghaus,

Your timestamp_relative label won't get ever translated, because this labels uses core translations and there it's not defined

wendevlin avatar Feb 14 '25 10:02 wendevlin

Thank for the contribution. I'm not sure we should add another option. I would prefer to format the timestamp correctly instead of using option. I'm also in favor of adding a format option to every card to let the user choose between relative or absolute.

piitaya avatar Feb 14 '25 10:02 piitaya

@piitaya I agree that this change is kind of tacked on and a general formatting system would be better.

In principle, it feels bad, to change the representation of an attribute so dramatically. In this case, however, I guess it would be fine, as I don't think that there are currently any users who would prefer the raw number format.

(Who came up with the idea that a timestamp should be represented as a relative time anyway..?)


Speaking of formatting, I think some general templating support would be great. This would allow users to format values however they like.

For known predefined attributes, the UI could still provide a selector that just sets a template internally. This could simplify the definition of format development, as you would just have to map (domain, attribute, format) => template. (Which could even be easily extended/modified by user configuration, since it is just data.)

Considering your work on lovelace-mushroom I think this is not a new idea. 🙂


@piitaya I guess it is up to you to decide. Either:

  • I update this PR to just change the formatting of timestamp.
  • Or we drop this change entirely in favour of a future formatting overhaul and we just cherrypick 088097f66379715114a805c2af4ee0c1ac66b36b.

MrEbbinghaus avatar Feb 19 '25 14:02 MrEbbinghaus

Related https://github.com/home-assistant/frontend/pull/22339

bramkragten avatar Mar 26 '25 10:03 bramkragten

Do I understand correctly that we have this today? Thanks for this PR to improve it!

Currently the timestamp attribute of input_datetime entities is displayed as a simple number: image

Time should be easy to read. Depending on the use case, it should be either relative or absolute. This choice should be up to the dashboard creator, so I agree with @piitaya that we should offer both options. For example, in the case of an alarm clock, I would prefer to see the exact time rather than the relative time.

Right now, we assume that relative time is the most common choice and use that for our dashboards. If we have to set a default, let's go with relative time.

matthiasdebaat avatar Mar 28 '25 14:03 matthiasdebaat

Sorry for the late answer. Let's format timestamp in relative for now as we format timestamp sensor in this format right now. We are looking to provide custom date formatting with https://github.com/home-assistant/frontend/pull/22339 as @bramkragten said.

piitaya avatar Mar 28 '25 15:03 piitaya

@piitaya I made the requested changes.

MrEbbinghaus avatar Apr 22 '25 11:04 MrEbbinghaus