Important Since shell commands executed from HomeAssistant are now limited to 60 seconds, downloading longer/higher resolution videos will fail. I'll try to figure out a workaround for this, so stay tuned.
This custom card for HomeAssistant lets you download videos with youtube-dl to a remote machine with just one click (and one link paste).
This card utilizes Home Assistant's Shell Command integration to execute a youtube-dl command on a remote machine using SSH. Backwards communication is handled by posting information to Home Assistant's REST API using cURL.
This card requires several components to work properly. Here is an instruction on how to set it up:
- Remote machine
- youtube-dl
- SSH configuration
- Shell Command
- Script
- Communication Sensor
- Downloading card code
- Card configuration
- Troubleshooting
Your remote machine is the one that the card will download videos to. Most often it is a NAS. If you run Home Assistant in a container, you can use this card to download videos on a host system or a system in other container (just provide adequate ip address and/or port number in card configuration).
Install youtube-dl on your remote machine. Since this card does not support passing arguments to youtube-dl command (for format selection etc.) they must be inserted in a config file.
If you use Linux and want the best quality video and audio I recommend creating /etc/youtube-dl.conf
file containing
-f bestvideo+bestaudio/best
-o path/%(title)s.%(ext)s
where path
should be replaced with location that you want to download videos to. Installing FFmpeg is required with this settings, it handles merging video and audio files if they are separated (like in most YouTube videos).
If the path of your youtube-dl executable is different than /usr/local/bin/youtube-dl
it must be added in card configuration. You can check this path by typing
which youtube-dl
in your remote machine's terminal.
Please refer to youtube-dl documentation for more information about configuration and config file locations on other operating systems.
This card requires passwordless access to the remote machine over SSH. You need access to the Home Assistant's terminal to set it up, so Terminal & SSH may come in handy.
First enter the following command into the HA terminal:
ssh-keygen
All options should be skipped by pressing enter.
After generating the key, it's time to add it to the remote machine. This can be done by executing the following command in the HA terminal:
ssh-copy-id username@ip
username
must be replaced with one existing on the remote machine (you can create a new one for this sole purpose), ip
must be replaced with the IP address of the remote machine.
Warning about the ECDSA key fingerprint can be dismissed by typing yes
.
After executing this command you should be able to connect to the remote machine using:
ssh username@ip
You should not be prompted for password.
To allow this passwordless connection outside the terminal (in this case for commands executed by Shell Command) just copy the key to SSH config by typing:
cp ~/.ssh/id_rsa ~/config/.ssh/id_rsa
If you encounter any problems please refer to this guide. It covers this configuration in much greater detail (key's paths slightly differ between this instruction and the linked guide, you can provide your chosen path in card configuration).
Adding a shell command allows executing it by calling it as a service from a script.
Add following code to your configuration.yaml
file:
shell_command:
youtube_dl: /bin/bash -c "{{ command }}"
Well, it calls that shell command from above.
Add following code to your configuration.yaml
file:
script:
youtube_dl_script:
sequence:
- service: shell_command.youtube_dl
data_template:
command: "{{ command }}"
That is a sensor that receives information about download result (using cURL).
Add following code to your configuration.yaml
file:
sensor:
- platform: template
sensors:
youtube_dl_communication:
friendly_name: "youtube-dl-info"
value_template: ""
Download youtube-dl-card.js
and place it somewhere in your config/www/
folder.
Then navigate to Configuration > Lovelace Dashboards > Resources and add it there.
Path should be set to /local/
+ path of the file in www
folder, type to JavaScript Module
.
You should restart Home Assistant at this point
Adding this card to Lovelace requires providing some data. Paste the following code in text editor and fill it with data according to the table below. You can edit default values by adding their keys to this configuration.
type: 'custom:youtube-dl-card'
remote_user:
remote_ip:
lla_token: >-
Name | Description | Default |
---|---|---|
remote_ip | IP address of the remote machine | - |
remote_user | Username of the account on the remote machine with passwordless access by SSH | - |
lla_token | Long-Lived Access Token, generated in Profile menu, used by cURL to authenticate (learn more) | - |
sensor | Name of the sensor created in section 4 | sensor.youtube_dl_communication |
script | Name of the script created in section 5 | script.youtube_dl_script |
yt_dl_path | Location of youtube-dl executable | /usr/local/bin/youtube-dl |
ssh_key_path | Location of SSH key (section 3) | /config/.ssh/id_rsa |
ha_port | Port that your Home Assistant is available on | 8123 |
debug | Change to true to log executed commands to console (more in section 9) |
false |
Since Long-Lived Access Tokens are quite long, they can be pasted below >-
to become multiline arguments.
That completes the installation process, enjoy the card!
If you suddenly get a Error!
message after using this card for some time, make sure that your Youtube-DL is up to date. You can simply check it by running youtube-dl -U
on the remote machine.
If you find yourself in a situation where you think that you set everything up correctly, but you get a Error!
message after pressing a download button, enable debug
in card configuration. It logs every command to the console before executing it. You can than copy that command and execute it in Home Assistant's terminal, where you will be able to see it's output and hopefully see what is wrong.
You can also enable debugging for the shell command by adding this:
logger:
default: info
logs:
homeassistant.components.shell_command: debug
to your configuration.yaml
file and than look for Stderr
of this command in your Home Assistant's logs.
If you find any bugs, feel free to add an issue in this repository.
Big thanks to michal7778 and wojtek14a - they got me interested in Home Assistant and helped with getting a basic idea of how such a card can be created.
Shoutout to Siytek for creating this amazing guide called 'Home Assistant shell integration: Local & SSH Linux control' - it was really helpful in the process of creating this card.
Thank you - Home Assistant's community and creators of all software used in this project. It would not be possible without you!