Show automation in device list when it is a dynamic target?

I have an automation that will randomly turn on a single light from a list of lights. It works well and randomly turns on a single light from the list.

alias: Turn On Random Light
description: ""
trigger:
  - platform: sun
    event: sunset
    offset: "00:10:00"
action:
  - variables:
      lights: |
        {{ [ "light.kasa_bulb_1", 
             "light.floor_light",
             "light.sink_dimmer"
           ] }}
      random_light: |
        {{ lights | random }}
  - service: light.turn_on
    data: {}
    target:
      entity_id: "{{random_light}}"
mode: parallel
max: 5

Unfortunately none of the lights defined in the list have this automation listed in the Automation section of the device. This makes it very easy to forget that this device is being used by this automation.

Is there a way to specify which devices are being targeted by an automation? Alternatively, is there a different way of randomly choosing an entity in an automation so that it would result in the automation being shown in the device’s automations list?

The answer to both questions is no.

Home Assistant can’t make the association for the entity because none of those lights appear in the service call’s entity_id option (more generally, they’re not explicitly part of the target).

Only by specifying the entity as described above.

As explained, you would have to ensure each entity is explicitly specified in the service call. Here’s one way to do it. Obviously, it’s simpler to use templating to select a random entity.

alias: Turn On Random Light
description: ""
trigger:
  - platform: sun
    event: sunset
    offset: "00:10:00"
action:
  - variables:
      nmbr: "{{ range(1, 4) | random }}"
  - choose:
      - conditions: "{{ nmbr == 1 }}"
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.kasa_bulb_1
      - conditions: "{{ nmbr == 2 }}"
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.floor_light
      - conditions: "{{ nmbr == 3 }}"
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.sink_dimmer
    default: []
mode: parallel
max: 5
1 Like

Thanks for the reply and the clarification on how it actually determines which entities to associate the automation with. The last part of your comment spurred a different idea that seems to accomplish my goal.

if:
  - condition: template
    value_template: "{{ False }}"
then:
  - service: homeassistant.update_entity
    data: {}
    target:
      entity_id:
        - light.kasa_bulb_1
        - light.floor_light
        - light.sink_dimmer

I’ve got to duplicate the entities in the list but specifically calling them out the second time in something that never gets hit accomplishes that goal.

Another way to do the same thing.

alias: Turn On Random Light
description: ""
trigger:
  - platform: sun
    event: sunset
    offset: "00:10:00"
action:
  - variables:
      lights:
        - light.kasa_bulb_1
        - light.floor_light
        - light.sink_dimmer
  - service: light.turn_on
    target:
      entity_id: "{{ lights | random }}"
  - stop: 'Stop here'
  - service: light.turn_on
    target:
      entity_id:
        - light.kasa_bulb_1
        - light.floor_light
        - light.sink_dimmer
mode: parallel
max: 5
1 Like

I totally forgot that the stop command even existed, great use for it! Thanks for the input.

1 Like