Q : Best way to trigger automation via frontend button

I want to have short cut buttons to play radio stations on my frontend. Here’s what I did, is this the best way to do it? It works but seems unnecessarily complicated.

  1. Create buttons for each radio station - Kitchen : Play STATION
  2. Create an automation to play STATION
    1. the automation is triggered by “State”
    2. the State is the name of the button
  3. Create button cards under the “Kitchen” dashboard
    1. each button card triggers the buttons from 1

You likely don’t need an entity for every room/station combo. Instead or an automation use a script then have the button actions pass the desired variables:

square: true
columns: 4
type: grid
cards:
  - type: button
    tap_action:
      action: call-service
      service: script.room_radio
      data:
        room: kitchen
        station: disco
    name: Disco
  - type: button
    tap_action:
      action: call-service
      service: script.room_radio
      data:
        room: kitchen
        station: jazz
    name: Jazz

That way you can copy the entire card config into each room’s dashboard and just change the room value.

That worked great! For anyone coming here later, here’s the script code (optimizations welcome!)

alias: Play Sonos Radio Station
sequence:
  - service: media_player.play_media
    target:
      entity_id: media_player.office
    data:
      media_content_id: "{{ favorite }}"
      media_content_type: favorite_item_id
mode: single
icon: mdi:radio

I’ll probably change the entity_id to be a parameter. Can I do something like this?

entity_id: media_player."{{ location }}"

Yes. And, if your “human friendly” location doesn’t match the media player’s object id 1:1 you can use templates to map those values to each other.

1 Like