Templating: How to return the area where a sensor is assigned?

Hi
We have an automation that monitors the last trigger of a binary_sensor.

The sensor’s are assigned to an area and I was wondering if there’s a way to write a template so the area of the sensor is returned rather than the sensor’s attributes?


image

This is so that we can use the automation to track last motion and write the last movement into an input_text helper. The friendly name is too long for other automations it’ll be used with.

This is an example of the automation so far which returns the friendly name instead of the area:

alias: Last Motion Sensor Active
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.bedroom_motion
      - binary_sensor.office_motion
      - binary_sensor.bathroom_motion
    to: "on"
condition: []
action:
  - variables:
      device: |
        {{state_attr(trigger.entity_id,'friendly_name')}}
      now: |
        {{ now().strftime('%Y-%m-%d %H:%M:%S') }}
  - service: input_text.set_value
    data:
      value: "{{device}}"
    target:
      entity_id: input_text.last_motion_sensor_triggered
  - service: input_datetime.set_datetime
    data:
      datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
    target:
      entity_id: input_datetime.last_motion_sensor_trigger_time
mode: single

{{ area_name(trigger.entity_id) }}
should give you the area name

3 Likes

Yup, that worked.
Thanks :slight_smile:

1 Like