is there a jellyfin api which I could query to get the state if someone is actively watching or not?
from swooosh@lemmy.world to jellyfin@lemmy.ml on 28 May 2024 11:36
https://lemmy.world/post/15903511

The use case is following:

I’d like to turn off my server at night. Sometimes I stop at 12, sometimes at 1 or 2. If I could ask “is someone watching?” And it returns false, I could postpone the shutdown.

The current solution is to always turn off at 2 and turn on at 7.

How do you guys handle the situation?

#jellyfin

threaded - newest

ShortN0te@lemmy.ml on 28 May 2024 11:43 next collapse

api.jellyfin.org

muntedcrocodile@lemm.ee on 28 May 2024 12:05 next collapse

Man i love foss!

swooosh@lemmy.world on 28 May 2024 13:04 collapse

Thx! It works :)

Esca@lemmy.one on 28 May 2024 13:30 next collapse

Yep, the api in jellyfin is quite easy. Someone already sent the link to the docs. The specific endpoint you want is Sessions. You need to get yourself an api_key which you can get in the admin panel. Here is an example code in Python to give you an idea on how to use the api:

#!/usr/bin/env python3

import requests

response = requests.get("http://your_jellyfin_server:8096/Sessions?api_key=your_api_key")
json = response.json()

currently_watching = []
for session in json:
    if "NowPlayingItem" in session:
        currently_watching.append(session["UserName"])

if currently_watching:
    print("Currently watching: " + ", ".join(currently_watching))
else:
    print("Nobody is watching")

If it is indeed Python that you want to use, you can adjust it to your needs depending on what you want it to output.

Basically, it is as simple as looping through every current session and checking if they have a NowPlayingItem key which is only present when they have a video open (both playing and paused). It works very reliability, I am using it to automatically change my lights when I watch a long video or movie.

swooosh@lemmy.world on 28 May 2024 16:16 collapse

Thank you!

I wrote a bash script that powers off the computer when there was no user activity for 10 minutes.

Epzillon@lemmy.ml on 29 May 2024 14:59 collapse

Neat and handy! I guess you could write some wake-on-LAN shenanigans to make it turn on when a user is trying to connect aswell 👍

swooosh@lemmy.world on 29 May 2024 15:48 collapse

Wow, that would be next level!

mmmmmsoup@lemmy.today on 28 May 2024 15:22 collapse

Just curious, why do you turn it off at night? Mine only uses ~2 watts when idling

Edit; more like 7 watts but still pretty good

Tippon@lemmy.dbzer0.com on 28 May 2024 15:43 next collapse

It depends on the server. Mine is an old Haswell based system, and uses more than 2 watts :)

mmmmmsoup@lemmy.today on 28 May 2024 20:01 next collapse

True, I’m using a M2 Mac mini. Used to use a raspberry pi

st3ph3n@midwest.social on 29 May 2024 14:40 collapse

I use one of those tiny mini PCs, with an AMD mobile CPU in it. It sips power but has enough oomph for transcoding when necessary. I’m sure the NAS that my library actually sits on uses way more power with its mechanical HDDs.

swooosh@lemmy.world on 28 May 2024 16:14 next collapse

2 watts isn’t a lot 😁 I’m not sure but my gpu alone consumes more than that when idle

[deleted] on 28 May 2024 20:02 collapse

.

PartyPatella@lemmy.ml on 28 May 2024 20:00 next collapse

Would you mind sharing the specs? Thanks in advance!

mmmmmsoup@lemmy.today on 28 May 2024 20:03 collapse

Sure! It’s an M2 MacMini, 16gb ram 512gb ssd. I monitor its power usage with iStat menus 6

rambos@lemm.ee on 28 May 2024 22:51 collapse

2W is insane. I wonder how much it actually drains from the wall.

My server idles at 30-35W and turning it off for 6 hours at night (cheaper electricity) would save like 4€ a year. Its cheap here tho

mmmmmsoup@lemmy.today on 29 May 2024 01:45 next collapse

I have a meter I could test with if I wanted, although I’m not sure why iStat would lie

mmmmmsoup@lemmy.today on 29 May 2024 03:02 collapse

Ok so I did some testing, from the wall it’s about 7 watts idle. I wonder where that extra 5 is coming from, oh well still pretty good!

Sleeping it’s less than 1 watt, so I might see if I can let it sleep at night somehow

rambos@lemm.ee on 29 May 2024 07:47 collapse

I wonder where that extra 5 is coming from

Most likely PSU efficiency because they are bad on super low power. Still insane, 7W is nothing