Lastchanged template sensor does not change

I have two identical sensors devices.
For both I have created a template sensor showing the shortest time since update of anyone of the sensors in the device.

Here are the template sensors:

  - platform: template
    sensors:
      xiaomiaqara_1_lastchanged:
        value_template: >
          {% set values = [
            states.sensor.xiaomiaqara_1_temperature.last_changed,
            states.sensor.xiaomiaqara_1_humidity.last_changed,
            states.sensor.xiaomiaqara_1_pressure.last_changed,
            states.sensor.xiaomiaqara_1_linkquality.last_changed,
            states.sensor.xiaomiaqara_1_voltage.last_changed,
            states.sensor.xiaomiaqara_1_battery.last_changed, ] %}
          {{ values | max }}
        device_class: timestamp

  - platform: template
    sensors:
      xiaomiaqara_2_lastchanged:
        value_template: >
          {% set values = [
            states.sensor.xiaomiaqara_2_temperature.last_changed,
            states.sensor.xiaomiaqara_2_humidity.last_changed,
            states.sensor.xiaomiaqara_2_pressure.last_changed,
            states.sensor.xiaomiaqara_2_linkquality.last_changed,
            states.sensor.xiaomiaqara_2_voltage.last_changed,
            states.sensor.xiaomiaqara_2_battery.last_changed, ] %}
          {{ values | max }}
        device_class: timestamp

And that’s the result: Left sensor works well, the right one doesn’t

remember reading somewhere in here
if you add the

entity_id: sensor.time

to the template

  - platform: template
    sensors:
      xiaomiaqara_1_lastchanged:
        entity_id: sensor.time    #<<<<< here
        value_template: >
          {% set values = [
            states.sensor.xiaomiaqara_1_temperature.last_changed,
            states.sensor.xiaomiaqara_1_humidity.last_changed,
            states.sensor.xiaomiaqara_1_pressure.last_changed,
            states.sensor.xiaomiaqara_1_linkquality.last_changed,
            states.sensor.xiaomiaqara_1_voltage.last_changed,
            states.sensor.xiaomiaqara_1_battery.last_changed, ] %}
          {{ values | max }}
        device_class: timestamp

should help it kick off sensor

can you share your lovelace bit thanks

The better option would be to list the entities actually used in the template, that way Home Assistant monitors them for change rather than just updating every minute as will happen with the time sensor.

  - platform: template
    sensors:
      xiaomiaqara_1_lastchanged:
        entity_id:
          - sensor.xiaomiaqara_1_temperature
          - sensor.xiaomiaqara_1_humidity
          - sensor.xiaomiaqara_1_pressure
          - sensor.xiaomiaqara_1_linkquality
          - sensor.xiaomiaqara_1_voltage
          - sensor.xiaomiaqara_1_battery
        value_template: >
          {% set values = [
            states.sensor.xiaomiaqara_1_temperature.last_changed,
            states.sensor.xiaomiaqara_1_humidity.last_changed,
            states.sensor.xiaomiaqara_1_pressure.last_changed,
            states.sensor.xiaomiaqara_1_linkquality.last_changed,
            states.sensor.xiaomiaqara_1_voltage.last_changed,
            states.sensor.xiaomiaqara_1_battery.last_changed, ] %}
          {{ values | max }}
        device_class: timestamp

Thanks for your replys, but unfortunately both did not help.

cards:
  - content: >-
      ## Terrasse

      Xiaomi Aqara 1
      [Beschreibung](https://xiaomi-mi.de/mi-smart-home/aqara-temperature-and-humidity-sensor/)
    style: |
      ha-card {
        background: rgba(0,250,0,0.5);
        border-radius: 15px;
        height: 80px
      }
    type: markdown
  - cards:
      - content: >-
          <img
          src="https://raw.githubusercontent.com/hajo62/HomeAutomation/master/images4git/xiaomi-aquara-sensor.jpg"
          width="100">   
        style: |
          ha-card {
            height: 140px
          }
        type: markdown
      - cards:
          - entity: sensor.xiaomiaqara_1_temperature
            scale: 26px
            type: 'custom:bignumber-card'
          - entity: sensor.xiaomiaqara_1_humidity
            scale: 26px
            type: 'custom:bignumber-card'
        type: vertical-stack
    type: horizontal-stack
  - entities:
      - entity: sensor.xiaomiaqara_1_lastchanged
        name: Last Changed
      - entity: sensor.xiaomiaqara_1_pressure
        name: Luftdruck
        secondary_info: last-changed
      - type: divider
      - entity: sensor.xiaomiaqara_1_linkquality
        name: Signalstärke
        secondary_info: last-changed
      - entity: sensor.xiaomiaqara_1_voltage
        name: Spannung
        secondary_info: last-changed
    type: entities
show_header_toggle: false
type: vertical-stack
1 Like

Solved. I hade a duplicate sensor definition - cut&paste mistake.