Jump to content

Help using Python to check for a genre


Go to solution Solved by LyraTheLycan,

Recommended Posts

LyraTheLycan
Posted

Hi, I'm using a script to get details and would like to alter it, but I don't know Python. This works to filter by type:

        if self.data[0]["Type"] == "Movie":
            return

But I would like to add a filter that checks the contents of the Genres array. This is what I want but it doesn't work:

      if self.data[0]["Type"] == "Movie" and "Animation" in self.data[0]["Genres"][0:]:
            return

Many thanks for assistance!

Posted

Hi, what exactly doesn't work?

LyraTheLycan
Posted (edited)

I am trying to find how many movies in the Movies library have the Animation tag, and get that as an integer for a Home Assistant sensor.. Chances are there's a much easier way to achieve this instead of trying to manipulate a RESTful API!

Specifically I am starting with the URL pointing to all items in the library, and wanting to get multiple values from it, so adding a Genres filter in the URL will not be possible.

Thanks!

Edited by LyraTheLycan
Posted

What are your query urls?

  • Solution
LyraTheLycan
Posted

I've figured it out, changed my approach and used Jinja. Converted the json to string then counted the genre word, and divided by two because Genres and GenreTypes is in the list. It works for now at least!

rest:
  - resource: http://{ip}:{port}/Items?recursive=true&ParentId={libraryid}&Fields=Genres&api_key={api}
    method: GET
    scan_interval: 3600
    sensor:
      - name: "{name}"
        value_template: '{{ (value_json | string).count("{Genre}") / 2 | int }}'
 

  • Thanks 1
LyraTheLycan
Posted
22 minutes ago, LyraTheLycan said:

        value_template: '{{ (value_json | string).count("{Genre}") / 2 | int }}'

 

value_template: '{{ ((value_json | string).count("{Genre}") / 2) | int }}

Posted

Thanks for following up.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...