Input_select problems

I have an issue in my config I can’t wrap my head around.

In configuration.yaml:

input_select:
  wakeup_sound:
    name: Wakeup sound
    options:
     - birds
     - beach
    initial: birds
    icon: mdi:music-note

in automation.yaml:

- alias: Wakeup alarm
  trigger:
    platform: template
    value_template: "{{ states('sensor.time') == (states.input_datetime.wakeup_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  condition:
    condition: state
    entity_id: input_boolean.wakeup_alarm
    state: 'on'
  action:
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.living_room
        volume_level: '0.7'
    - service: media_player.play_media
      data:
        entity_id: media_player.living_room
        media_content_id: >
          {% if is_state("input_select.wakeup_sound", "birds") %}
          "http://ipaddresshere:8123/local/birds.mp3"
          {%-elif is_state("input_select.wakeup_sound", "beach") %}
          "http://ipaddresshere:8123/local/beach.mp3"
          {% endif %}
        media_content_type: 'music'

when I trigger this, I end up with:

ERROR (SyncWorker_2) [homeassistant.components.media_player.sonos] Error on play_media with UPnP Error 714 received: Illegal MIME-Type from ipaddresshere

…however if I switch media_content_id to just the link to the mp3 (without the logic), it works just fine.

I’ve been trying for hours to get this working now … can someone help?

Well, I haven’t played with media_player items yet, but right off the bat, I think, in the action part of your automation, you have data where you should have data_template, and you have data_template where you can use just data.

And, FWIW, you don’t need the elif, you can (and probably should) just use else.

As @pnbruckner said, you need the data_template instead of data. Also, you need to add a default sound for when the input select isn’t selecting something in the interface, i.e startup. So you need to add an else and choose a default media to play. Or do what @pnbruckner said and swap the elif is_state(…) for else.

One thing about jinja is that you should never leave pathways that don’t provide data because yaml won’t complain and you could cause python errors.

Ok now I have:

  action:
    - service: media_player.volume_set
      data:
        entity_id: media_player.living_room
        volume_level: '0.7'
    - service: media_player.play_media
      data_template:
        entity_id: media_player.living_room
        media_content_id: >
          {% if is_state("input_select.wakeup_sound", "birds") %}
            "http://ip:8123/local/birds.mp3"
          {%-elif is_state("input_select.wakeup_sound", "beach") %}
            "http://ip:8123/local/beach.mp3"
          {% else %}
            "http://ip:8123/local/birds.mp3"
          {% endif %}
        media_content_type: 'music'

…but still same result.

Also tried this:

  action:
    - service: media_player.volume_set
      data:
        entity_id: media_player.living_room
        volume_level: '0.7'
    - service: media_player.play_media
      data_template:
        entity_id: media_player.living_room
        media_content_id: >
          {% if is_state("input_select.wakeup_sound", "birds") %}
            "http://ip:8123/local/birds.mp3"
          ##{%-elif is_state("input_select.wakeup_sound", "beach") %}
            ##"http://ip:8123/local/beach.mp3"
          {% else %}
            "http://ip:8123/local/beach.mp3"
          {% endif %}
        media_content_type: 'music'

…but I get the same still.

Also, the “initial” entry under “input_select” should make sure “birds” is selected if no user actually changes its value manually I believe…
When I check in the template editor, it shows correct values (i.e “birds” or “beach” depending on what is chosen.

So for the record, this works:

  action:
    - service: media_player.volume_set
      data:
        entity_id: media_player.living_room
        volume_level: '0.7'
    - service: media_player.play_media
      data_template:
        entity_id: media_player.living_room
        media_content_id: "http://ip:8123/local/beach.mp3"
        media_content_type: 'music'

…but obviusly, that means no logic…

bingo.

    - service: media_player.play_media
      data_template:
        entity_id: media_player.living_room
        media_content_type: 'music'
        media_content_id: >
          {% if is_state("input_select.wakeup_sound", "birds") %}
            http://ip:8123/local/birds.mp3
          {%-elif is_state("input_select.wakeup_sound", "beach") %}
            http://ip:8123/local/beach.mp3
          {% else %}
            http://ip:8123/local/beach.mp3
          {% endif %}

…works.

(I removed the double quotes in the URL). Oh well.

1 Like

Yeah, I wondered about those. Still too new to jinja or I probably would have said something. Glad you got it working! :slight_smile:

I’m pretty sure using is_state() will let you use a simple else. I.e., is_state() will never fail, even if input_select.wakeup_sound didn’t exist. It would simply return False. I think that’s the main reason for that function existing (although it does also make it a little easier than having to enter states.input_select.wakeup_sound.state == ‘birds’.)

FWIW, after having done some careful inspection of the HA log during startup, I’m pretty sure you don’t have to worry about the automation running before an input_select is ready to do. Either it will have the value it had before HA shutdown (assuming you’re using the logger), or it will have the initial state (if you specify one.) Even if it were to come up with no selection, again, I think using is_state() will “protect” you from that scenario. But, again, I’m still pretty new to HA and I still get surprised on a daily basis. :slight_smile:

Glad you got it working. Just as an FYI, if you use ‘reply’ under a persons name or tag them, it will send them a notification. I never got any so didn’t even know you responded!

alright, @petro - cool. Thanks for you help. Also thanks @pnbruckner :slight_smile:

1 Like