Single youtube api request with parsed results to rest json_attributes

Hello,

I’ve been struggling with a problem for a while now.

I’m trying to run a single youtube api request and get the last five videoIDs from the request and then create an html-template-card from those five IDs.

I’ve had success create five separate request but would like to consolidate to one as each query -with a 100 “price”- means I can only do 100 queries a day.

My thought was to try and parse my request and extract the videoID and add them as sensor attributes.

I can parse the json request using this method: $.items[:0].id.videoId but I’m not sure how to translate this to Jinja2. The request results are nested.

A response result with only two items looks like this for the Dev Tools Template:

{% set value_json = {
   "kind":"youtube#searchListResponse",
   "etag":"ikzEL69Ko8m2pdzUzxAVQ82caKs",
   "nextPageToken":"CAUQAA",
   "regionCode":"CA",
   "pageInfo":{
      "totalResults":924,
      "resultsPerPage":5
   },
   "items":[
      {
         "kind":"youtube#searchResult",
         "etag":"9vPq06Fozbyt0GhnC05L-NYb0r8",
         "id":{
            "kind":"youtube#video",
            "videoId":"t1NUH8xDcAY"
         },
         "snippet":{
            "publishedAt":"2022-05-27T08:11:46Z",
            "channelId":"UCagiBBx1prefrlsDzDxuA9A",
            "title":"Elon Musk & The Cold Hard TRUTH About Billionaires",
            "description":"Join Patreon: https://www.patreon.com/solvingthemoneyproblem BlockFi Bitcoin Rewards Card https://bit.ly/3wlnXFn ...",
            "thumbnails":{
               "default":{
                  "url":"https://i.ytimg.com/vi/t1NUH8xDcAY/default.jpg",
                  "width":120,
                  "height":90
               },
               "medium":{
                  "url":"https://i.ytimg.com/vi/t1NUH8xDcAY/mqdefault.jpg",
                  "width":320,
                  "height":180
               },
               "high":{
                  "url":"https://i.ytimg.com/vi/t1NUH8xDcAY/hqdefault.jpg",
                  "width":480,
                  "height":360
               }
            },
            "channelTitle":"Solving The Money Problem",
            "liveBroadcastContent":"none",
            "publishTime":"2022-05-27T08:11:46Z"
         }
      },
      {
         "kind":"youtube#searchResult",
         "etag":"b9tJB_ZQCFZ0IVwEVqzzVPqtTf4",
         "id":{
            "kind":"youtube#video",
            "videoId":"NbkBZolmksg"
         },
         "snippet":{
            "publishedAt":"2022-05-27T03:58:28Z",
            "channelId":"UCagiBBx1prefrlsDzDxuA9A",
            "title":"Tesla Stock SOARS As Elon Squeeze's Twitter’s 🍒s",
            "description":"Join Patreon: https://www.patreon.com/solvingthemoneyproblem BlockFi Bitcoin Rewards Card https://bit.ly/3wlnXFn ...",
            "thumbnails":{
               "default":{
                  "url":"https://i.ytimg.com/vi/NbkBZolmksg/default.jpg",
                  "width":120,
                  "height":90
               },
               "medium":{
                  "url":"https://i.ytimg.com/vi/NbkBZolmksg/mqdefault.jpg",
                  "width":320,
                  "height":180
               },
               "high":{
                  "url":"https://i.ytimg.com/vi/NbkBZolmksg/hqdefault.jpg",
                  "width":480,
                  "height":360
               }
            },
            "channelTitle":"Solving The Money Problem",
            "liveBroadcastContent":"none",
            "publishTime":"2022-05-27T03:58:28Z"
         }
      },
   ]
}
  %}

{{ value_json["items"][1].id.videoId}}

Maybe my approach is wrong. Any suggestions would be appreciated. Thank you.

You can just use the map filter to transform a list by pulling out one attribute from each item:

{{ value_json["items"][:5]
    | map(attribute="id.videoId") | list }}

Also that won’t throw an error even if there’s less then 5, jinja just takes the rest if you picked a number larger then the length.

This was what I was looking for and worked well thank you for the reply.

When I try to create my card it treat the list as a complete string. So for example, something like this which I though would get the second element, simply returns the the characters starting at the second index.

{{states.sensor.youtube_second_latest_video.state[2:]}}

Initially I thought something like this would be the quick solution for my first element.

{{states.sensor.youtube_second_latest_video.state[0]}}

But this fails.

Thanks for your help.