How to use the area of an entity in scripts/automations

Id like to have an automation report the state change of several binary_sensors motion, which all have been given an area.

Is it possible to use the context of the entity for this? if so, how would I go about in below set of automation and script, passing the entity_id:

script:
  alarm_triggered:
    alias: 'Alarm triggered'
    sequence:
      - condition: template
        value_template: >
          {{ is_state('input_boolean.notify_alarm', 'on') }}
      - service: notify.mobile_app_calltheboss
        data_template:
          title: >   ## <-----like to change this to "in {{entityid.area}}" 
            Alarm triggered: {{ states[entityid.split('.')[0]][entityid.split('.')[1]].name }} notification
          message: >
            {% set name = states[entityid.split('.')[0]][entityid.split('.')[1]].name %}
            {{as_timestamp(now()) | timestamp_custom('%X') }}:
              {{-name }} triggered the alarm, check whats up!

automation:
  - alias: 'Trigger alarm while armed'
    trigger:
      platform: state
      entity_id: 
        - binary_sensor.laundry_motion_sensor
        - binary_sensor.dining_table_motion_sensor
        - binary_sensor.auditorium_motion_sensor
        - binary_sensor.frontdoor_motion_sensor
        - binary_sensor.corridor_terrace_motion_sensor
        - binary_sensor.master_bedroom_motion_sensor
        - binary_sensor.dorm_motion_sensor
        - binary_sensor.attic_motion_sensor
      to: 'on'
    condition:
      condition: template
      value_template: >
        {{ states('alarm_control_panel.ha_main_alarm') in ['armed_away','armed_home'] }}
    action:
      - service: alarm_control_panel.alarm_trigger
        entity_id: alarm_control_panel.ha_main_alarm
      - service: script.alarm_triggered
        data_template:
          entityid: >
            {{trigger.entity_id}}

As far as I know this is not possible currently.

too bad really, it would be a great way of finally using the area :wink:

will see if a feature request is useful…

seems we cant even count the available areas in the templates?

{{areas | count}} as a mockup to illustrate what I am looking for.

should be available?

see: https://developers.home-assistant.io/docs/en/next/device_registry_index.html

adding this to the automation:

        data_template:
          entityid: >
            {{trigger.entity_id}}
          contextid: >
            {{trigger.to_state.context.id}}

does give me the an id in the script:

          message: >
            {% set name = states[entityid.split('.')[0]][entityid.split('.')[1]].name %}
            {{as_timestamp(now()) | timestamp_custom('%X') }}:
              {{-name }} triggered the alarm, check whats up!
              {{contextid}}  ##<--- renders the id number in the message

but it changes on every trigger… so that isnt very useful at all in finding a way that to connect to the area ;-(

It’s a fairly new attribute (area), I don’t think it would have been introduced without a plan for it’s development. By all means make the feature request but I think it’s happening anyway.
No facts, just my supposition based on it being there in the first place. :man_shrugging:

will check some more, and might just do that:

for completeness sake, also tried this:

        data_template:
          entityid: >
            {{trigger.entity_id}}
          contextid: >
            {{trigger.to_state.context.id}}
          userid: >
            {{trigger.to_state.context.user_id}}
          parentid: >
            {{trigger.to_state.context.parent_id}}

but both user_id and parent_id render ‘None’. So, im left with a ‘Id’ that changes every time, and might just as well be the id for the event itself, which is useless for my quest for the area for now…

taking them out again.

why I made the original template for the message so difficult is beyond me…

this does the same and better:

        data_template:
          title: >
            {% set room = entityid.split('.')[1].split('_motion_sensor')[0].replace('_',' ')|capitalize %}
            Alarm triggered in the {{room}}
          message: >
            {% set name = entityid.split('.')[1].replace('_',' ')|capitalize %}
            {{as_timestamp(now()) | timestamp_custom('%X') }}: {{name}} triggered the alarm, check whats up!

Habe you figured out how to get the area for your entities?

no unfortunately

This is fairly new, iirc, and you’ve probably used it by now, but just in case… wouldn’t this do what you want?

area_name(entity_id)

1 Like

yes, I am using that since t became possible:

      - service: persistent_notification.create
        data:
          title: >
            Alarm triggered in the {{area_name(trigger.entity_id)}}
          message: >
            {{now().strftime('%X')}}: {{area_name(trigger.entity_id)}} triggered the alarm, check whats up!
          notification_id: alarm-triggered

guess I should have solved this thread some time ago, my apologies :wink:

4 Likes