Play a random sound over chromecast

Hi, I’m trying to do a bit of basic scripting, and not getting very far. As a basic test, I just want to be able to play a random mp3 over chromecast.

  playrandom:
     sequence: 
      - service: media_player.play_media
        data:
          entity_id: media_player.meow
          media_content_id: "http://192.168.0.33:8081/{{ states.input_slider.sleepscape.state | INT }}.mp3"
          media_content_type: audio/mp3

The slider is either 1 or 2, and in that folder are two mp3 files called 1.mp3 and 2.mp3

I’ve fiddled with the syntax a load, and have failed to get it working, currently the error I’m getting is: ERROR (Thread-9) [homeassistant.components.recorder] Error during query: (sqlite3.OperationalError) database is locked

If I remove that script the error disappears. Is the script correct and something else is wrong?

I’ve also tried this, that didn’t work:

   playrandom:
     sequence: 
      - service: media_player.play_media
        data:
          entity_id: media_player.meow
          media_content_id: >
            {% if states.input_slider.sleepscape.state | int = '1' %}
              http://192.168.0.33:8081/1.mp3
            {% elif states.input_slider.sleepscape.state | int = '2' %}
              http://192.168.0.33:8081/2.mp3
            {% endif %}"http://192.168.0.33:8081/{{ states.input_slider.sleepscape.state | INT }}.mp3"
          media_content_type: audio/mp3

Thanks for reading!

Is your .mp3 files in /.homeassistant?

First of all those to lines make no sense:

{% if states.input_slider.sleepscape.state | int = '1' %}
{% elif states.input_slider.sleepscape.state | int = '2' %}

First either get rid of the “|int” or of the quotes around the numbers. You specifically cast the state to an integer and then try to compare it to a string (Quotes around the numbers).

Second if you are comparing something you need two equal signs. With just one you assign something usally. So I would try this:

{% if (states.input_slider.sleepscape.state | int) == 1 %}
{% elif (states.input_slider.sleepscape.state | int) == 2 %}

Also like @Coolie1101 said where are your mp3 files located?

~Cheers

Thanks for the help, I’m new to python and not an experienced programmer otherwise.

I will try this when I get home.

As for mp3 location, this is a true and valid location:
http://192.168.0.33:8081/1.mp3

There is a server running in .homeassistant/audio. Putting this in a web browser “http://192.168.0.33:8081/1.mp3” plays the file.

I tried a bunch of stuff, without quotes, with double == etc. Hopefully I was just messing up someting stupid.

Thanks again.

Ok, so I’ve tried it exactly as PhyberApex said, both getting rid of the | int and keeping the quotes AND keeping the | int and losing the quotes. Neither of those worked. This is with == instead of =.

After even more browsing around I found a different way, listed below:

  playrandom:
    sequence: 
     - service: media_player.play_media
       data:
         entity_id: media_player.meow
         media_content_id: >
           {% if is_state("input_slider.sleepscape", "1.0") %} 
             http://192.168.0.33:8081/1.mp3
           {% elif is_state("input_slider.sleepscape", "2.0") %} 
             http://192.168.0.33:8081/2.mp3
           {% endif %}
         media_content_type: audio/mp3

This doesn’t work either. No errors, just nothing happens. I am calling chromecast MP3s in other places which work fine, just none within {% %}

input_slider.sleepscape state is listed as “2.0” in the dev console.

I really don’t understand what I’m doing wrong here, what am I missing? Do you need to enable this scripting somehow or something?

Many thanks,

[quote=“whatever, post:4, topic:17050”]
As for mp3 location, this is a true and valid location:
http://192.168.0.33:8081/1.mp3

Is the above your HA address?

What server?

Where is your audio files, on a separate device?

It is the address of my Pi3, on which home assistant is running. The Pi is running an http server on that port, it was a while ago since I set it up but it does work, as attested by pasting that into a browser and other mp3s playing correctly from within automations.

I found this in another thread via @BarryHampants

- service: media_player.play_media
  data:
    entity_id: media_player.bedroom_audio
    media_content_type: audio/mp3
  data_template:
      media_content_id: >
       {{ ["http://192.168.1.7/audio/morningmusic1.mp3", "http://192.168.1.7/audio/morningmusic2.mp3", "http://192.168.1.7/audio/morningmusic3.mp3"] | random}}
1 Like

hey u
how to fix
found character ‘|’ that cannot start any token