Automation - Action Case Statement?

I’m trying to tie an input_select to an automation rule to allow starting or changing a Sonos playlist. Based on the selection from the input_select I would like to have a different media_content_id sent but I’m not sure of the format required (or if possible).

Curent:

  • alias: Playlist Selection
    trigger:
    platform: state
    entity_id: ‘input_select.sonosplaylist’
    to: “Today’s Biggest Hits”
    action:
    • service: media_player.play_media
      data:
      entity_id: media_player.main
      media_content_id: “x-sonosapi-radio:o3_MYs-V-nE5FYdz_wa0B7FPneeLn6D3Fz5R1EKg1MMk2T0RAVZo5zucq-QwW_BNxHMm7t-2AqKZPGvZe2iuDw?sid=151&flags=8300&sn=1”
      media_content_type: “PLAYLIST”

Again, I want to perform the equivalent of a case or switch on the “to” and change the “media_content_id”.

hey @kylerw ,

I have the same need. By now I’ve solved this by creating 1 script per playlist and adding it to each group. Very confusing and complicated to maintain.

Maybe we can open a feature request to have something like a switch.

anyone else thoughts?

I have been reviewing the Jinja documentation and I’ve not been able to find a switch or case.

based on another thread I see this recomendation and I’m going to do some testing.

There is no switch/case in Jinja, or Python for that matter, so data templates are your only option.

alias: 'Playlist Selection'
trigger:
  platform: state
  entity_id: input_select.sonosplaylist
action:
  service: media_player.play_media
  data_template:
    entity_id: media_player.main
    media_content_type: "PLAYLIST"
    media_content_id: >
      {%- if trigger.to_state.state == "Today's Biggest Hits" -%}
        x-sonosapi-radio:o3_MYs-V-nE5FYdz_wa0B7FPneeLn6D3Fz5R1EKg1MMk2T0RAVZo5zucq-QwW_BNxHMm7t-2AqKZPGvZe2iuDw?sid=151&flags=8300&sn=1
      {%- elif trigger.to_state.state == "My Favorite Songs" -%}
        x-sonosapi-radio:o3_MYs-V-nE5FYdz_wa0B7FPneeLn6D3Fz5R1EKg1MMk2T0RAVZo5zucq-QwW_BNxHMm7t-2AqKZPGvZe2iuDw?sid=151&flags=8300&sn=1
      {%- elif trigger.to_state.state == "Mary's Playlist" -%}
        x-sonosapi-radio:o3_MYs-V-nE5FYdz_wa0B7FPneeLn6D3Fz5R1EKg1MMk2T0RAVZo5zucq-QwW_BNxHMm7t-2AqKZPGvZe2iuDw?sid=151&flags=8300&sn=1
      {% endif %}
1 Like

wow, I must confess that I’m starting on Python but not having switch … that’s very surprising for me … :slight_smile:

thanks for your help!

1 Like

Yeah, it is something that shocks a lot of people moving from other coding languages, as it almost seems like it should be a given that all languages use switch/case, and this has been debated back and forth in forums from one side of the internet to the other, but apparently the creators of Python believe in some purity aspects of their vision of the perfect coding language and the switch/case concept is not in their playbook.

1 Like

is it possible to then customize a notification to use the state? Something like:

  - service: notify.notifyall
    data:
      message: "Playlist changed to: trigger.to_state.state"

Is there anyway to configure a dictionary to provide a mapping from some input state to a value that you would like to hand to a service?

Yes, you can use the state in a notification:

- service: notify.notifyall
  data:
    message: >
      Playlist changed to: {{ trigger.to_state.state }}