Format Date as "xx days ago" in the picture elements

Good Morning,
I have created a picture elements in order fell free to format the element as i prefer but i need to convert the datetime shown in the pictuer below as “xx hour/day/month” ago.

image

How can i proceed?

cards:
  - type: picture-elements
    image: /local/images/transparent2.png
    show_header_toggle: false
    elements:
      - type: state-label
        entity: sensor.wcf_thermo_automation_last_triggered
        style:
          top: 20%
          left: 72%
          font-size: 100%
          font-weight: normal

What is this? A custom template sensor you made?

Convert the template sensor to show it in the format you want, or make another template sensor.

Here’s an option if you only want it to show the next highest value time ago. (Borrowed macro from this thread) so it doesn’t say “1 days ago” :stuck_out_tongue:

Might have to change the sensor.wfc_thermo. I just guessed on that one.

sensor:
  - platform: template
    sensors:
      wcf_thermo_automation_last_triggered:
        value_template: >
          {% set last_triggered = states.sensor.wfc_thermo.attributes.last_triggered %}
          {% set time_since = as_timestamp(now()) - as_timestamp(last_triggered) %}
          {% macro phrase(value, name) %}
            {%- set value = value | int %}
            {%- set end = 's' if value > 1 else '' %}
            {{- '{} {}{}'.format(value, name, end) if value | int > 0 else '' }}
          {%- endmacro %}
          {% if time_since < 3600 %}
            {{ phrase(time_since | round(0), 'second') }} ago
          {% elif time_since < 86400 %}
            {{ phrase((time_since / 3600) | round(0), 'hour') }} ago
          {% elif (time_since / 86400) < 7 %}
            {{ phrase((time_since / 86400) | round(0), 'day') }} ago
          {% elif (time_since / 86400) < 30 %}
            {{ phrase((time_since / 86400 / 7) | round(0), 'week') }} ago
          {% else %}
            {{ phrase((time_since / 86400 / 7 / 4) | round(0), 'month') }} ago
          {% endif %}

Keep in mind, it’s not too accurate, especially with the rounding. 1.6 weeks will be rounded to 2 weeks. And it also just uses a hardcoded 30 days per month rather than try to precise calculations…

1 Like

Thanks a lot for your feedback.
I have tried, and i agree with you that is not accurated :slight_smile:

You could always change the instances of round(0) to round(1) to have number such as 1.6 instead of 2…
The other way round would be to use the | timestamp_custom() filter
Filter timestamp_custom(format_string, local_time=True) converts an UNIX timestamp to its string representation based on a custom format, the use of a local timezone is default. Supports the standard Python time formatting options.

Here’s my take on this::

sensor:
  - platform: template
    sensors:
      wcf_thermo_automation_last_triggered:
        value_template: >
          {% set last_triggered = states.sensor.wfc_thermo.attributes.last_triggered %}
          {% set time_since = as_timestamp(now()) - as_timestamp(last_triggered) %}
          {% macro phrase(value, name) %}
            {%- set value = value | int %}
            {%- set end = 's' if value > 1 else '' %}
            {{- '{} {}{}'.format(value, name, end) if value | int > 0 else '' }}
          {%- endmacro %}
          {%- if (as_timestamp(now()) | timestamp_custom('%m',false) | int) - (as_timestamp(last_triggered) | timestamp_custom('%m',false) | int) > 0 -%}
          {{ phrase((as_timestamp(now()) | timestamp_custom('%m',false) | int) - (as_timestamp(last_triggered) | timestamp_custom('%m',false) | int) , 'month')}}, 
          {%- endif -%}
          {%- if (as_timestamp(now()) | timestamp_custom('%d',false) | int) - (as_timestamp(last_triggered) | timestamp_custom('%d',false) | int) > 0 -%}
          {{ phrase((as_timestamp(now()) | timestamp_custom('%d',false) | int) - (as_timestamp(last_triggered) | timestamp_custom('%d',false) | int) , 'day')}}, 
          {%- endif -%}
          {%- if (as_timestamp(now()) | timestamp_custom('%H',false) | int) - (as_timestamp(last_triggered) | timestamp_custom('%H',false) | int) > 0 -%}
          {{ phrase((as_timestamp(now()) | timestamp_custom('%H',false) | int) - (as_timestamp(last_triggered) | timestamp_custom('%H',false) | int) , 'hour')}}, 
          {%- endif -%}
          {%- if (as_timestamp(now()) | timestamp_custom('%M',false) | int) - (as_timestamp(last_triggered) | timestamp_custom('%M',false) | int) > 0 -%}
          {{ phrase((as_timestamp(now()) | timestamp_custom('%M',false) | int) - (as_timestamp(last_triggered) | timestamp_custom('%M',false) | int) , 'minute')}}, 
          {%- endif -%}
          {{ phrase((as_timestamp(now()) | timestamp_custom('%S',false) | int) - (as_timestamp(last_triggered) | timestamp_custom('%S',false) | int) , 'second')}} ago

Gives me (with a fake date)
2 months,1 day,1 hour,25 minutes,41 seconds ago

1 Like

It’s really is hard to improve upon this, and I suppose it depends on how long you think you’ll be monitoring this condition before reset. (over a year ??? !!! )
30 days is the best average you are going to get for a month.
But given the variability, they may just want to stop at weeks as even my feeble interlect can work out that 52 weeks is around about a year. And that 32 weeks (etc.) is a multiple of 4 …
I suppose that some financial triggers might require a particular day, week, calendar month, breakdown - but I can’t think of why that would be relevant to HA. Though the things some people tie into HA has to be seen to be believed as they are really on the edge of reality.

welcome to my world :rofl:

Yeah ! I’m just waiting for a guy with a z wave network of x devices and a zigbee network of y devices wanting to know about local aircraft traffic integrations, as he’s susceptible to em radiation interacting with his central nervous system. :rofl:

I also remember a guy who wanted to monitor his dog’s kennel occupancy with an ultrasonic sensor, till someone had to point out to him that ultrasonics drive dogs crazy ! :rofl:

1 Like