How can I access the parent device of an entity in an automation?

Say I have an automation that is triggered by any of a number of entities (ideally a group, but that has its own issues) – how can I access the parent device along with other entities belonging to that device?

Example: I have ten temperature / humidity sensors and when the humidity on any of them gets to high I want an alert that includes the temperature and I don’t want to have ten different automations.

Each temperature / humidity sensor should found in HA as a single device, with two entities: temperature sensor and humidity sensor. It’s possible it also shows up as a single entity with two attributes: temperature and humidity. So, the answer depends on which one you have…

If you have the latter setup where temperature and humidity are attributes, then it’s rather straightforward because the entity_id of the alert will be the same for both and you just include the attribute in your notification.

If you have the former setup, then hopefully they’re named predictably, if they are not then I’d suggest renaming them. My ecobee termostat is a single device with multiple sensors. The thermostat is named “upstairs” and it creates sensors called “sensor.upstairs_humidity” and “sensor.upstairs_temperature”. So, if I get alert on “sensor.upstairs_humidity”, I already know the temperature will be “sensor.upstairs_temperature” so I can use it in a notification.

To my knowledge there is no way to “walk” from “sensor.upstairs_humidity”, find the parent device “upstairs”, and then figure out what the temperature sensor is, without it being named predictably.

You could do something like this as long as the temp/humidity sensors have similar entity IDs (as in sensor.bedroom_temperature and sensor.bedroom_humidity.

- alias: High humidity notification
  initial_state: true
  mode: parallel
  - platform: numeric_state
    - sensor.bedroom_humidity
    - sensor.upstairs_humidity
    - sensor.downstairs_humidity
    above: 60
  - service: notify.whatever
      message: "{{ }} is {{ states(trigger.to_state.entity_id.replace('humidity','temperature')) }}!"

That will show a notification with the appropriate temperature. Of course, change the entity IDs, service name, service data, etc. as appropriate.

@ pickerin
My sensors (Aqara via ZHA) show up as devices with several entities, no useful attributes.

@ Tediore
That looks like it should work for me as the names are predictable. It does seem rather “hacky” though, I wish there was a cleaner way that exploited the fact that the entities are from the same device.

Generally I wish HA’s groups, areas and devices were more flexible and better thought out; right now they don’t really lacking in functionality.


1 Like