How to use an attribute of an entity to calculate time of last usage?

Hi,

I am trying to create a card that will display how many days ago ago my Xiaomi vacuum cleaner was active for the last time. First thing I tried was to use the history data, but havent managed to get out the correct value.

Different approach would be to use the devices “last used” attribute (YYYY-MM-DDTHH:MM:SS) and subtract it from the current time and date. But I have no clue how to do it.

Really appreciate any ideas or hints…

You should just be able to expose the secondary-info for a normal entity card and it will display the last time it was updated.

type: entities
title: Entities card sample
show_header_toggle: true
entities:
  - entity: alarm_control_panel.alarm
    name: Alarm Panel
    secondary_info: last-changed

image

Or you can make a template sensor that exposes last changed then use the device_class: timestamp


sensor:
  - platform: template
    sensors:
      solar_angle:
        friendly_name: "Last Changed Vacuum"
        device_class: timestamp
        value_template: "{{ as_timestamp(states.sensor.xiaomi_vacume.last_changed) }}"

Not sure what this one looks like as I haven’t done it before.

1 Like

This sensor will give you the days. you need to modify vacuum.vacuum_cleaner with your vacuum’s name.

sensor:
  - platform: template
    sensors:
      last_vacuum:
        friendly_name: "Last Vacuum"
        device_class: timestamp
        value_template: "{{ (as_timestamp(now()) - as_timestamp(states.vacuum.vacuum_cleaner.attributes.clean_stop)) / (3600*24) | round(0) }}"

Thanks a lot! I think I am close, I changed to code, as with device_class timestamp I got “invalid date”:

platform: template
sensors:
last_vacuum:
friendly_name: “Last Vacuum”
unit_of_measurement: days
value_template: “{{((as_timestamp(now()) - as_timestamp(states.vacuum.xiaomi_vacuum_cleaner.attributes.clean_stop)) / (3600*24)) | round(1) }}”

However, the calculations are still not correct. Right now I get a value of 0.17 days, which does not equal the 6,5 hours since the last cleanup…

If you are looking for x days, x hours, x minutes, use the following template:

sensor:
  - platform: template
    sensors:
      last_vacuum:
        friendly_name: "Last Vacuum"
        value_template: >-
          {%- macro gettimestring(tstring, number) %}
          {%- if number > 0 %}
          {%- set ret = '{} {} ' if number == 1 else '{} {}s ' %}
          {{- ret.format(number, tstring) }}
          {%- else %}
          {{- '' }}
          {%- endif %}
          {%- endmacro %}
          {% set time = as_timestamp(now()) - as_timestamp(states.vacuum.vacuum_cleaner.attributes.clean_stop)) %}
          {%- set minutes = gettimestring('minute', ((time % 3600) / 60) | int ) %}
          {%- set hours = gettimestring('hour', ((time % 86400) / 3600) | int ) %}
          {%- set days = gettimestring('day', (time / 86400) | int ) %}
          {{ 'Less than 1 min ' if time < 60 else days + hours + minutes %}

is “last_used” a special attribute of that entity?

I believe it is. I’ve seen that attributes on other vacuums when helping others. I can’t say for certain cause I don’t own one.

Yes:

1 Like

then since that’s the case you can’t use secondary-info since it only exposes last_changed or entity_id (and not both at the same time).

you can, however, use a custom secondary-info component to display the data in lovelace. It will expose pretty much any data you want from any sensor but it only works in the entities card. and works exactly like the regular secondary-info row only better!

You can also use relativetime() to accomplish this in most cases.

See https://www.home-assistant.io/docs/configuration/templating/

That was added in October this year, the methods in this thread are out of date.

1 Like