Skip to content

Instantly share code, notes, and snippets.

@logikal
Last active December 18, 2024 23:42
Show Gist options
  • Save logikal/25cc11f929a0a4e06eac036252d6b21b to your computer and use it in GitHub Desktop.
Save logikal/25cc11f929a0a4e06eac036252d6b21b to your computer and use it in GitHub Desktop.
a HomeAssistant automation for using an MQTT smart knob for controlling the volume of a media_player
alias: "Knob: Volume Up/Down"
description: "Knob: Volume Up/Down"
triggers:
- domain: mqtt
device_id: 96d04892651a90bd5c81cd7211453052
type: action
subtype: brightness_step_up
trigger: device
- domain: mqtt
device_id: 96d04892651a90bd5c81cd7211453052
type: action
subtype: brightness_step_down
trigger: device
conditions: []
actions:
- variables:
action: "{{ trigger.payload }}"
media: media_player.tv_arc
volume_level: "{{ state_attr(media,'volume_level') | float | round(2) }}"
action_step_size: "{{ states.sensor.smart_knob_action_step_size.state_with_unit }}"
- choose:
- conditions:
- condition: template
value_template: "{{ action == \"brightness_step_down\" }}"
sequence:
- data_template:
entity_id: media
# the /5 here smooths out the volume steps so they're not so jarring
volume_level: >-
{% set sub = ((state_attr(media,'volume_level') - (action_step_size | float / 5 / 100 ))) | round(2) %}
{{ ([0, sub, 1]|sort)[1] }}
action: media_player.volume_set
- conditions:
- condition: template
value_template: "{{ action == \"brightness_step_up\" }}"
sequence:
- data_template:
entity_id: media
# the /5 here smooths out the volume steps so they're not so jarring
volume_level: >-
{% set sum = ((state_attr(media,'volume_level') + (action_step_size | float / 5 / 100 ))) | round(2) %}
{{ ([0, sum, 1]|sort)[1] }}
action: media_player.volume_set
mode: queued
max: 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment