Convert entity status from GMT to local time?

Hello,

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

I’m not sure where to start.

Capture

sensor:
  #UPS
  - platform: apcupsd
    resources:
      - status
      - hostname
      - upsname
      - lastxfer
      - xonbatt
      - cumonbatt

I tried adding this template but I keep getting ‘Unknown’ as the output in lovelace

    sensors:
      apc_time_date:
        friendly_name: 'APC Friendly'
        entity_id: sensor.ups_transfer_to_battery
        value_template: "{{ as_timestamp(strptime(state_attr('sensor.ups_transfer_to_battery', 'start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_local }}"

If the answer isn’t in here, you don’t need it:

This seems to work:

{%- 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):

2019-11-02 10:46:41

:slight_smile: I skimmed through that thread but was really lost (I’m not a developer, just a hobbyist I guess).

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') }}

You can fiddle around with the output by reading up on strftime’s parameters.

That is not UTC.

That worked! Thank you!!

Looking at your original post again, what timezone are you in? According to the sensor your timezone is -0500. Is that right?

For anyone who may care in the future:

#configuration.yaml
###################################################################    
# The below template takes the value from sensor.ups_transfer_to_battery 
# and converts it to Nov 02 @ 08:46:41.  Otherwise you get something like
# 2019-11-02 08:46:41 -0500
###################################################################
  - platform: template
    sensors:
      apc_time_date:
        friendly_name: 'APC_Human Readable'
        entity_id: sensor.ups_transfer_to_battery
        value_template: >-
            {%- set time = states.sensor.ups_transfer_to_battery.state -%}
            {%- 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('%b %d %Y @ %H:%M:%S') }}

Capture

1 Like