Running list of all state changes

I would like a lovelace that will show a list of state changes with newest ones on top.

Sorry if my question is unclear or doesn’t make sense or if I’m using the wrong terminology.

Here’s what I mean: I have all kinds of sensors (temperature, electric use, water use, etc.).

I would like to have a lovelace that shows a list, time ordered with newest at the top, of all new data (changed temperatures for any particular temp sensor, for example).

Is this possible?

Thanks!

I’m using a Markdown Card for my most important binary sensors that have recently changed. The code can easily been adjusted:


type: markdown
content: |-
  {% for x in (states.sensor
  |rejectattr('entity_id', 'search', '_updated|test')
  |selectattr('attributes.device_class', 'defined')
  |sort(attribute='last_changed', reverse=true))[0:10] %}
  <b>
  {%- if x %}
    {%- set name = x.name %}
    {%- set state = x.state %}
    {%- set time = x.last_changed.timestamp()|timestamp_custom('%H:%M:%S', true, now() ) %}
    {{- '• ' ~ name ~ ': ' ~ state ~ ' (' ~ time ~ ')' -}}
  {% else %} ERROR
  {% endif %}
  </b>
  {% endfor %}

Note: If you want more than 10 state changes to be displayed, alter [0:10] to your needs.

1 Like

Do you mean like the Logbook? Especially the Logbook if you don’t filter by any entities?

I’d like to be able to put this on a dashboard too. I suspect the novelty would soon wear of though - probably after causing household members concern about the amount of data they are generating.

That’s great!

Any suggestion as to how to choose which entities to list (instead of all of them)?

Look at this piece:


  |rejectattr('entity_id', 'search', '_updated|test')

makes the output get rid of sensors that contain _updated or test in their entity_id.

Yee haw!

I’m a programmer :grinning:

|selectattr(‘entity_id’, ‘search’, ‘temperature’)

You can also filter by class:


  {% for x in (states.sensor
  |selectattr('attributes.device_class', 'defined')
  |selectattr('attributes.device_class', 'eq', 'temperature')
  |sort(attribute='last_changed', reverse=true))[0:10] %}

Totally cool.

I see in DEVELOPER TOOLS | STATES I can filter by attribute and, for example, use “device_class: Temperature”

Is there a way to list the devices by class? The goal is to be able to see what classes there are and what devices are in each class. I suspect I have a much smaller than average HA setup, but the number of devices is still far too large to manage by human memory.

Certainly there are more elegant ways, but it’s a lazy sunday, so I simply packed my example above in a second loop:


type: markdown
content: |-
  {% for x in states.sensor 
  |selectattr('attributes.device_class', 'defined')
  |groupby('attributes.device_class') %}

  {% if loop.first %} ### Device Classes: {{loop.length}}{% endif %} 
  **{{ loop.index }}. Device Class "{{ x[0] }}"**
    {% for value in (x[1] |sort(attribute='last_changed', reverse=true))[0:10] %}
    {%- if value is defined %}
      {%- set name = value.name %}
      {%- set state = value.state %}
      {%- set time = value.last_changed.timestamp()|timestamp_custom('%H:%M:%S', true, now() ) %}
      {{- '• ' ~ name ~ ': ' ~ state ~ ' (' ~ time ~ ')' -}}
    {% else %} ERROR
    {% endif %}
    {% endfor %}
  {% endfor %}



I think your 18 lines of code can be the syllabus for a comp sci major’s entire 4 years of undergrad work!

I need to learn this stuff!

What language is this?

Thank you again so much!

It’s Jinja. Documentation can be found here: Jinja — Jinja Documentation (3.1.x)

In Developer Tools ——> Template there are a few pre-installed examples.

1 Like

Greetings, I’m trying to create a log of the different pins that were entered into our alarm system. Based on your bit of code I came up with:

type: markdown
content: |-
  {% for x in (states.sensor
  |rejectattr('entity_id', 'search', '_updated|test')
  |selectattr('entity_id', 'search', 'sensor.last_alarm_arm_disarm')
  |sort(attribute='last_changed', reverse=true))[00:10] %}
  <b>
  {%- if x %}
    {%- set name = x.name %}
    {%- set state = x.state %}
    {%- set time = x.last_changed.timestamp()|timestamp_custom('%H:%M:%S', true, now() ) %}
    {{- '• ' ~ state ~ ' (' ~ time ~ ')' -}}
  {% else %} ERROR
  {% endif %}
  </b>
  {% endfor %}

I had to create a template sensor to watch the attribute on the alarm control panel.

{{state_attr('alarm_control_panel.alarm_control_panel', 'last_event_info')}}

I’ve got the card displaying the last state, however it’s not repeating the state. Is there a way for it to show a longer history for one entity?

Thanks!

You cannot loop a single attribute or property.
But you can create a history in your template sensor’s attributes. Here are a few examples:

Trigger sensor to determine if someone is either entering or exiting a door

Template sensor with last good value stored in attribute

Is there a way to retain the previous attribute with trigger sensor? - #3 by coolhand

Persistent version of "last-changed" for the UI? - #67 by TheFes

List of 10 last breached motion sensors/doors/windows? - #3 by 123

1 Like

Thanks for the reply! Somehow I missed it until coming across this thread during a search.

I was able to move this project forward a little with a template sensor of the alarm system state attribute, and a card I found in HACS that shows the history of a sensor.

Now I’m stuck at trying to filter out some of the unwanted attribute history.

Here’s my recent inquiry about it:

Any pointers would be hugely appreciated. Cheers.