Random varible in the media_content_id or file name inside an automation

This is my automation that plays a mp3 file via a google nest speaker. I have 3 files (enzobark1, enzobark2, enzobark3) that I want to play in sort of random order every time the automation kicks in.

I was thinking of two approaches to substitute the number in the file name.

  1. To use random function with min=1 and max=3 and generate filename based on that.
  2. Do an if condition check say if current time seconds <20 choose file1, if seconds <40 choose file2 and if seconds <60 choose file3.

Problem is that I dont know how to use make such a substitution in the automation. Anyone who can help out?

alias: Frigate Notification
description: ""
trigger:
  - platform: mqtt
    topic: frigate/events
    payload: new
    value_template: "{{ value_json.type }}"
action:
  - service: media_player.play_media
    data:
      media_content_id: /local/enzobark1.mp3
      media_content_type: audio/mp3
    target:
      device_id: f9ada08ba91258ac7edee0383abec80f

Like that?

alias: Frigate Notification
description: ""
trigger:
  - platform: mqtt
    topic: frigate/events
    payload: new
    value_template: "{{ value_json.type }}"
action:
  - service: media_player.play_media
    data:
      media_content_id: '/local/enzobark{{[1,2,3]|random}}.mp3'
      media_content_type: audio/mp3
    target:
      device_id: f9ada08ba91258ac7edee0383abec80f
1 Like

Sweet! Taras suggestion worked like a charm and is quite neat. I havent tested James C idea, it should probably work too. Thanks guys!

1 Like

That’s the difference between a template hacker and a template master…
I can usually make it work, but not often is it elegant like that!

If it’s working, consider adding solution to 123’s answer.