Template to find most recently changed device

I’d like to find which device has been most recently changed (i.e. turned on/off, brightness adjusted) using a template. It looks like there might be 2 values that could be of use: the state property last_changed and the device attribute timestamp. Is there one of these that would be more appropriate than the other?

I have attempted a template using timestamp, but it does not seem to give me what I’m looking for. The template I’ve been using is:

{{ expand('light.kitchen_desk','light.kitchen_under_cabinet') 
| sort(attribute= 'timestamp', reverse=true) 
| map(attribute ='entity_id') 
| first
}}

However, it always seems to return the first device.

I also attempted to use last_changed, however I could not figure how to access it within the template.

Can anyone provide some guidance on this?

cheers,
J.

replace timestamp with last_changed, for on/off
last_updated for mode-changes(lights)
Apparently , just try with “last_updated”, it might cover it all

The property last_changed only refers to changes of the entities’ states, for changes that include the state and attributes like brightness you need to use the last_updated property.

{{ expand('light.kitchen_desk','light.kitchen_under_cabinet') 
| sort(attribute= 'last_updated', reverse=true) 
| map(attribute ='entity_id') | first }}

I think I have a formatting problem… any chanced you can help me understand why the template light is not picking up either the state or brightness?

template:
  - light:
    - name: kitchen cabinet
    #       - 42.76.23: 'kitchen desk' (light.kitchen_desk)
    #       - 42.70.ff: 'kitchen under cabinet' (light.kitchen_under_cabinet)
      turn_on:
        - action: mqtt.publish
          data:
            topic: insteon/command/42.76.23
            payload: '{ "cmd" : "scene", "is_on" : 1, "level": 255 }'
            qos: 1
            retain: true
      turn_off:
        - action: mqtt.publish
          data:
            topic: insteon/command/42.76.23
            payload: '{ "cmd" : "scene", "is_on" : 0 }'
            qos: 1
            retain: true
      set_level:
        - action: mqtt.publish
          data:
            topic: insteon/command/42.76.23
            payload: '{ "cmd" : "scene", "is_on" : 1, "level": {{ brightness }} }'
            qos: 1
            retain: true
      default_entity_id: light.kitchen_cabinet
      level: >
        '{{ state_attr(expand("light.kitchen_desk","light.kitchen_under_cabinet") 
        | sort(attribute="last_updated", reverse=true) 
        | map(attribute="entity_id") | first, "brightness") }}'
      state: >
        '{{ states(expand("light.kitchen_desk","light.kitchen_under_cabinet") 
        | sort(attribute="last_updated", reverse=true) 
        | map(attribute="entity_id") | first) }}'

You’re over quoting in state and level. Use the multi-line quote indicators or quotes around the templates, not both.

Since you are already expanding to get the entities’ state objects, there’s no need to go through the entity_id and recalculate everything to get the state and attribute value… those values are already contained in the state object.

...
      level: >
        {{ expand("light.kitchen_desk","light.kitchen_under_cabinet") 
        | sort(attribute="last_updated", reverse=true) 
        | map(attribute="attributes.brightness") | first }}
      state: >
        {{ expand("light.kitchen_desk","light.kitchen_under_cabinet") 
        | sort(attribute="last_updated", reverse=true) 
        | map(attribute="state") | first }}
1 Like

Thank you!