Template to Store Last Tripped Sensor?

Hello Community,

I’m looking to create a Sensor Template to store the last tripped sensor from a group of binary sensors. Does anyone know if this is possible and might give me some hints?

TIA

would be handy for my alarm hmmmm

Someone else had the same request in another thread. The issue they are facing is that, on startup, one of the binary_sensors doesn’t get initialized promptly so it takes some time before it ‘exists’. That presents a problem for the Template Sensor because, on startup, it promptly refers to a binary_sensor that doesn’t exist yet (which produces an error).

The alternative is to use an automation that is triggered by any one of several binary_sensors. Whichever sensor triggers the automation gets its friendly_name stored in an input_text.

If this solution (or the first one) interest you, head over to this thread:

Hi, I using this approach and with the variable component. This will do what you looking for if I understand what you are after (early monday morning…)

Here’s how I setup a template sensor:

- platform: template
  sensors:
    template_last_motion:
      friendly_name: 'Motion'
      icon_template: 'mdi:walk'
      entity_id:
        - binary_sensor.garage_motion
        - binary_sensor.motion_sensor_158d0003d57f46
        - binary_sensor.motion_sensor_158d0002f786a1
        - binary_sensor.motion_sensor_158d00047906a4
      value_template: >
        {% set allmotion = states | selectattr('entity_id', 'in', state_attr('group.motion', 'entity_id')) | list %}
        {% set open= allmotion | map(attribute='last_changed') | list |max %}
        {{allmotion |selectattr('last_changed','eq', open)|map(attribute='name')|list|join}} ({{allmotion |selectattr('last_changed','eq', open)|map(attribute='state')|list|join}}) on {{ as_timestamp(open) | timestamp_custom('%d/%m/%Y at %-Hh%M') }}

Entity_Id (4x) trigger a change of the sensor template. These 4 Sensors are in a group called group.motion.
You can test it out in the “Templates” Section of your HA:

Thanks to both of you. I’m actually trying to catch the last tripped binary_sensor and not all of them are motion sensors. Can your code be adapted by including all the sensors and changing the variable name allmotion to something like alltripped?

The example I provided isn’t tied to a specific type of binary_sensor. For that matter, neither is hiscorebob’s example. ‘allmotion’ is just a variable name within the template. The template is acting on all the entities it finds within a group called group.motion. That group can contain, and be named, anything you want. You’re free to create a group called group.my_group and have it contain only door and window binary_sensors. Then use that in the template. The choice is up to you.

Thank you so much! I’ll give it a try tonight.

Update: works like a charm! I went with the template from @hiscorebob as it was better suited to my needs, but thanks to @123 as well!

If you’re interested, here’s a streamlined version of hiscorebob’s Template Sensor.

For the binary_sensors you wish to monitor, enter them directly within the template’s sensors list. There’s no need to list them in entity_id: or to create a group containing them.

  - platform: template
    sensors:
      last_activated:
        friendly_name: 'Last Activated'
        value_template: >
          {% set sensors = [states.binary_sensor.front_door,
                            states.binary_sensor.rear_door, 
                            states.binary_sensor.garage_door] %}
          {% set time = sensors | map(attribute='last_changed') | max %}
          {% set sensor = (sensors | selectattr('last_changed','eq', time) | list)[0] %}
          {{sensor.name}} {{sensor.state}} {{sensor.last_changed.timestamp() | timestamp_custom('%b-%d %-H:%M')}}

The output looks like this:
Garage door off Jul-16 21:40
However, you can easily modify it to produce whatever you prefer.

Be advised that, upon startup, Home Assistant sets an entity’s last_changed value to the current date and time (i.e. the time when Home Assistant restarted). That means moments after startup all entities have the same last_changed value. In other words, this attribute’s value doesn’t survive a restart; it is reset after every restart.

3 Likes

This code works great. Except I just want to know the last ‘on’ state. How would I modify this code to fit my purpose?

Anyone has found a way to change the template sensor only on “on” state of the reference sensors?