My UPS is connected to HA (via NIS) and reports when was the last time it transferred power to battery (i.e. power interruption). I then display that on a card in lovelace. The issue is the UPS sends the info in UTC so it looks like “2019-11-02 08:46:41 -0500”
Is there a way to convert into something more easily readable (November 2nd 2:46 PM)? I don’t have a way to manipulate the source of the data (the UPS) so wondering if I can manipulate the string after it is received by HA
{%- set time = "2019-11-02 08:46:41 -0500" -%}
{# Get the time in the format yyyy-mm-ddTHH:MM:ss-offset #}
{%- set timeParts = time.split(' ') -%}
{%- set newtime = timeParts[0] + 'T' + timeParts[1] + timeParts[2] -%}
{{ as_timestamp(newtime) | timestamp_local }}
Output (my local time is obviously different from yours):
Hi- thanks for your help.
I’m not following how I can take the output from sensor.ups_transfer_to_battery and pass it through what you wrote to get the time to translate. At least as far as I understand.
I’m not looking to convert current time/date into human readable. I’m looking to convert the time passed from sensor._ups_transfer_to_battery to an easy to read format
I’m not sure it needs to be this complicated, but this works. Perhaps somebody more familiar with datetime stuff can comment. You should replace the time variable with state_attr('sensor.ups_transfer_to_battery', 'start_time')
{%- set time = state_attr('sensor.ups_transfer_to_battery', 'start_time') -%}
{# Get the time in the format yyyy-mm-ddTHH:MM:ss-offset #}
{%- set timeParts = time.split(' ') -%}
{%- set newtime = timeParts[0] + 'T' + timeParts[1] + timeParts[2] -%}
{%- set localtime = as_timestamp(newtime) | timestamp_local -%}
{%- set datetime = strptime(localtime, '%Y-%m-%d %H:%M:%S') -%}
{{ datetime.strftime('%d %B %Y @ %H:%M:%S') }}