Note
Thanks to matt8707, I made this project based on youtube-watching
If you prefer not to depend on AppDaemon, or you want to add the `entity_picture` attribute to the Apple TV
Check this
-
A
cookies.txtfile is still required -
Your version of
yt-dlpmust be2025.03.31or later. Even if your Home Assistant core version is2025.4.1, you still need to update it.2-1. Log in to the console window. It’s not an ssh addon or putty!
2-2. Run
login2-3. Run
docker exec -it homeassistant /bin/bash2-4. Run
pip install -U yt-dlp2-5. Run
python3 -c "import yt_dlp; print(yt_dlp.version.__version__)"(Verify that the update is complete. If it outputs 2025.03.31, then it’s OK.) -
In the
/config(homeassistant)/python(any folder name)/, create two files:youtube_thumbnail.pyandset_entity_picture.py. -
Add the following to
/config/secrets.yaml:ha_host: "http://{your_ha_ip}:8123"ha_token: "{yout-long-lived-token}" -
Insert the following code into
youtube_thumbnail.py:
import yt_dlp
import json
URL = "https://www.youtube.com/feed/history"
ydl_opts = {
"cookiefile": "/config/python/.cookies.txt",
"skip_download": True,
"playlist_items": "1",
"quiet": True,
"no_warnings": True,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(URL, download=False)
data = ydl.sanitize_info(info)
entry = data.get("entries", [data])[0]
print(
json.dumps(
{
"channel": entry.get("channel"),
"title": entry.get("fulltitle"),
"video_id": entry.get("id"),
"thumbnail": entry.get("thumbnail"),
"original_url": entry.get("original_url"),
},
indent=2,
)
)
- Insert the following code into set_entity_picture.py:
But I found that from secrets import get_secret didn’t work, so I just put the
TOKENandHOSTright in.
import argparse
import requests
import yaml
with open('/config/secrets.yaml') as f:
secrets = yaml.safe_load(f)
HOST = get_secret("ha_host")
TOKEN = get_secret("ha_token")
def update_entity_picture(entity_id, entity_picture):
url = f"{HOST}/api/states/{entity_id}"
headers = {"Authorization": f"Bearer {TOKEN}", "Content-Type": "application/json"}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
data["attributes"]["entity_picture"] = entity_picture
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
print("ok")
else:
print("Error posting update: ", response.text)
else:
print("Error retrieving state: ", response.text)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="update entity_picture")
parser.add_argument("--entity_id", required=True, help="entity_id")
parser.add_argument("--entity_picture", required=True, help="entity_picture")
args = parser.parse_args()
update_entity_picture(args.entity_id, args.entity_picture)
- Add the following
command_linesensor to/config/configuration.yaml:
command_line:
- sensor:
name: youtube_thumbnail
command: "python3 /config/python/youtube_thumbnail.py"
value_template: "{{ value_json.thumbnail }}"
json_attributes:
- channel
- title
- video_id
- thumbnail
- original_url
scan_interval: 86400
- Add the following shell_command to
/config/configuration.yaml:
shell_command:
set_entity_picture: "python3 /config/python/set_entity_picture.py --entity_id '{{ entity_id }}' --entity_picture '{{ entity_picture }}'"
- Create an automation.
alias: Set youtube entity_picture
triggers:
- trigger: state
entity_id:
- media_player.sovrum
- media_player.vardagsrum
to:
- playing
- paused
conditions:
- condition: template
value_template: >
{% set entity_id = trigger.entity_id %}
{% set youtube = 'sensor.youtube_thumbnail' %}
{{ is_state_attr(entity_id, 'app_id', 'com.google.ios.youtube')
and (state_attr(entity_id, 'media_artist') != state_attr(youtube, 'channel'))
and (state_attr(entity_id, 'media_title') != state_attr(youtube, 'title')) }}
actions:
- action: homeassistant.update_entity
data:
entity_id:
- sensor.youtube_thumbnail
- action: shell_command.set_entity_picture
data:
entity_id: >
{{ trigger.entity_id }}
entity_picture: >
{{ states('sensor.youtube_thumbnail') }}
mode: single
- It’s done. Now, when you play or pause the Apple TV, the
media_player.apple_tv (used as a trigger in the automation)will have anentity_pictureattribute.
Make a thumbnail of a recently watched youtube video on apple tv as a homeassistant sensor
- You need an MQTT integration for
MQTT discoveryand a server running theMQTT broker.
- To authenticate with youtube, you need to set a HTTP Cookie File.
In order to extract cookies from browser use any conforming browser extension for exporting cookies. For example, Get cookies.txt LOCALLY (for Chrome) or cookies.txt (for Firefox).
Tip
for Chrome
- Open a new Incognito tab.
- Open cookies.txt LOCALLY.
- Export the cookies with Export Format: Netscape (You can also copy them if you prefer).
- Close the Incognito tab.
- Save the
.txtfile as/homeassistant/.youtube_cookies.txt
Warning
If you do not obtain cookies in an incognito tab, the cookies may expire very quickly.
Use HACS or Download the youtube_thumbnail.py file from inside the apps directory to your local apps directory. then add the configuration to enable the youtube_thumbnail module.
- To use appdaemon, you must make appdaemon visible in your HACS settings. CHECK
- You can now see appdaemon in the
HACStab. go toHACStab - Three dots in the upper right > custom repositories >
Addhttps://github.com/kkqq9320/Youtube-Thumbnail, type isappdaemon - Search
Youtube-Thumbnailand install - If you're having trouble, CHECK
- go to Next step
Choose only one.
- Add
app_dir: /homeassistant/appdaemon/appsonappdaemon.yaml
- Copy
Youtube-Thumbnail/apps/youtube_thumbnail.pyor/homeassistant/appdaemon/apps/Youtube-Thumbnail/youtube_thumbnail.py - Now you need to take this and paste it into your
addon_configdirectory. - Paste file to
/addon_configs/a0d7b954_appdaemon/apps/ /addon_configis one level above/homeassistantin the file structure. CHECK
Caution
Note: /config will not work. Use /homeassistant instead, as /homeassistant = /config.
| key | required | type | default | description |
|---|---|---|---|---|
module |
Yes | string | youtube_thumbnail | The module name of the app. |
class |
Yes | string | YouTubeThumbnail | The name of the Class. |
apple_tv_entity_id |
Yes | string | media_player.apple_tv |
Enter your Apple TV entity ID. Only one Apple TV is supported. |
cookies_url |
Yes | string | /homeassistant/.youtube_cookies.txt |
Specify the path where you saved the .txt file. |
## apps.yaml
youtube_thumbnail:
module: youtube_thumbnail
class: YouTubeThumbnail
apple_tv_entity_id: media_player.4k
cookies_url: /homeassistant/.youtube_cookies.txt- Thumbnail URL
- Channel
- Title
- Video ID
- Duration String
- Thumbnail
- Original url

