Automation - changing playlists depending on sensor state

Hey guys,

my KNX button cycles states 0,1,2,3,4 by each press. Since I am just a HAOS beginner I have currently 5 automations setup for each state (example for state 1 is below). I am sure that there is more elegant way to combine all 5 into one but I was not able to find similar example. I would appreciate your help as I have 7 similar buttons :slight_smile:

THANKS!

alias: "1"
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.audio_bathroom_1np
    to: "1"
condition: []
action:
  - service: media_player.play_media
    data:
      media_content_id: My Live Radio/Fun Radio
      media_content_type: music
    target:
      entity_id: media_player.guest_bathroom
mode: single

I see that if the state is 1 then media_content_id is My Live Radio/Fun Radio.

What are the other 4 choices?

let’s say that:
state 2 = My Live Radio/Radio 2
state 3 = My Live Radio/Radio 3
state 4 = My Live Radio/Radio 4
state 0 = stop

Id use choose or if/then to select the action.
And a knx_event rather than a sensor as trigger.

alias: example
description: ""
trigger:
  - platform: state
    entity_id: sensor.audio_bathroom_1np
    to:
      - '0'
      - '1'
      - '2'
      - '3'
      - '4'
condition: []
action:
  - if: "{{ trigger.to_state.state == '0' }}"
    then:
      - service: media_player.media_stop
        target:
          entity_id: media_player.guest_bathroom
    else:
      - service: media_player.play_media
        data:
          media_content_id: >
            {% set channels = {
              '1': 'My Live Radio/Fun Radio', 
              '2': 'My Live Radio/Radio 2',
              '3': 'My Live Radio/Radio 3',
              '4': 'My Live Radio/Radio 4' } %}
            {{ channels.get(trigger.to_state.state, '1') }}
          media_content_type: music
        target:
          entity_id: media_player.guest_bathroom
mode: single

Thx mate. It’s working. I forgot to start ROCK server but it is fine now. Coffee is coming :wink:

1 Like