How to get the "Area" of an entity in a template?

Hi all - complete newbie to all of this. Editing the YAML for an automation that would send a notification to my phone when the AC is turned on and if any window in my “All Windows” group is open. Here’s what I have so far:

alias: Central AC - On - Notification
description: Sends notification to phones if AC is turned on, and any window is open
triggers:
  - device_id: ed829695bc93b85bbb2c8fd6b0ff62af
    domain: climate
    entity_id: b893bd2a93dae62d6f261b4d0c3893b3
    type: hvac_mode_changed
    trigger: device
    to: cool
conditions:
  - condition: state
    entity_id: binary_sensor.all_windows
    state: "on"
actions:
  - variables:
      open_windows: >-
        {{ expand('binary_sensor.all_windows')|selectattr('state', 'eq', 'on')|map(attribute='name')|join('\n') }}
  - action: notify.mobile_app_iphone
    data:
      message: >-
        Close windows:
        {{ open_windows }}
mode: single

This gives me a notification with the full name of the entity, e.g. “Kitchen Window Sensor Opening”. But instead of that, I’d like to just get the Area that the entity is in. Something like… “Close windows in Kitchen, Living Room, Bedroom”.

Does anyone know of a good way to approach this? Thanks!

  1. google “home assistant templating” → u’ll get Templating - Home Assistant
  2. cmd+f and search for area on the page → u’ll get Templating - Home Assistant
  3. there you go → area_name(entity_id)
1 Like
  - action: notify.mobile_app_iphone
    data:
      message: >-
        Close windows in {{ expand('binary_sensor.all_windows')|map(attribute='entity_id')|select('is_state', 'on')|map('area_name')|unique|join(', ') }}
1 Like