Creating a dynamic selection

Summary: I would like to periodically execute a rule that runs a script and use the output that scripts to update a selection.

Details: I have a Nuvo audio system w/ a Music Player that I can query and get a list of Spotify stations on my account. This lists is updated occasionally as I add and remove stations. What is the best practice for doing this within an openhab rule? Should I create a virtual item of type Number and then have the output of my rule update/overwrite a mapping file that my sitemap looks at? Additionally, there is no ‘state’ associated with this item, so I am not sure how to represent that either. These are essentially dynamic macros that can be triggered.

Following up on my own post here…
I started going down the path of creating a map transformation from numbers to strings. However, it appears there is no way to reference a MAP transformation from a selection mapping. And according to this post Change selection mappings via a rule it is also not possible to dynamically change mappings via a rule.

Any suggestions on how to create a dynamic selection of objects in OpenHab?

I have a solution that works… But its pretty ugly.

I have a rule that I execute in cron

rule 'Update Sitemap'
when
  System started or
  Time cron "0 0 12 1/1 * ? *"
then
  logInfo("Nuvo", "Updating Sitemap")
  executeCommandLine("/etc/openhab2/scripts/updateSitemapWithPlaylists.sh", 60000)
  logInfo("execTest", "Finished updating sitemap")
end

Inside the script I have the following:

#!/bin/bash
set -e
/etc/openhab2/scripts/nuvo_get_menu.rb -h 10.0.0.70 -p "Radio|Spotify|My Library|Playlists" | while read line; do echo \"$line\"=\"$line\"; done | head -c -1 | tr '\n' ',' | awk '{print $0}' | while read line; do echo \<% @spotifyPlaylists=%q\($line\) %\>; done > /tmp/spotifyPlaylist.var.erb
cat /tmp/spotifyPlaylist.var.erb /etc/openhab2/sitemaps/home.sitemap.erb | erb > /tmp/home.sitemap
cp /tmp/home.sitemap /etc/openhab2/sitemaps/home.sitemap

This fetches the spotify menu, turns its contents into a comma separated quoted list then assigns it to an erubis variable.

I have renamed the sitemap that I edit to end in a “.erb” indicating its a template.
Inside of that template file, I have the following for my playlist mappings:

Selection item=SpotifyPlaylist label="Spotify" visibility=[Audio_Family_Room_Power==ON] mappings=[<%= @spotifyPlaylists %>]

This code replaces the @spotifyPlaylists part with the appropriate mappings.

Several drawbacks:

  1. I have to run this every time I update the sitemap.
  2. The editor no longer understands the sitemap content for highlighting, etc.

Is there a better way to do this?

I’m afraid the answer is probably not. You might have better luck using HABPanel and a custom widget. There has been at least one widget posted that shows something like this for spotify.

Thanks for the validation.

It would seem displaying a dynamic set of values would be a commonly requested feature.

Commonly requested, exceptionally difficult to implement with impacts to a lot of separate repos.

I also wanted to do this, like just keep adding stuff to groups and then have the selection to display all the name of group members…

I would also be interested in a solution for this problem. Isn´t the kodi binding doing something like this in its ‘pvr-open-tv’ and ‘pvr-open-radio’ command?

Opens the PVR Radio channel with the provided name (channel’s state options contains available PVR Radio channels)

https://docs.openhab.org/addons/bindings/kodi/readme.html

Dear Simson,

Yes, that is true. The solution depends on your purpose. If you have a Selection item linked to a channel of a binding it maybe is possible. The binding has to implement a so called DynamicStateDescriptionProvider to populate the options. These will be used automatically for Selection items in some UIs (e.g. Paper UI or HABPanel).

1 Like

It sounds to me like someone may then be able to build a “dynamic selection list” binding for this purpose that doesn’t have the impact @rlkoshak mentioned? I’d be super interested in a binding for this purpose.

Alternatively, maybe someone needs to write a Spotify binding that replaces the python scripts many of us are using.

Now available in Basic UI and Classic UI too.

To be honest I have the slight feeling that a binding for this purpose is not the best fit. It is not about supporting any real piece of hardware or external service at all (and that’s what bindings are meant for). I would hence rather implement a Spotify binding.

On the other hand it would be a great feature to access the state description in a rule. As well as a way to trigger a rule and let a rule know that a state description has changed. I will submit a feature request for it.