Search code examples
home-assistant

Unable to format timestamp sensor in home assistant / hass.io using strptime function


I am trying to display a sensor state value, which is of type 'timestamp' formatted as %H:%M but not able to get the strptime function to work the way I expect it.

e.g. in the Dev Tools when i add the below to the 'TEMPLATE' section

sunrise {{ strptime(state_attr("sun.sun", "next_rising"), "%H:%M") }} 

it still displays the output as:

sunrise 2020-01-20T19:05:26+00:00 

Solution

  • Per Home Assistant Templating documentation:

    • strptime(string, format) will parse a string to a datetime based on a format.

    So that's not exactly what you need.

    You may want to apply timestamp_custom(format_string, local_boolean) filter to convert a timestamp to a custom format, e.g:

    {{ as_timestamp(state_attr("sun.sun", "next_rising")) | timestamp_custom('%H:%M') }}