Is there a way of linking entities?

I have a bunch of windows (sensors) and covers that I would like to pair so I can use this information to make some automations. My automations are done in NodeRED, so I can get a list of all opened windows after a weather state change or the sun dawn. I would like, for example, that if one window is open and it starts raining close the corresponding cover. Sure I can do is match one by one bay hand, but what I would like is been able to link, associate or pair the information in the entity so when I recover the information from any windows I have a property that says which is its cover.
The question is: Is there a way of adding a custom property to an entity?
Thanks,

1 Like

This is best done using a structured naming schema.

If you have a window sensor called:

binary_sensor.south_window

And a shutter or blind called

cover.south_window_shutter

Similarly for north, west, and east windows, then you can do things like:

trigger:
  platform: state
  entity_id: 
    - binary_sensor.south_window
    - binary_sensor.north_window
    - binary_sensor.east_window
    - binary_sensor.west_window
  to: 'off'
action:
  service: cover.close_cover
  target:
    entity_id: "cover.{{ trigger.object_id }}_shutter"

Using this, if any window is closed then the corresponding shutter/blind will also close.

Entity ids are composed of a domain and an object_id:

domain.object_id

so if the entity_ids for your binary_sensors and covers (domains) have similar object_ids you can easily associate them.

1 Like

While I tend to use the method Tom suggested, you can also assign custom attributes to the individual entities:

homeassistant:
  customize:
    binary_sensor.south_window:
      cover: "cover.southern_blinds"
    binary_sensor.north_window:
      cover: "cover.north_shades"
    binary_sensor.east_window:
      cover: "cover.master_roller_blinds"
    binary_sensor.west_window:
      cover: "cover.kitchen_shutters"

    cover.southern_blinds:
      window: "binary_sensor.south_window"
    cover.north_shades:
      window: "binary_sensor.north_window"
    cover.master_roller_blinds:
      window: "binary_sensor.east_window"
    cover.kitchen_shutters:
      window: "binary_sensor.west_window"
Entity-based attribute automation example
trigger:
  platform: state
  entity_id: 
    - binary_sensor.south_window
    - binary_sensor.north_window
    - binary_sensor.east_window
    - binary_sensor.west_window
  to: 'off'
action:
  service: cover.close_cover
  target:
    entity_id: '{{ trigger.to_state.attributes.cover }}'

OR to each window/cover pair… something like window_id

Window ID config example
homeassistant:
  customize:
    binary_sensor.south_window:
      window_id: "S1"
    cover.southern_blinds:
      window_id: "S1"
    binary_sensor.north_window:
      window_id: "N1"
    cover.north_shades:
      window_id: "N1"
    binary_sensor.east_window:
      window_id: "MB1"
    cover.master_roller_blinds:
      window_id: "MB1"
    binary_sensor.west_window:
      window_id: "K2"
    cover.kitchen_shutters:
      window_id: "K2"
Window ID automation example
trigger:
  platform: state
  entity_id: 
    - binary_sensor.south_window
    - binary_sensor.north_window
    - binary_sensor.east_window
    - binary_sensor.west_window
  to: 'off'
action:
  service: cover.close_cover
  target:
    entity_id: >
      {{ states.cover
      | selectattr('attributes.window_id', 'defined')
      | selectattr('attributes.window_id', 'eq', trigger.to_state.attributes.window_id)
      | map(attribute = 'entity_id') | list }}
3 Likes

Thank you for your answers. Both works perfectly. For me the solution that best fits is the “custom attributes” as i tend to keep the original ids for every device, I got some mess up at the beginning replacing ids so I decided not to do so.
I didn’t know that you could create your own custom attributes, I thought that it what for customising the existing ones.