- alias: Tautulli Kill Stream - Paused Sessions After X Minutes description: 'Terminates any Plex session reported by Tautulli that has been paused longer than a specified number of minutes (configurable in ''pause_limit_minutes''). ' mode: single variables: pause_limit_minutes: 10 trigger: - platform: time_pattern minutes: /1 condition: [] action: - variables: sessions: '{{ state_attr(''sensor.tautulli_stream_count'',''sessions'') | default([]) }}' - repeat: for_each: '{{ sessions }}' sequence: - variables: this_session: '{{ repeat.item }}' sid: '{{ this_session.session_id | default(''unknown'') }}' paused_sec: '{{ this_session.Stream_paused_duration_sec | float(0) }}' state: '{{ this_session.state | lower | default(''unknown'') }}' - if: - condition: template value_template: "{{ state == 'paused'\n and paused_sec > (pause_limit_minutes\ \ * 60) \n and sid != 'unknown' }}\n" then: - service: tautulli_active_streams.kill_session_stream data: session_id: '{{ sid }}' message: 'Session paused more than {{ pause_limit_minutes }} minutes. Terminating. ' - alias: Tautulli Kill Streams - Limit All Users to One Session ( With User Exclusions) description: 'Terminates the newest stream when any non-excluded user has multiple active sessions, based on ''user'' and ''start_time_raw''. Skips partial updates where session_id is missing or empty, and allows excluded users to have multiple sessions without being terminated. ' variables: excluded_users: - admin - plex_admin mode: single trigger: - platform: state entity_id: sensor.tautulli_stream_count condition: - condition: template value_template: "{% set sessions = state_attr('sensor.tautulli_stream_count','sessions')\ \ %} {% if sessions is none or sessions|length <= 1 %}\n false\n{% else %}\n\ \ {% set ns = namespace(duplicate_found=false, seen=[]) %}\n {% for s in sessions\ \ %}\n {% if s.user is defined and s.user|trim != '' \n and s.session_id\ \ is defined and s.session_id|trim != '' %}\n {% set user_lower = s.user|lower|trim\ \ %}\n {% if user_lower not in excluded_users %}\n {% if user_lower\ \ in ns.seen %}\n {% set ns.duplicate_found = true %}\n {% else\ \ %}\n {% set ns.seen = ns.seen + [user_lower] %}\n {% endif\ \ %}\n {% endif %}\n {% endif %}\n {% endfor %}\n {{ ns.duplicate_found\ \ }}\n{% endif %}" action: - service: persistent_notification.create data: title: Duplicate Plex Sessions Detected message: "{% set sessions = state_attr('sensor.tautulli_stream_count','sessions')\ \ %} {% if sessions is not none %}\n {% set ns = namespace(duplicates=[])\ \ %}\n {% set seen_users = namespace(list=[]) %}\n {% for s in sessions\ \ %}\n {% if s.user is defined and s.user|trim != '' \n and s.session_id\ \ is defined and s.session_id|trim != '' %}\n {% set user_lower = s.user|lower|trim\ \ %}\n {% if user_lower not in excluded_users %}\n {% if user_lower\ \ in seen_users.list %}\n {% set ns.duplicates = ns.duplicates +\ \ [s] %}\n {% else %}\n {% set seen_users.list = seen_users.list\ \ + [user_lower] %}\n {% endif %}\n {% endif %}\n {% endif\ \ %}\n {% endfor %}\n Found {{ ns.duplicates|length }} duplicate session(s):

\n\ \ {% for d in ns.duplicates %}\n - User: {{ d.user }}
\n Session ID:\ \ {{ d.session_id }}
\n Title: {{ d.full_title }}
\n start_time:\ \ {{ d.stream_start_time if d.stream_start_time is defined else 'unknown'\ \ }}

\n {% endfor %}\n{% else %}\n No sessions found.\n{% endif %}" - service: tautulli_active_streams.kill_session_stream data: session_id: "{% set sessions = state_attr('sensor.tautulli_stream_count','sessions')\ \ %} {% if sessions is none %}\n unknown\n{% else %}\n {% set ns = namespace(duplicates=[])\ \ %}\n {% set seen_users = namespace(list=[]) %}\n {% for s in sessions\ \ %}\n {% if s.user is defined and s.user|trim != '' \n and s.session_id\ \ is defined and s.session_id|trim != '' %}\n {% set user_lower = s.user|lower|trim\ \ %}\n {% if user_lower not in excluded_users %}\n {% if user_lower\ \ in seen_users.list %}\n {% set ns.duplicates = ns.duplicates +\ \ [s] %}\n {% else %}\n {% set seen_users.list = seen_users.list\ \ + [user_lower] %}\n {% endif %}\n {% endif %}\n {% endif\ \ %}\n {% endfor %}\n {% if ns.duplicates|length > 0 %}\n {% set sorted\ \ = ns.duplicates|sort(attribute='start_time_raw') %}\n {{ sorted[-1].session_id\ \ }}\n {% else %}\n unknown\n {% endif %}\n{% endif %}" message: Admin Notified! Multiple sessions detected. Session Terminated. - alias: Tautulli Kill Duplicate Plex Streams (Username) description: 'Terminates the newest stream when a user has multiple active sessions, based on ''username'' and ''start_time_raw''. ' triggers: - entity_id: sensor.tautulli_stream_count trigger: state conditions: - condition: template value_template: "{% set sessions = state_attr('sensor.tautulli_stream_count','sessions')\ \ %} {% if sessions is none or sessions|length <= 1 %}\n false\n{% else %}\n\ \ {% set ns = namespace(duplicate_found=false, seen=[]) %}\n {% for s in sessions\ \ %}\n {% if s.username is defined and s.username|trim != '' %}\n {%\ \ set user_lower = s.username|lower|trim %}\n {% if user_lower in ns.seen\ \ %}\n {% set ns.duplicate_found = true %}\n {% else %}\n \ \ {% set ns.seen = ns.seen + [user_lower] %}\n {% endif %}\n {% endif\ \ %}\n {% endfor %}\n {{ ns.duplicate_found }}\n{% endif %}\n" actions: - data: title: Duplicate Plex Sessions Detected message: "{% set sessions = state_attr('sensor.tautulli_stream_count','sessions')\ \ %} {% if sessions is not none %}\n {# Use a namespace to ensure our duplicates\ \ list persists #}\n {% set ns = namespace(duplicates=[]) %}\n {% set seen_users\ \ = namespace(list=[]) %}\n \n {# Gather duplicates #}\n {% for s in sessions\ \ %}\n {% if s.username is defined and s.username|trim != '' %}\n \ \ {% set user_lower = s.username|lower|trim %}\n {% if user_lower in\ \ seen_users.list %}\n {% set ns.duplicates = ns.duplicates + [s] %}\n\ \ {% else %}\n {% set seen_users.list = seen_users.list + [user_lower]\ \ %}\n {% endif %}\n {% endif %}\n {% endfor %}\n \n Found {{ ns.duplicates|length\ \ }} duplicate session(s):

\n {% for d in ns.duplicates %}\n - Username:\ \ {{ d.username }}
\n Session ID: {{ d.session_id }}
\n Title:\ \ {{ d.full_title }}
\n Start Time (raw): {{ d.start_time_raw }}

\n\ \ {% endfor %}\n{% else %}\n No sessions found.\n{% endif %}\n" action: persistent_notification.create - data: session_id: "{% set sessions = state_attr('sensor.tautulli_stream_count','sessions')\ \ %} {% if sessions is none %}\n unknown\n{% else %}\n {% set ns = namespace(duplicates=[])\ \ %}\n {% set seen_users = namespace(list=[]) %}\n {% for s in sessions\ \ %}\n {% if s.username is defined and s.username|trim != '' %}\n \ \ {% set user_lower = s.username|lower|trim %}\n {% if user_lower in\ \ seen_users.list %}\n {% set ns.duplicates = ns.duplicates + [s] %}\n\ \ {% else %}\n {% set seen_users.list = seen_users.list + [user_lower]\ \ %}\n {% endif %}\n {% endif %}\n {% endfor %}\n {% if ns.duplicates|length\ \ > 0 %}\n {% set sorted = ns.duplicates|sort(attribute='start_time_raw')\ \ %}\n {{ sorted[-1].session_id }}\n {% else %}\n unknown\n {% endif\ \ %}\n{% endif %}\n" message: Admin Notified! Multiple sessions Detected. Terminated Stream. action: tautulli_active_streams.kill_session_stream mode: single - alias: Tautulli Kill Streams - Limit Fixed User - Kill All description: 'Terminates all Plex streams for "CHANGE_ME" whenever sensor.tautulli_stream_count changes. ' trigger: - platform: state entity_id: sensor.tautulli_stream_count condition: - condition: template value_template: "{% set sessions = state_attr('sensor.tautulli_stream_count','sessions')\ \ %} {% if sessions is none or sessions|length == 0 %}\n false\n{% else %}\n\ \ {% set ns = namespace(found=false) %}\n {% for s in sessions %}\n {%\ \ if s.username is defined and s.username|lower|trim == 'CHANGE_ME' %}\n \ \ {% set ns.found = true %}\n {% endif %}\n {% endfor %}\n {{ ns.found\ \ }}\n{% endif %}" action: - service: tautulli_active_streams.kill_user_stream data: user: CHANGE_ME message: Stream has been stopped by Admin. Contact for Support mode: single - alias: Tautulli Kill Streams - Limit Fixed User - One Stream Only description: 'Monitors sensor.tautulli_stream_count for the user "CHANGE_ME". If that user has more than one active session, the newest session is terminated. ' trigger: - platform: state entity_id: sensor.tautulli_stream_count condition: - condition: template value_template: "{% set sessions = state_attr('sensor.tautulli_stream_count',\ \ 'sessions') %} {% if sessions is none or sessions|length < 2 %}\n false\n\ {% else %}\n {% set matched = sessions\n | selectattr('username','defined')\n\ \ | selectattr('username','equalto','CHANGE_ME')\n | list %}\n {{ matched|length\ \ >= 2 }}\n{% endif %}" action: - service: persistent_notification.create data: title: Duplicate Sessions Detected for CHANGE_ME message: "{% set sessions = state_attr('sensor.tautulli_stream_count','sessions')\ \ %} {% if sessions is not none %}\n {% set matched = sessions\n | selectattr('username','defined')\n\ \ | selectattr('username','equalto','CHANGE_ME')\n | list %}\n {%\ \ if matched|length >= 2 %}\n Found {{ matched|length }} sessions for CHANGE_ME:

\n\ \ {% for s in matched %}\n - Session ID: {{ s.session_id }}
\n \ \ Title: {{ s.full_title }}
\n start_time_raw: {{ s.start_time_raw\ \ }}

\n {% endfor %}\n {% else %}\n No duplicates found.\n \ \ {% endif %}\n{% else %}\n No session data available.\n{% endif %}" - service: tautulli_active_streams.kill_session_stream data: session_id: "{% set sessions = state_attr('sensor.tautulli_stream_count','sessions')\ \ %} {% if sessions is not none %}\n {% set matched = sessions\n | selectattr('username','defined')\n\ \ | selectattr('username','equalto','CHANGE_ME')\n | sort(attribute='start_time_raw')\ \ \n | list %}\n {% if matched|length >= 2 %}\n {{ matched[-1].session_id\ \ }}\n {% else %}\n unknown\n {% endif %}\n{% else %}\n unknown\n{%\ \ endif %}" message: 'Admin Notice: Exceeded the one-session limit. Terminating newest session.' mode: single