Skip to content

Instantly share code, notes, and snippets.

@thekoma
Created December 26, 2025 14:30
Show Gist options
  • Select an option

  • Save thekoma/2a244a29b122ecc133c8d7d318667b92 to your computer and use it in GitHub Desktop.

Select an option

Save thekoma/2a244a29b122ecc133c8d7d318667b92 to your computer and use it in GitHub Desktop.
Normalizza il volume dei media player inattivi. Configurazione centralizzata.
alias: System - Volume Enforcer (Configurable)
description: Normalizza il volume dei media player inattivi. Configurazione centralizzata.
mode: single
variables:
exclude_players:
- media_player.m5stack_atom_echo_0fe9c4
- media_player.assistant_cdl_media_player
- media_player.esp32_s3_box_3_05421c
- media_player.voice_assistant_soggiorno
inactivity_timeout: 300
notify_service: notify.telegram_andrea
debug_mode: false
is_night: "{{ now().hour >= 22 or now().hour < 8 }}"
is_nap: "{{ 14 <= now().hour < 17 }}"
target_volume: "{{ 0.20 if (is_night or is_nap) else 0.35 }}"
candidate_players: |
{{ states.media_player
| selectattr('attributes.volume_level', 'defined')
| rejectattr('entity_id', 'in', exclude_players)
| map(attribute='entity_id')
| list }}
triggers:
- minutes: /10
trigger: time_pattern
conditions: []
actions:
- repeat:
for_each: "{{ candidate_players }}"
sequence:
- variables:
entity_id: "{{ repeat.item }}"
seconds_inactive: |
{% set s = states[repeat.item] %} {% if s is not none %}
{{ (now() - s.last_changed).total_seconds() | int }}
{% else %}
0
{% endif %}
current_vol: "{{ state_attr(entity_id, 'volume_level') | float(0) }}"
current_state: "{{ states(entity_id) }}"
- variables:
check_state: "{{ current_state not in ['playing', 'buffering'] }}"
check_time: "{{ seconds_inactive > inactivity_timeout }}"
check_vol: "{{ (current_vol - target_volume) | abs > 0.01 }}"
- if:
- condition: template
value_template: "{{ check_state and check_time and check_vol }}"
then:
- target:
entity_id: "{{ entity_id }}"
data:
volume_level: "{{ target_volume }}"
action: media_player.volume_set
- data:
title: βœ… Volume Fixed
message: |
*Device:* `{{ entity_id }}`
*Action:* {{ current_vol }} ➝ {{ target_volume }}
*Idle:* {{ seconds_inactive // 60 }} min
action: "{{ notify_service }}"
else:
- if:
- condition: template
value_template: "{{ debug_mode }}"
then:
- data:
title: 🐞 Volume Debug
message: >
*Device:* `{{ entity_id }}`
*Status:* SKIP
{% if not check_state %}πŸ”΄ In Use ({{ current_state }}){%
endif %}
{% if not check_time %}⏳ Too Soon ({{ seconds_inactive }}s
< {{ inactivity_timeout }}s){% endif %}
{% if not check_vol %}πŸ‘Œ Vol OK ({{ current_vol }}){%
endif %}
action: "{{ notify_service }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment