Updating Comma-separated List on Selection Widget

Hi all

I’m relatively new to OpenHab, and so far have gone with the ‘move fast and break things’ approach, learning just enough about how it works to make it do what I want (essentially, use HabPanel on a RPi with touchscreen as a control centre).

My problem is this: I’m using the Selection widget to change my current playback device on Spotify Connect (using a modified version of the webconnect api wrapper written by pmpkk - while it’s awesome, I don’t want/need all the skinning provided by the matrix-theme etc and would rather use ‘stock’ HabPanel items wherever possible). This works fine when set to the ‘Comma-separated List’ setting with manually-input values, but as far as I can work out, it can’t/won’t work with ‘Server-provided item options’ because the Spotify script works with two separate items - one containing all the available Spotify Connect devices and one to change the current device id. This is problematic because, periodically, the device ids change on the Spotify API, and my Selection widget stops working properly.

So I’m trying to find a way to update the Comma-separated List regularly/automatically. I’ve written a simple python script which grabs the data from my spotify_device_list item and formats it to suit the config setting for a Selection widget’s Comma-separated List, but I’m not sure how (or if) I can automate actually changing the value for my particular Selection widget - at the moment all I can do is manually copy/paste the output of my script into HabPanel.

I’ve looked at /var/lib/openhab2/config/org/openhab/habpanel.config and can see where I need to replace the existing settings (and add a lot more escaping 's) but I’m reluctant to dive deeply into adding the functionality to my script to do all the necessary reformatting/replacing because: (a) I’ll probably break things before I get them to work properly and (b) one of you more experienced and more enlightened folks might be able to point me in the direction of a better/easier method.

Does anyone know a way to do what I’m trying to do?

I don’t know HABPanel that well but I’d say take backups and go break things. Editing the file you found is probably the approach I would take.


Maybe someone more knowledgeable will chime in if there is a better way.

That was an unforeseen scenario, but you should avoid tampering those .config files - unless you have backups :smiley:

What you could do instead is add a template widget with a selection widget with dynamic config in it:

<div style="position:absolute;left:0;top:0;width:100%;height:100%"
     ng-init="model = {
              name: 'Spotify Connect',
              item: 'SpotifyConnect_Control',
              square: true,
              choices_columns: 3,
              choices_source: 'csv',
              choices: itemState('SpotifyConnect_Choices')
  <widget-selection ng-model="model"></widget-selection>

Next you would have to add a String item: SpotifyConnect_Choices and maintain its state with the CSV choices (like “id0=First Speaker,id1=Second Speaker, …”) using the results of your script. This is left as an exercise :wink: You will want a “restore on startup” persistence strategy on it.
Note that the choices would not be updated in real time with the above solution - the widget will only update its model, meaning refresh the choices by e.g. navigating to another dashboard and going back.

Thanks, ysc, appreciate you taking the time to help - it simply hadn’t occurred to me that I could use a template widget (which is stupid because the rest of my Spotify implementation relies on a couple of them). That’ll be easy to knock up now.

Last night, I followed rikoshak’s advice, did some serious spuddling, and wrote a much more complicated (and almost certainly useless to anyone but me) script to extract the current choices from the habpanel.config, compare them with the (parsed) values from the item that has the correct values from the Spotify API, and replace the entire selection block if they don’t match. It’s a ‘hammer to crack a nut’ solution, but it was a fun little side project and works (as long as I don’t cause any daft conflicts by trying to put two widgets in the same place, I suppose).

Anyway, your solution looks much more elegant and sane, but is there any way I can make the widget refresh its model periodically? It’s only a very minor inconvenience, so I’ll probably accept a ‘no’, but I can see it bugging me when, bleary-eyed, I’m stabbing four times at my little touchscreen to get some tunes going!

Did you ever make the simple widget?
I’m using the Spotify scraping python code from elsewhere on here, but want to create a playlist widget to allow me to pick any of my Spotify playlists - have you got anything that will help? If not, I’ll work through the widget code above.

Ignore the previous post - there are 2 great widgets posted already - combined them to get exactly what I’m after