I was in the process of connecting home assistant to my kodi instance. The first problem I tried to solve was to play one random file from one directory. This wasn't very easy, but doable. Home assistant seems more like a tool for people with some experience configuring software.
The first step was to allow access to the directory with the videos. Then I created a folder sensor. The necessary entries in configuration.yaml look like this:
homeassistant: allowlist_external_dirs: - /path/to/video media_dirs: homevideo: /path/to/video/home sensor: - platform: folder folder: /path/to/video/home
With these changes the rest of the configuration can be done in the the web interface. In Settings -> Automations and Scenes a new Automation can be created with an Action that looks like the example below:
Getting a random file from a directory hierarchy
After I had done this I thought it should be easy to get a random file from a nested structure. However, the folder sensor only checks one folder and doesn't support recursion. It took me a while to find a working solution, though I'm not perfectly happy with it.
This solution uses a command_line sensor. The downside is that this is a polling sensor, by default it executes the command every 60 seconds. This is more of an overhead than I need, but I'll accept that for now.
homeassistant: allowlist_external_dirs: - /path/to/video media_dirs: homevideo: /path/to/video/home sensor: - platform: folder folder: /path/to/video/home - platform: command_line name: tv_show_favorite command: "/usr/bin/find /path/to/video/shows/favorite -type f -size +5000 | shuf | head -n1"
Using this in an action template is straight forward:
That's all. If you know of a better (non polling) method leave a comment! I'm using my own caching find command at the moment.