Format a value from sensor in dashboard

Today is my first day with Home Assistant (installed on Synology as Docker image).

I would like to show the value of the sensor swiss_public_transport on my dashboard in an Entities Card. The default output is a datetime value, e.g.: 2023-12-27T20:36:00+0100, but I would like to have it in the format %H:%M.

What I did so far is to create the sensor in Configuration.yaml as described on the integration details page:

sensor:
  - platform: swiss_public_transport
    name: TrainWinterthurZurich
    from: Winterthur
    to: Zurich

To show the formatted value, I thought I’d have to create a template:

template:
  - sensor:
      NextTrainFromWinterthurToZurich:
        friendly_name: "Next Train"
        value_template: "{{ as_datetime(state_attr('sensor.trainwinterthurzurich', 'next_departure')).strftime('%H:%M') }}"

I watched tutorials, searched for solutions or examples but did not find anything that would be close enough to my requirement.

My questions are:

  • Is this the correct way (create a sensor, then a template to use the sensor’s output, then show it in an Entities Card)?
  • Or is there a way to combine the two (sensor, template) into one, and if yes, how would it look like?

Thank you very much in advance!
Martin

What you did is correct. You could also use the Template Entity Row instead of having a second sensor, if you have installed HACS.

You could also do a Markdown card as well as such:

type: markdown
content: >-
  {{ as_datetime(state_attr('sensor.trainwinterthurzurich', 'next_departure')).strftime('%H:%M') }}
title: Next Train

image

1 Like

Thank you @CO_4X4, will check HACS.

I did some more try and error and finally found the correct solution, apparently I did some mistakes in the yaml format and mixed legacy with modern style.

This works:

sensor:
  - platform: swiss_public_transport
    name: TrainWinterthurZurich
    from: Winterthur
    to: Zurich

template:
  - sensor:
      - name: "NextTrainFromWinterthurToZurich"
        state: "{{ as_datetime(state_attr('sensor.trainwinterthurzurich', 'next_departure')).strftime('%H:%M') }}"

Welcome!
Quick extra tip, I like to have the availability attribute linked to the sensor(s) being processed for any integration that calls a third party that could easily fail (like retrieving values from a website/API). It will avoid you the hassle of figuring out potential errors down the road (API goes down for maintenance, etc).

template:
  - sensor:
      - name: "NextTrainFromWinterthurToZurich"
        unique_id: next_train_from_winterthur_to_zurich
        state: "{{ as_datetime(state_attr('sensor.trainwinterthurzurich', 'next_departure')).strftime('%H:%M') }}"
        availability: "{{ has_value('sensor.trainwinterthurzurich') }}"

You can also check the validity of the attribute itself, just in case.

However, I agree with CO_4X4, if it’s just to display a plain, formatted value somewhere (dashboard, notification, etc), you can do it “in place” (markdown card, templated value, etc), instead of creating a “duplicate” sensor. It doesn’t seem like much at the beginning, but if you duplicate sensors for every type of formatting you could want, for every sensor you have, you quickly could end up with hundreds of template sensors… and a headache.
My rule is, if it doesn’t need history tracking, and isn’t involving multiple sensors, then a template sensor is not needed.

2 Likes