Displaying a formatted state_attr on a card/lovelace

I want to display some time information extracted from a UTC state attribute on a card, in HH:MM format. In the dev tools template section, this works fine:

{{(state_attr('binary_sensor.octopus_energy_a_999999a_intelligent_dispatching', 'current_start'))| as_timestamp | timestamp_custom('%H:%M') }}

The original attribute value was :
15 February 2024 at 23:30:00
and the code above displays “23:30”, which is what I want. I’m struggling to see how I can display that on a card. I tried this in the config.yaml:

  - sensor:
      - name: "OI Current Start"
        state: >
          {{(state_attr('binary_sensor.octopus_energy_a_999999a_intelligent_dispatching', 'current_start'))| as_timestamp | timestamp_custom('%H:%M') }}
        unit_of_measurement: "time"

Am I barking up the wrong tree? Thanks!

Only a little. You need to create a template sensor, which can be done in the UI via Devices & Services, Helpers - prob easier and quicker than putting in config.yaml. just paste your template code in the setup.

This kind of sensor would not have a unit of measure either as time is not one.

So, I did try that. I chose ‘template helper’ and then both as a sensor and a binary sensor. All I get is an unknown, I’m assuming I’m doing something wrong here.

Unit of measurement is blank
device class as time (also tried blank)
state class blank

For the codes I’ve tried these:
{{(state_attr(‘binary_sensor.octopus_energy_a_9999999a_intelligent_dispatching’, ‘current_start’))| as_timestamp | timestamp_custom(‘%H:%M’) }}

and

value_template: “{{(state_attr(‘binary_sensor.octopus_energy_a_9999999a_intelligent_dispatching’, ‘current_start’))| as_timestamp | timestamp_custom(‘%H:%M’) }}”

This should work fine. The unknown is that is cannot find the sensor or attribute. Have you copied straight from what was working in your dev console? Leave all device classes and uom as blank.

1 Like

Yes, copied straight across, everything blank. It just isn’t having it.

Hmm. Maybe post a screenshot of the entity state from dev tools showing all attributes.

It did work, and the reason why it didn’t appear to work is the sensor was only refreshing on a change, not when I changed the code. Thanks for your help!