Hello,
I am working with templates for the first time so forgive me if this seems easy. I usually find the solution by reading the other posts and experimenting with the developer tools but had no luck with this.
I am trying to have the “last changed” information displayed as “x hours/minutes ago” to use it in a mushroom template card.
I read about this in another thread but I thought it was something different in my case.
How can I put the device_class in the template string that I wrote above? Is it possible to have everything in one string?
Thank you for the feedback
I added this to my configuration.yaml. Is this the right way or am I making a mistake? Because I don’t have the entity in the developer tools area after the reboot. I tried sensor: and template: with no luck
@Troon I am using the template card because I need to configure the icon and color with a template, that’s why I cannot use secondary_info in this card and was hoping to get the info with a template
Thanks, the template works now (I was looking for the wrong name in the dev tools, I realized that thanks to the code that you shared), it is now visible in the developer tools.
Still, I have the problem about having the template in my card (it is a different entity but same problem, I added the code in the yaml file as suggested before to this entity too):
Is there a way to write a string that has the result that I want so that I can put it in the mushroom card?
Clearly this: {{(as_timestamp(states.sensor.bagno_ping_last_changed.last_changed) | timestamp_custom('%I:%M %p'))}} does not work properly because I have the time as a result and not “x minutes ago” like it was with the original entity
Thanks, however this card is next to many other mushroom cards and it would be very different to the others resulting in a bad layout. I hope someone that uses mushroom cards can point me in the right direction. I am trying to make it work with a time calculation but I am finding it difficult to edit the templates that I find in other posts to suit my needs. I don’t have enough experience with templates
If you mean using the string in the mushroom card I get this:
If I use a normal mushroom entity card, the state of the sensor that we created is correctly “x hours ago”.
But if I could use a normal entity card, I wouldn’t have a problem because I could just use secondary_info: last-changed with the original entity without needing to create the sensor. The problem is that the mushroom template card requires a template to have the last-changed info in the secondary info spot.
I managed to make it work in the end by editing some code that another forum member shared. Took a while with the dev tools but now it works.
This is the template that works, I leave it here in case anybody else needs it in the future:
{% set sec = as_timestamp(now()) - as_timestamp(states('sensor.bagno_ping_last_changed')) %}
{%set hr = (sec / 3600) | int %}
{%set min = sec / 60 - hr * 60 %}
{% if hr > 1 %}
{{"%d" % (hr)}} ore fa
{% elif hr > 0 and min > 2 and min <30 %}
{{"%d" % (hr)}} ora e {{"%2d" % (min)}} minuti fa
{% elif hr > 0 and min > 30 %}
{{"%d" % (hr)}} ora e mezza fa
{% elif hr > 0 and min > 1 and min <30 %}
{{"%d" % (hr)}} ora e {{"%2d" % (min)}} minuto fa
{% elif min > 2 %}
{{min | int}} minuti fa
{% elif min > 1 %}
{{min | int}} minuto fa
{% elif sec > 0 %}
{{sec | int}} secondi fa
{% endif %}
@Olivier1974 Thank you for your patience and for your help, your suggestion about the sensor at the beginning was crucial to make it work!
Computing the text of a timestamp by hand is cumbersome.
It works but the developer of mushroom should consider to correct the behaviour to display timestamp sensors everywhere like in the entity card.