Media Player platform for Mopidy

This is exactly what I’m looking to do, but I’m missing something somewhere.
I am new to all of this running mopidy and snapcast, but have it working with two instances of mopidy pointing to one snapserver and I can choose between the two mopidy instances from each snapclient.
Yay!
Now I’m trying to do what you are saying and figure out how to inject something else into the stream.
I’ve been trying picoTTS from HA and the mopidy instance(s) stop what they were playing, but I don’t hear anything. picoTTS works with other media players.
I know this isn’t what this thread is about and I don’t want to take it off topic, but if you have an example of how you play your door chime maybe I could learn something from it.

Thanks

Will this work for TTS on HA Host Raspberry Pi audio output jack… as Mopidy ded before it was removed? And I assuming you use the same media_player config.

@danbutter
Sorry for the late reply. Real life has a way to rear its ugly head sometimes…
So, real life example.

My doorbell is a voip device connected using SIP to my Asterisk PBX.
My Asterisk server has a queue which triggers the execution of a bash script.
Said script calls the HA API to execute a HA script.

This is possibly not the best way to do it, but I needed to come up with something fast at the time, as I had enough of OpenHAB, and wanted to switch overnight.

---
doorbell_ring:
  alias: Doorbell is ringing
  description: The doorbel is rining, play doorbell

  sequence:
  - service: mopidy.snapshot
    data:
      entity_id: media_player.figrindan_local

 - service: media_player.volume_set
    entity_id: media_player.figrindan_local
    data:
      volume_level: 0.2 

  - service: media_player.play_media
    entity_id: media_player.figrindan_local
    data:
      media_content_id: http://<my local webserver>/notifications/big_ben.mp3
      media_content_type: music

  - delay: 00:00:10

  - service: mopidy.restore
    data:
      entity_id: media_player.figrindan_local
...

The real script I took this from has more, but I just copied the relevant parts for mopidy.
So the sequence has 4 steps:

  1. Make a snapshot of what is playing, and all other settings at the time (including volume)
  2. Set the Volume to 20%
  3. Play my Big Ben mp3 which I keep on my local web server (this could be a URI through Mopidy just as well)
  4. Wait for 10 seconds
  5. Restore mopidy to the state it was when creating the snapshot.

It took me a while to understand the playback action does not wait to be completed. It needed to have the delay action to postpone the restore action.

I have no experience with TTS or PicoTTS, as I’ve never ventured into this. But I expect something similar can be achieved with TTS.
If you’re willing to give me more details of your setup (a PM is fine if you value your privacy), I can try to mimic what you have and try to get it working overhere.

@REALogics
I have no clue. I haven’t tested this (yet). If you would care to share your setup, I am willing to give it a shot.

No apologies needed…I try to check in here a few times a week, but really only get any time to play with this stuff on weekends. Thanks for the reply. Makes sense how you are calling the different services in a sequence.
I’ve messed around with the TTS a bit more and can’t get it still. I can get TTS to work by using another media player entity like browser_mod.
The only thing I found that I even suspect is the URI may be off. When I try to send TTS using developers tools - services I can see it in the Iris Now playing section. It gives the address like this:

https://192.168.1.230:8123/api/tts_proxy/6e71b3c640_en-us_-_picotts_remote.wav

I’m trying to use picotts to keep it local, but using google by clicking in the media player and filling in the text to speak box has the same result.
image

https://192.168.1.230:8123/api/tts_proxy/aa1258d_en_-_google_translate.mp3

I’m running HassOS in a VM and my mopidy instances are all on the same network.
Can anyone else type into the text to speak box in the media player card for their mopidy instance and hear it?

Thanks

Well I figured out why it didn’t work and it was the URL. Since I have a duckdns address the local address that is being sent didn’t resolve. I temporarily changed my internal URL in HA to be that of my duckdns address and TTS to various mopidy instances I have setup started working.
Unfortunately I can’t leave it that way. I am one that like these things to work when the internet is down so this setup can’t be permanent.
I’ll have to do some more searching on how to do TTS when you have duckdns setup. Or reverse proxy…or???

Loving this integration! Got it working with Spotify and can see the playlists! Thank you!

Do you have any intention of adding shuffle and repeat?

@danbutter
that is not my strong suit, I’m afraid.

@avrealm
I thought it was in it. thanks for pointing this out. I will get to the bottom and fix it.

I had a quick look, and shuffle and repeat are supported by my platform.
The default media player card doesn’t seem to offer a button for this functionality, it seems, but mini-media-player does, at least for the shuffle part.

hide:
  shuffle: False

If you want to offer a repeat option, you’ll need to create a button and have it execute a service

service: media_player.repeat_set
target:
  entity_id: media_player.figrindan_local
data:
  repeat: all

replace all with one to repeat the current song, or off to turn off the repeat alltogether.

No worries.

The shuffle question made me think…is there a way to search? I use the mopidy youtube functionality and don’t see a way to search for anything in HA. I use iris to search now, but if I’m just missing something I’d like to figure it out.

Thanks

That’s a shortcoming of the media library, unfortunately…

What I could do is create a mopidy-only search service, that can be called.

Arguments for each album, artist, track and source.

And the result of this is sent as a playlist to mopidy.
The downside, is there’s no way I see to make the search ‘dynamic’ and auto-completing like modern-day search engines.

Care to chip in?

Boy as time goes on and I play with this whole Home Automation hobby I really with I could do some programming. I’ve not got the skills though so I’m afraid I’d not be much for help. I could test, but that is about it.
Thanks again for your work and also for letting me know I didn’t miss anything related to the searching.

That’s allright, I was also referring to thinking about features.
If anything comes to mind, don’t hold back!

@danbutter (and others interested)
While looking around, I found this:

Roku offers a search service which allows to search for keywords.

Mopidy’s search API allows for this to be used. but we can also perform targeted searches, ie album, trackname only.

I’m thinking about a search service which allows to do all of this in a similar way:

mopidy.search(any=keywords,album=keywords,track_name=keywords,…source=source, entity_id=entity)

only one of the attributes (any, album, …) being required
The result of this would then be added to the existing playing tracklist (if any)
Or maybe I should add another attribute controlling this? ie clear_current_tracklist?

@danbutter

I made a search service, and added it to the repo.
Documentation is also on the repo. Give it a whir, and let me know what you think.

I love the snapshot/restore feature, which I use extensively, as I’m usually playing a a radio stream, which is interrupted by a lot of voice notifications…
But I don’t seem to get a hang of volume setting… does a snapshot also save and restore volume value or should it always be set manually prior snapshot and after restore? If later, is there a way for volume level to be included in a snapshot?

The snapshot service saves the following states:

  • player queue
  • current queue index
  • current media position
  • volume
  • muted or not
  • repeat mode
  • shuffled or not

and restores them when using the restore service.

The volume is set using the media_player.volume_set service.

Something like this does the same. If this works, then there’s no reason why restore doesn’t work.

service: media_player.volume_set
data:
  volume_level: 0.4
target:
  entity_id: media_player.figrindan

If you scroll up, you can find how I use the snapshot feature, if you didn’t see that already.

Also, make sure you’re running the latest version. It’s been a long time, but I remember committing a couple of updates to the repo when working on the snapshot/restore functionality.

If it stil doesn’t work, could you provide me with your script/whatever used to control snapshot/restore?

Sorry, stupid me, in two of my automations (don’t ask me why) I had command order messed up (volume set to 100% prior to snapshot), therefore it seemed like it’s not working as volume remained 100% after restore - well, I instructed him to do so :). Got it sorted now, works perfectly, thanks anyway :).

Awesome! I’m glad it’s working!

Still not quite perfect, one issue is still bugging me. I have an automation, which triggers, if kitchen window is opened more than 15 minutes. When triggered, it runs this script:

alias: Window opened warning
sequence:
  - service: mopidy.snapshot
    data:
      entity_id: media_player.mopidy
  - service: media_player.volume_set
    target:
      entity_id: media_player.mopidy
    data:
      volume_level: 1
  - repeat:
      until:
        - condition: state
          entity_id: sensor.kitchen_window
          state: 'True'
      sequence:
        - service: tts.microsoft_say
          data:
            entity_id: media_player.mopidy
            message: Close the kitchen window!
        - delay: '00:01:00'
  - service: mopidy.restore
    data:
      entity_id: media_player.mopidy
mode: single

If mopidy is playing, when it triggers, it works fine.
But if mopidy is off, when it triggers, I would expect, that it will turn back to off after my TTS is spoken, but it doesn’t. It just plays the last thing from mopidy (that means the last thing, that has been played
before turning it off, whenever it hapened) and at 100% volume level. So basically, something is restored, but not what has been snapshot.
If I understood correctly, the snapshot is made also if it’s off and it should also restore to off state, or not?

I believe you may have found a bug.
I do not save the playback state.
Let me fix that