Relative Time of Binary Sensors

I want to create a table that will show my binary sensors and the last time they tripped in a relative time.

For example, I want the table to show:

Sensor 1, 10 minutes ago
Sensor 2, 4 hours 10 minutes ago
Sensor 3, 6 days ago

On the binary sensors, the data that is logged is “last_tripped_time” which shows the number of seconds ago that the sensor was tripped. How can I use that data to populate a table like I mention above? I believe I can use the relative_time function, but documentation on that seems limited or I simply can’t find it.

{{(as_timestamp(now()) - as_timestamp(states.sensor.upstairs_temperature.last_updated | default(0))) | timestamp_custom("%d")|int-1}} Days {{(as_timestamp(now()) - as_timestamp(states.sensor.upstairs_temperature.last_updated | default(0))) | timestamp_custom("%H")|int-1}} Hours {{(as_timestamp(now()) - as_timestamp(states.sensor.upstairs_temperature.last_updated | default(0))) | timestamp_custom("%M")|int}} Minutes ago

Replace upstairs_temperature with your sensor name
Note: this works for me as I’m in British Summer Time timezone (I had to subtract 1 to days and hours). You may need to adjust for your toimezone

1 Like

Custom-UI can show this on frontend on all entities, looks like this:

111221

configuration.yaml:

homeassistant:
  customize_glob:
    "sensor.*":
      show_last_changed: true
    "binary_sensor.*":
      show_last_changed: true
1 Like

Hi @g0g0,

Not sure what I’m doing wrong, added the lines as per your comments, but no additional info shows
do I need anything else?

You need to “install” Custom-UI first: copy files: state-card-custom-ui.html, state-card-custom-ui-es5.html and state-card-custom-ui.html.gz to folder /config/www/custom_ui/

then add to configuration.yaml:

frontend:
  extra_html_url:
    - /local/custom_ui/state-card-custom-ui.html
  extra_html_url_es5:
    - /local/custom_ui/state-card-custom-ui-es5.html

use “manual method” from here: https://github.com/andrey-git/home-assistant-custom-ui/blob/master/docs/installing.md

Thanks, this seems to do the trick. Only modification from your original post is that you also need a line to call it to use the template, like this:

homeassistant:
  customize_glob:
    "sensor.*":
      show_last_changed: true
      custom_ui_state_card: state-card-custom-ui
    "binary_sensor.*":
      show_last_changed: true
      custom_ui_state_card: state-card-custom-ui

Now, it looks like the show_last_changed function resets every time that I restart HA. Is there any way that it can use the data from the binary_sensor itself?

For example, it shows here 33 min ago, which was the last time I restarted HA.
basement1

But that door hasn’t been opened in 3 days, based on the last_tripped_time
basement2

Any way to have it reference last tripped time field for the last_changed in custom ui?

I wrote a script earlier to check which one of my z-wave sensors have checked in at what time, that kinda of gives the output you mentioned.

https://github.com/skalavala/smarthome/tree/master/jinja_helpers#8-find-out-which-zwave-device-checked-in-at-when

The code might look a bit complicated, but all it is doing is iterating through all my zwave devices, and calculating the time. You could easily put that into a script or automation.

1 Like

That seems pretty complex for the ask…

I see you can reference:

extra_data_template: ${attributes.last_tripped_time}

So from there, there must be a way to tell custom ui to use the value returned here as a relative time, no?

Will you share the code for those cards: I am really interested how to create them…

Sure but which code? its simple sensors displayed in groups

Yes, that code… i am really at the beginning of my knowledge about hass, so every example will help me in this trip…

check group component: https://www.home-assistant.io/components/group/

code example:

sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'

group:
  timedate:
    view: no
    name: ' '
    entities:
      - sensor.time
      - sensor.date
1 Like

Hi @g0g0,

Not sure if you’re able to help with this, similar to above, I want to display the last time an automation triggered.
I tried this to no avail:

    "automation.*":
      show_last_triggered: true
      custom_ui_state_card: state-card-custom-ui

Is it at all possible? Do I need to ask this elsewhere?

Thanks

Just tried it’s not working, on front-end it shows last time switch moved or HA restart, only in details it shows last triggered:
1122112

123123123

Check Custom-UI “extra_data_template”, it can show any additional info available

Thanks mate :slight_smile:

Did you succeed?

Not sure who you’re asking, but I managed to do it myself yes.

would you mind to post your result as I’m trying to do something similar but its not working for me @lolouk44. thx

you can easily do it out of the box if you migrate to Lovelace-ui.
https://home-assistant-lovelace-gallery.netlify.com/#demo-hui-entities-card

If you’re not yet ready to convert to lovelace-ui, you then need custom ui from Andrey:

check the info on his github, it should be enough to get you started.
Here is what I had to add in my config.yaml:

  • Under homeassistant:
    "sensor.*":
      show_last_changed: true
      custom_ui_state_card: state-card-custom-ui
    "binary_sensor.*":
      show_last_changed: true
      custom_ui_state_card: state-card-custom-ui
  • in main part of configuration.yaml (like at the end):
customizer:
  custom_ui: local

Note: I’ve downloaded a local copy hence local above, Adjust as necessary if you select a different mode.