Correct way to represent a Time (HH:MM) in a custom component

Hi All,

I’ve seen various discussions on time formatting etc. but I can’t align any of them to my specific use case, which I know is simple, but I can’t work out the “proper” way to do it.

The short version
In a custom sensor, what is the correct “Unit of measurement” and “Device class” to use for a string that is in HH:MM format?
Every time I try to convert it to a timestamp I just get an “Invalid timestamp” error (when I use DEVICE_CLASS_TIMESTAMP) and I can’t find the correct unit of measurement to use as there only seems to be TIME_HOUR or TIME_MINUTE nothing for TIME on it’s own.

The background/Long version
I’m adding new functionality to the “Growatt” sensor in HASS. The sensor works by scraping information from the external growatt server online, one of the values I want to add is the last time that the server received an update from the hardware.

The value that I’m able to get from the server is the last X-axis value from a chart that gets returned and this is in the format HH:MM.
All of the existing sensor types in the component are in this format:

"mix_import_from_grid_today": (
        "Import from grid today (load)",
        ENERGY_KILO_WATT_HOUR,
        "etouser",
        {"device_class": DEVICE_CLASS_ENERGY},
    ),

I’m looking to add one like this:

"mix_last_update": (
        "Last Data Update",
        NOT_SURE_WHAT_TO_PUT_HERE,
        "lastdataupdate",
        {"device_class": DEVICE_CLASS_???},
    ),

The only way I’ve got it to work without throwing an invalid timestamp error is to leave both fields empty and it is essentially just treated as a string. It works, but it doesn’t feel like the “right” way of doing it.

Thanks in advance.

My personal opinion is that you should just leave them blank (i.e., the sensor should not have unit_of_measurement or device_class attributes.) If you want a precedence, take a look at sensor.time and input_datetime.

@pnbruckner Thanks for the assist. It was my hope that that was the “correct” approach, I really needed someone other than myself to validate it though :slight_smile:

My end-goal (personally) is to be using the Prometheus exporter and have everything shown in Grafana. This particular value will be used purely to send me an alert if the server hasn’t been updated in the last 30 minutes (it is supposed to receive an update from the Growatt system every 5 minutes) so I’m only ever going to use it for a string comparison in AlertManager, but I want to do right by the HASS project to make sure everyone’s needs are accounted for, not just my own!

Thanks again,

C

I wanted to circle back to this now that the pull request that was behind it has been merged in.

As discussed here: Add 'mix' system support for Growatt integration by muppet3000 · Pull Request #49026 · home-assistant/core · GitHub

The preferred way to do this from the perspective of the code maintainers is to use timestamp for the device class and then None for the unit.

Importantly all date/timestamps should be in UTC and isoformatted.