Sensor template - day and time attributes showing null

The sensor shows up with the correct motion sensor that last detected motion, but attributes day and time are showing null

Can anyone spot anything obvious?

  - sensor:
      - name: "last_motion"
        state: >
          {% set sensors = [states.binary_sensor.pir_lux_backyard_motion,
                            states.binary_sensor.pir_lux_barneys_door_motion, 
                            states.binary_sensor.pir_lux_hallway_motion,
                            states.binary_sensor.pir_lux_kitchen_motion,
                            states.binary_sensor.motion_entry,
                            states.binary_sensor.motion_laundry,
                            states.binary_sensor.motion_living_room,
                            states.binary_sensor.pir_bedroom_occupancy,
                            states.binary_sensor.camera_backyard_motion_trigger,
                            states.binary_sensor.camera_barney_motion_trigger] %}
          {% set t = sensors | map(attribute='last_changed') | max %}
          {% set s = (sensors | selectattr('last_changed', 'eq', t) | list)[0] %}
          {{ s.name }}
        attributes:
          day: >
            {{ s.last_changed.timestamp() | timestamp_custom('%d %b %y') }}
          time: >
            {{ s.last_changed.timestamp() | timestamp_custom('%I:%M %p') }}

My old sensor looked like this using a value template. It worked but I wanted to split up the data and thought I may as well use the new sensor template.

  - platform: template
    sensors:
      last_motion:
        friendly_name: "Last Motion Detected"
        value_template: >
          {% set sensors = [states.binary_sensor.pir_lux_backyard_motion,
                            states.binary_sensor.pir_lux_barneys_door_motion, 
                            states.binary_sensor.pir_lux_hallway_motion,
                            states.binary_sensor.pir_lux_kitchen_motion,
                            states.binary_sensor.motion_entry,
                            states.binary_sensor.motion_laundry,
                            states.binary_sensor.motion_living_room,
                            states.binary_sensor.camera_backyard_motion_trigger,
                            states.binary_sensor.camera_barney_motion_trigger] %}
          {% set t = sensors | map(attribute='last_changed') | max %}
          {% set s = (sensors | selectattr('last_changed', 'eq', t) | list)[0] %}
          {{s.name + ' on '}} {{s.last_changed.timestamp() | timestamp_custom('%d %b %y at %I:%M %p') }}

Any help is greatly appreciated.

One thing I can tell is the variable t and s defined for state, are not available for the attributes.
You will have to copy those t and s variable definitions from state to attributes.

1 Like

Because the Jinja2 variable s that you defined within the state option is undefined outside of the state option.

A Jinja2 variable’s “scope” is limited to the option where it’s defined.

You might consider voting for the following Feature Request. A script variable can be referenced within any option (but it’s not currently supported by Template Sensor).

Got it working with the following

Had to repeat everything :expressionless:

    # Last motion detected for security page
  - sensor:
      - name: "last_motion"
        state: >
          {% set sensors = [states.binary_sensor.pir_lux_backyard_motion,
                            states.binary_sensor.pir_lux_barneys_door_motion, 
                            states.binary_sensor.pir_lux_hallway_motion,
                            states.binary_sensor.pir_lux_kitchen_motion,
                            states.binary_sensor.motion_entry,
                            states.binary_sensor.motion_laundry,
                            states.binary_sensor.motion_living_room,
                            states.binary_sensor.pir_bedroom_occupancy,
                            states.binary_sensor.camera_backyard_motion_trigger,
                            states.binary_sensor.camera_barney_motion_trigger] %}
          {% set t = sensors | map(attribute='last_changed') | max %}
          {% set s = (sensors | selectattr('last_changed', 'eq', t) | list)[0] %}
          {{ s.name }}
        attributes:
          day: >
            {% set day = [states.binary_sensor.pir_lux_backyard_motion,
                    states.binary_sensor.pir_lux_barneys_door_motion, 
                    states.binary_sensor.pir_lux_hallway_motion,
                    states.binary_sensor.pir_lux_kitchen_motion,
                    states.binary_sensor.motion_entry,
                    states.binary_sensor.motion_laundry,
                    states.binary_sensor.motion_living_room,
                    states.binary_sensor.pir_bedroom_occupancy,
                    states.binary_sensor.camera_backyard_motion_trigger,
                    states.binary_sensor.camera_barney_motion_trigger] %}
            {% set tt = day | map(attribute='last_changed') | max %}
            {% set ss = (day | selectattr('last_changed', 'eq', tt) | list)[0] %}
            {{ ss.last_changed.timestamp() | timestamp_custom('%d %b %y') }}
          time: >
            {% set time = [states.binary_sensor.pir_lux_backyard_motion,
                    states.binary_sensor.pir_lux_barneys_door_motion, 
                    states.binary_sensor.pir_lux_hallway_motion,
                    states.binary_sensor.pir_lux_kitchen_motion,
                    states.binary_sensor.motion_entry,
                    states.binary_sensor.motion_laundry,
                    states.binary_sensor.motion_living_room,
                    states.binary_sensor.pir_bedroom_occupancy,
                    states.binary_sensor.camera_backyard_motion_trigger,
                    states.binary_sensor.camera_barney_motion_trigger] %}
            {% set ttt = time | map(attribute='last_changed') | max %}
            {% set sss = (time | selectattr('last_changed', 'eq', ttt) | list)[0] %}
            {{ sss.last_changed.timestamp() | timestamp_custom('%I:%M %p') }}

How inefficient

If anyone has a better way, please let me know

Template Self Referencing

That’s why I created the Feature Request, to avoid duplicating a template (especially when the template is lengthy).

FYI

Discussion: