Blueprint Automation to allow for the Tuya smart knob: ERS-10TZBVK-AA (via zigbee using zigbee2mqtt) to control the volume of 1 or 2 media players.
https://gist.github.com/lymbada/6e32b92293d1938f09a6464a04ec4a0b
Purpose. Use the smart knob, (which features OOTB control for brightness and hue of smart-lights), but instead have it control the volume of 2 media_players. (in my case 2 zones on my basic Denon amplifier, but should work with any media_players supporting volume control)
Solution
Clockwise rotation increase volume (player1)
Counter-Clockwise rotation decrease the volume (player1)
Knob / button push-in & clockwise rotation increase volume (player2)
Knob / button push-in & counter clockwise rotation decrease volume (player2)
Because the know supplies a value for the amount of change each rotation causes âaction_step_sizeâ, the faster rotations will cause a greater change in volume (while slower rotation will make smaller changes to volume)⌠Because this value from the know is high (1-255 in my testing), there is a decrease in factor by 19. As my Denon stores its volume with 0-100% (or 0.00 to 1.00), the final number the divided by 100 to get the percentage (change).
The automation runs in queued mode, so that extra rotations are all captured (rather than ignored or restarted)
Limitations
- When the button is tripple-clicked, the mode is changed between âcommandâ and âeventâ. This automation requires âcommandâ mode to function
- The volume change is a % (0.00-1.00)
*The configured media_player needs to support the âvolume_levelâ attribute - The âactionâ is reset (via MQTT message) when the rotation event is finished. As such you need to look at both the current and previous Trace when checking our the actions undertaken from the automation
- the Home Assistant in Z2M configuration requires âHome Assistant legacy entity attributesâ to be enabled, this is so that the step_size attribute is sent along with the trigger action (otherwise it is itâs on trigger event).
Source
blueprint:
name: Mqtt-VolumeKnob
description: Using the (Tuya ERS-10TZBVK-AA) MQTT Volume-Knob to controle the volume of a primary MediaPlaye (with 2nd option on hold & rotate)
domain: automation
input:
primary_media:
name: Primary Player
description: Media Player for main volume control
selector:
entity:
filter:
domain: media_player
secondary_media:
name: Secondary Player
description: an additional Media Player object for the push&rotate function
selector:
entity:
filter:
domain: media_player
volume_knob:
name: Volume Knob
description: the (Tuya) MQTT device with the rotation for controling volume
selector:
entity:
filter:
integration: mqtt
domain: sensor
mode: queued
max: 10
trigger:
- platform: state
entity_id:
- !input volume_knob
condition: []
variables:
p_media: !input 'primary_media'
s_media: !input 'secondary_media'
action:
- variables:
action: >-
{{ trigger.to_state.attributes.action }}
volume_entity: !input primary_media
- choose:
- conditions:
- >-
{{ action == "brightness_step_down" }}
sequence:
- service: media_player.volume_set
data_template:
entity_id: !input primary_media
volume_level: >-
{{ state_attr(p_media,'volume_level') -
((trigger.to_state.attributes.action_step_size / 19) | round
)/100 }}
- conditions:
- >-
{{ action == "brightness_step_up" }}
sequence:
- service: media_player.volume_set
data_template:
entity_id: !input primary_media
volume_level: >-
{{ state_attr(p_media,'volume_level') +
((trigger.to_state.attributes.action_step_size / 19) | round
)/100 }}
- conditions:
- >-
{{ action == "color_temperature_step_up" }}
sequence:
- service: media_player.volume_set
data_template:
entity_id: !input secondary_media
volume_level: >-
{{ state_attr(s_media,'volume_level') +
((trigger.to_state.attributes.action_step_size / 19) | round
)/100 }}
- conditions:
- >-
{{ action == "color_temperature_step_down" }}
sequence:
- service: media_player.volume_set
data_template:
entity_id: !input secondary_media
volume_level: >-
{{ state_attr(s_media,'volume_level') -
((trigger.to_state.attributes.action_step_size / 19) | round
)/100 }}