How to retrieve the zone of a sensor in a script?

Hi,

Does anyone know if it’s possible to retrieve the zone where a sensor is in a script or a template ?

I added different zone in my house, like kitchen, room children 1, bathroom, … and to this differents zones the devices created by mqtt integration.

I want to do a resume of all temperature sensors I have in a script and send it in a notification. For example I want my script return :
Resume Temp

  • Room children 1 : 19.8°C - 45.3%
  • Kitchen : 19.8°C - 45.3%
  • Bathroom : 19.8°C - 45.3%

I found how iterate over all sensors and keep only those ending with _temperature for example. I can get the state value. But I’m unable to access the zone name to display it in my message.

I tried in a python script :

for e in hass.states.entity_ids():
    if e.startswith("sensor.temp") and e.endswith("_temperature") :
        logger.warn("Entity : %s", e)
        logger.warn("State : %s", hass.states.get(e))

But there is no attribute that refer the device where the sensors is or the area_id.

The goal is, when I add a new temperature sensor, I have nothing to do except add it in an area and my message let appear the new zone temperature. No template to modify or something else.

Thank’s for help!

1 Like

“Area”.

Zones are for geolocation.

Ah ok , thanks, but I don’t see anything about area in sensors attributes. Can I retrieve area from anywhere ? Get all sensors in an area ? Or get Area from a sensor ?

The answer to all three questions is no. Currently, area is inaccessible to templates. All you can currently do with an area is to use it as the target for a service call (for example, turn on all lights in a given area).

Ok thank you for the answer :+1:! May be in a futur version :sweat_smile:

You’re welcome!

If you want to organize entities into some form of “collection” that can be referenced in a template, I suggest you consider using a Group.

Yes, group was my first intention but the inconvenience is to add manually each time I add a new temperature sensors. Ok, it’s not every day :laughing:

I was using Jeeedom before and I try to do the same things I had on this platform. Home assistant is quite hard for a beginner :sweat_smile:

Thank’s again for help.

It’s possible to have dynamically-created groups. For example, the following automation is triggered on startup and whenever the group.reload service is called. It selects all sensors whose device_class is temperature and adds them to a group called group.temperature_sensors.

- alias: 'Create group'
  description: 'Dynamically create group on startup and reload'
  trigger:
  - platform: homeassistant
    event: start
  - platform: event
    event_type: 'call_service'
    event_data:
      domain: 'group'
      service: 'reload'
  action:
  - service: group.set
    data:
      object_id: temperature_sensors
      name: 'Temperature Sensors'
      entities: >
        {{ states.sensor
           | selectattr('attributes.device_class', 'eq', 'temperature')
           | map(attribute='entity_id') | list }}

Sorry for reviving this topic.

You can get the area of a sensor using these:

{{ area_id('binary_sensor.esp32_ble_tracker_test_ibeacon') }}
3 Likes

Use

area_name
1 Like