Show time as 'hh:mm' on a card in Lovelace

Goodmorning,
As a NOOB Home Assistant user I do have the following question:
Is it possible to show de duration of an online machine (like an Xbox with the Xbox Addon) in hours:minutes on the Lovelace interface?

I’ve created a binary_sensor for the Xbox Media Player with the following configuration:

binary_sensor:
 - platform: template
   sensors:
   xboxone:
    friendly_name: "Xbox"
    entity_id: media_player.xbox_one
    value_template: >-
      {{ not is_state('media_player.xbox_one', 'off') }}

Now it is visible in Lovelace, but shown as ‘h’. I would like to show the value in Lovelace, like:

timestamp_custom(’%Y/%m/%d at %H:%M’)

so for example for mine agenda i use this:

value_template: >
          Date: {{ as_timestamp(states.calendar.xxxxxx.attributes.start_time) | timestamp_custom('%Y/%m/%d at %H:%M') }}

Thanx, Timx, that was the trick!

Doesn’t work for my duration-of-last-night’s-sleep-sensor.

type: entity
entity: sensor.wie_lang_zuletzt_geschlafen
value_template: >
          Date: {{ as_timestamp(sensor.wie_lang_zuletzt_geschlafen) | timestamp_custom('%Y/%m/%d at %H:%M') }}

“value_template” should be defined inside a template sensor.
Entity card does not support templates.

1 Like

I’m still struggling to implement this for the following template sensor. It looks like this in configuration.yaml:

sensor: 

  - platform: history_stats
    name: Wie lang zuletzt geschlafen?
    entity_id: timer.ich_schlafe
    state: "active"
    type: time
    start: '{{ now().replace(hour=21).replace(minute=0).replace(second=0) - timedelta(days=1) }}'
    end: '{{ now().replace(hour=15).replace(minute=0).replace(second=0) }}'
  

and its entity id is sensor.wie_lang_zuletzt_geschlafen

This is not a template sensor, this is a history_stats sensor.

Okay, but still, how do I convert this into hh:mm?

I have tried a variant in which I put the value template right below the history stats sensor like so:

sensor: 
  - platform: history_stats
    name: Wie lang zuletzt geschlafen?
    entity_id: timer.ich_schlafe
    state: "active"
    type: time
    start: '{{ now().replace(hour=21).replace(minute=0).replace(second=0) - timedelta(days=1) }}'
    end: '{{ now().replace(hour=15).replace(minute=0).replace(second=0) }}'
    value_template: >
          Date: {{ as_timestamp(states.wie_lang_zuletzt_geschlafen) | timestamp_custom('%Y/%m/%d at %H:%M') }}

But that didn’t do anything.

I’ve also tried putting it in a separate template but that just throws a bunch of errors I don’t understand.

  - platform: history_stats
    name: Wie lang zuletzt geschlafen?
    entity_id: timer.ich_schlafe
    state: "active"
    type: time
    start: '{{ now().replace(hour=21).replace(minute=0).replace(second=0) - timedelta(days=1) }}'
    end: '{{ now().replace(hour=15).replace(minute=0).replace(second=0) }}'
    
  - platform: template
    sensors:
    friendly_name: "test"
    entity_id: sensor.wie_lang_zuletzt_geschlafen
    value_template: >
              Date: {{ as_timestamp(sensor.wie_lang_zuletzt_geschlafen) | timestamp_custom('%Y/%m/%d at %H:%M') }}

I’m not gonna understand it like this. I have no idea what I’m doing, the templating docs are gibberish to me and make my brain explode. I just need the solution which I can then use for reference in similar situations.

And even if I was capable of learning templating, I just find that with the amount of information one seemingly has to understand, it goes against the purpose of home automation which is to save time, at least to me and to a certain part of the community.

Assume your source sensor is created by a “history_stats” integration with “type: time”. Then the sensor has a “device_class: duration”.
Displaying sensors with “device_class: duration” is glitchy in Frontend:

  1. In different cards they may be shown in a different way (issue).
  2. Sensors with same “device_class: duration” in the same Entities card may be shown differently - “history_stats”-sensor is shown as “xx.xx” (as a number), “template”-sensor is shown as “hh:mm:ss” (issue).

So, if you wish to show a “history_stats” sensor in “hh:mm:ss”-like way - your possible options are:

  1. Create a template sensor: take a value of that “history_stats” sensor & convert it to a needed format using templates (to have a nice look everywhere in UI).
  2. Use a custom:template-entity-row (to have a nice look in some particular Entities card).

Update: temporarily removed my template from the post, found an error.
Hours (if uom=hour) must be represented as hh:mm:ss, made a template but it was wrong.
Next try:

  - entity: sensor.day_vs_night_time
    type: custom:template-entity-row
    state: >-
      {{ timedelta(hours = states(config.entity)|float(default=0)) }}

изображение
where “day vs night” (name is confusing) is actually always a curr time (made this sensor just for testing):

sensor:
  - platform: history_stats
    name: "day vs night (time)"
    entity_id: sun.sun
    state:
      - "above_horizon"
      - "below_horizon"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"
1 Like

What other products did you test before choosing Home Assistant to automate your home?

The reason why I ask is because the challenges you are experiencing suggest
Home Assistant may not be the best choice for your needs and expectations.

Home Assistant requires a significant investment of one’s time and effort to learn its features, functions, and conventions. This is especially true when you venture into creating entities via YAML and making Jinja2 templates (that’s two languages you have to learn).

For example, what you tried here doesn’t even come close to the example in the documentation and has at least four errors.

  - platform: template
    sensors:
    friendly_name: "test"
    entity_id: sensor.wie_lang_zuletzt_geschlafen
    value_template: >
              Date: {{ as_timestamp(sensor.wie_lang_zuletzt_geschlafen) | timestamp_custom('%Y/%m/%d at %H:%M') }}
  1. Incorrect YAML indentation (starting at the third line).
  2. Incorrect configuration format (missing object_id on the third line).
  3. Invalid option (“entity_id”).
  4. Missing quotations (for the sensor’s entity_id in the template’s as_timestamp()).

Although not an error, you chose to define the Template Sensor using the old format (called “legacy” format) instead of the new format (sometimes called “modern” format). My advice is to use modern format (or create a Template Sensor helper) because it will receive enhancements whereas legacy will not.

1 Like

What other products did you test before choosing Home Assistant to automate your home?

Chinese Wireless Hue bulbs and the Philips Hue Hub. Throughout the years, I’ve read reviews for a lot of automation platforms as I was getting frustrated with HA time and time again but the reviews were contradictory and didn’t inspire much confidence and most enthusiasts recommended HA. At the same time, the community kept saying HA is gonna become less buggy and easier to use over time, so I was thinking, it probably wouldn’t be worth it to completely redo my smart home just for testing something that’s maybe better than HA for me.
I’ve researched Apple Homekit, Samsung Smart Things and Hubitat.

Home Assistant requires a significant investment of one’s time and effort to learn its features, functions, and conventions

Reviews didn’t make the impression it was that messy and complicated and to be honest, I think a platform that a lot of times goes against the, as I think, average consumer’s idea of home automation, which is to save time, should clearly communicate it’s that type of platform upfront.

That’s useful for lighting control (I still use a Philips Hue Hub), meant for consumers, easy to use, but is only a fraction of what “home automation” represents. I wouldn’t classify it as a home automation system.

Apple’s Homekit is targeted for consumers. The other two for consumer/hobbyists (meaning people willing to invest more of their free time to learn and tinker). All three are easier to use than Home Assistant (but not necessarily as capable). I think the best choice for one’s needs strikes a balance between power and ease of use.

I can’t comment on a review I haven’t seen or read but if it didn’t cover templating and YAML-based configuration (what you’re currently having difficulty with) then the review didn’t cover advanced topics.

Here’s a question you should consider: is it any easier to achieve your goals using another product? For example, can you easily display time in a desired format for the equivalent of a History Stats sensor in some other product?

If the answer is that the other product doesn’t even have the equivalent of a History Stats sensor, then you know that your requirements are more advanced than what other products can achieve. Effectively, your requirements exceed what others consider to be typical.

Your home automation goals dictate the tool required to get the job done. If you can’t dedicate time to learn the tool, you either find an easier tool, if it even exists, or scale back your requirements.

FWIW, one the best “reviews” for a product is to observe its community support forum. What kind of difficulties do its users encounter? What are the common questions? The answers to these questions reveal the product’s true strengths and weaknesses.

1 Like