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:
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.
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)
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).
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.
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.