Saving part of a state attribute

Hello,

I’m trying to figure out how to save only part of a state attribute into a variable so that TTS can read out a time.

Eg: sensor.noaa_tides

Attributes
low_tide_time: 2023-01-26T14:34
low_tide_height: 1.074

So far:

 {% set
    lowtide =
    state_attr('sensor.noaa_tides', 'low_tide_time') %}

But this reads out the date which isn’t needed as its always the current date. How do I read out anything after the “T” for time?

Much appreciated!

There are a few ways to do this, depending on how you want to approach it…

Using time functions:

{% set lowtide =
(state_attr('sensor.noaa_tides', 'low_tide_time')|as_datetime|as_local).strftime("%H:%M") %}
{{ lowtide }}

Using string manipulation:

{% set lowtide =
state_attr('sensor.noaa_tides', 'low_tide_time').rsplit('T')[1] %}
{{ lowtide }}

hi,

as_timestamp(state_attr('sensor.noaa_tides', 'low_tide_time')) | timestamp_custom('%H:%M') 

extracts time
more information here:

1 Like