Template assistance as_timestamp changes?

I did a full restart on my HA install a few months ago, and as I’ve had time I’ve added back automations, sensors, etc that I missed.

Anyway, finally got my motion sensors set back up and I used to use this

      last_motion:
        friendly_name: 'Last Motion'
        icon_template: 'mdi:walk'
        value_template: >
          {%- set sensors = [states.binary_sensor.bedroom_motion_sensor, states.binary_sensor.first_floor_motion_sensor, states.binary_sensor.master_bedroom_closet_motion_sensor, states.binary_sensor.great_room_occupancy, states.binary_sensor.outdoor_motion_sensor] %}
          {% for sensor in sensors %}
            {% if as_timestamp(sensor.last_changed) == as_timestamp(sensors | map(attribute='last_changed') | max) %}
              {%- set short_name = "(".join(sensor.name.split("(")[1:]) %}
              {{ ")".join(short_name.split(")")[:-1]) }}
            {% endif %}
          {% endfor %}

and now it does not work. I seem to remember that there may have been changes with timestamps or templates in the past few months, but cannot seem to find anything specific.

This template above had been working for me for quite a while. So I’m pretty confident that the concept is good.

Where is sensor.name set? ← nevermind this, I get it now.

I don’t really understand what all the ( ) is for.
Do you mind explaining what it does?

when it was working, ultimatly the sensor.last_motion, would just show the friendly name if which ever motion sensor last triggered

This will get you the friendly_name of whichever sensor has the most recent last_changed.

{%- set sensors = [states.binary_sensor.bedroom_motion_sensor, states.binary_sensor.first_floor_motion_sensor, states.binary_sensor.master_bedroom_closet_motion_sensor, states.binary_sensor.great_room_occupancy, states.binary_sensor.outdoor_motion_sensor] %}
{{ (sensors | sort(reverse=true, attribute="last_changed"))[0].name }}

Is the remainder of your template designed to extract a string that exists within ( ) in the entity’s friendly_name? If that’s what it does then I can offer another way to do it.


{{ (sensors | sort(reverse=true, attribute="last_changed"))[0].name | regex_findall_index("\((.*)\)") }}
1 Like

I think we’d want to move to expand to filter out bad entity_ids and then map the name and grab the first one

{%- set sensors = 'binary_sensor.bedroom_motion_sensor', 'binary_sensor.first_floor_motion_sensor', 'binary_sensor.master_bedroom_closet_motion_sensor', 'binary_sensor.great_room_occupancy', 'binary_sensor.outdoor_motion_sensor' %}
{{ expand(sensors) | sort(reverse=true, attribute="last_changed") | map(attribute='name') | list | first | default(none) }}
1 Like

I’ll be 100% honest. I think I got help from someone here years ago, and it just worked. I understood enough to know it was looping through the sensors looking for the last_changed attribute and comparing time stamps, but not much more than that. :slight_smile:

I feel like I pretty much just used what had worked, (changed a few sensor names) and now it does not work, but since I never 100% understood what it was doing, I could not understand why it did not work.