With a little help from ChatGPT about automation, make havoc with randomly turning on lights with Home Assistant

This isn't a post about "BaD AI ToOk OVER MY HoME, BeWaRE!".

I was wondering if there was a way to make lights go on and off randomly in Home Assistant. I imagined an alarm automation that, when a breach is detected, could just start a show with lights on all over the house with sound or sounds from horror movies or other SCP Containment Breach stuff.

Looking at the HA documentation I only found a binary sensor "random" but that was not what I needed. Also in the GUI of the automatoins there was no random function.

So I decided to ask ChatGPT about it, as I had had good experience with it in the past when creating automations. So I asked:

write an automation in the home assistant that turns on a random light 10 times. I have four light sources

First automation that ChatGPT came with was:

automation:
  - alias: losowe_swiatlo
    initial_state: true
    trigger:
      platform: time
      at: "20:00:00"
    action:
      repeat:
        count: 10
        sequence:
          - service: light.turn_on
            entity_id: light.{{ ["kuchnia", "salon", "sypialnia", "łazienka"]|random }}

First wow moment here - there is actually a random function in Automation, but when viewing it through the Automation GUI it says it cannot display it because it is not supported.

Then I asked to change light to switch, as I will be using Xiaomi WS-EUK02 wall switches. Automation changed to:

automation:
  - alias: losowy_przelacznik
    initial_state: true
    trigger:
      platform: time
      at: "20:00:00"
    action:
      repeat:
        count: 10
        sequence:
          - service: switch.turn_on
            data:
              entity_id: switch.{{ ["przelacznik1", "przelacznik2", "przelacznik3", "przelacznik4"]|random }}
          - delay: "00:00:00.5"

I pointed that there should be data_templete in use instead of data:

does not work, perhaps you need to use data_template: instead of data:

ChatGPT agrees with this:

Apologies for the mistake - actually, when using entity_id with a template value, use data_template instead of data. Below is the corrected version of the code:
automation:
  - alias: losowy_przelacznik
    initial_state: true
    trigger:
      platform: time
      at: "20:00:00"
    action:
      repeat:
        count: 10
        sequence:
          - service: switch.turn_on
            data_template:
              entity_id: switch.{{ ["przelacznik1", "przelacznik2", "przelacznik3", "przelacznik4"]|random }}
          - delay: "00:00:00.5"

It started to work - kind of, got and error of: Error: not a valid value for dictionary value @ data['entity_id'] after 4 or 5 iterations.

ChatGPT concluded:

I apologise for the inconvenience. It looks like the reason for the error is that we are using template interpolation to generate the entity_id value, and this value is not generated correctly during each iteration of the loop. To fix this, we can use the random() function instead of random in the selectattr filter to always return a unique value from the list.
Here is a new version of the code that should work correctly:
automation:
  - alias: losowy_przelacznik
    initial_state: true
    trigger:
      platform: time
      at: "20:00:00"
    action:
      repeat:
        count: 10
        sequence:
          - service: switch.turn_on
            data_template:
              entity_id: >
                {{ ["switch.przelacznik1", "switch.przelacznik2", "switch.przelacznik3", "switch.przelacznik4"]
                | selectattr("random()", "defined")
                | list
                | first }}
          - delay: "00:00:00.5"

Well, at this point nothing was working, and when I gave ChatGPT another syntax error, it finally created proper automation. I also gave it proper entity names so that it would not change with every implementation:

automation:
  - alias: losowe_swiatlo
    initial_state: true
    trigger:
      platform: time
      at: "03:00:00"
    action:
      repeat:
        count: 10
        sequence:
          - service: switch.toggle
            data_template:
              entity_id: >
                {{ ["switch.kuchnia_swiatlo_gora_left",
                    "switch.kuchnia_swiatlo_gora_right",
                    "switch.salon_swiatlo_gora_left",
                    "switch.salon_swiatlo_gora_right"]
                | random
                }}
          - delay: "00:00:00.5"

I thanked it, because I learnt something new today, both with HA and AI :)