I have several devices to play my radio-stream. (A (windows) laptop from work, an HP Pavilion, a GigaByte mini PC with AMD Ryzen 7 …)
Depending on the work I do, the mood I’m in, one of these devices is playing my radio-stream.
They can also be connected via network-cable or WiFi.
I have one slider in HA, and depending on what device is playing my music stream, and the way it is connected, I can use this one and the same slider, to control the volume of my music stream from within HA …
I will explain how I did this …
1. The slider itself
A. Create an input_number helper and name it laptop_volume, with a min. value of 0 and max. value of 100
B. Install the My Cards Bundle via HACS ( more info here )
C. Add a slider to a dashboard, use this as an example yaml:
type: custom:my-slider
entity: input_number.laptop_volume
intermediate: false
height: 30px
mainSliderColor: rgb(255,224,0,50%)
secondarySliderColor: rgb(128,128,128,1%)
thumbColor: rgb(255,255,255,100%)
thumbColorOff: rgb(255,255,255,100%)
D. You now have a “greenish” (30px high) slider on a dashboard, now onto some automation.
2. The automation
alias: "@HELPER - adjust volume work_laptop-HP_Pavilion-Gigabyte_Ryzen7"
description: ""
trigger:
- platform: state
entity_id:
- input_number.laptop_volume
condition: []
action:
- service: mqtt.publish
data:
topic: iotlink/DOMAIN/PC_NAME/audio-commands/audio/volume
payload_template: |
{{ states['input_number.laptop_volume'].state | int }}
- if:
- condition: state
entity_id: binary_sensor.hp_pavilion_wired
state: "on"
then:
- service: shell_command.general_volume_pavilion_wired
data: {}
- if:
- condition: state
entity_id: binary_sensor.hp_pavilion_wireless
state: "on"
then:
- service: shell_command.general_volume_pavilion_wireless
data: {}
- if:
- condition: state
entity_id: binary_sensor.gigabyte_wired
state: "on"
then:
- service: shell_command.general_volume_gigabyte_wired
data: {}
- if:
- condition: state
entity_id: binary_sensor.gigabyte_wireless
state: "on"
then:
- service: shell_command.general_volume_gigabyte_wireless
data: {}
mode: restart
3. The explanation
A. We run this automation, whenever the slider-value is changed (by dragging it)
B. I have 3 machines:
– A work-machine with windows 10, which runs “IOTLink” (and uses MQTT)
– An HP Pavilion laptop, running Ubuntu 22.04 (LTS) and
– a Gigabyte miniPC, also running Ubuntu 22.04 (LTS)
– For every machine, the connection is checked if Wifi or Lan is “up and running”.
– For windows machines, we have IOTLink running as a service. Download here.
– You also need the AudioAddon for IOTLink. Watch instructions and download here.
– for both Ubuntu machines, I have added sensors in my configuration.yaml to check network connection:
binary_sensor:
- platform: ping
host: 192.168.123.23
name: "HP Pavilion - wired"
count: 4
scan_interval: 30
- platform: ping
host: 192.168.123.24
name: "HP Pavilion - wireless"
count: 4
scan_interval: 30
- platform: ping
host: 192.168.123.25
name: "Gigabyte - wired"
count: 4
scan_interval: 30
- platform: ping
host: 192.168.123.31
name: "Gigabyte - wireless"
count: 4
scan_interval: 30
– Next, I can use these sensors to check if a device is online via wired or wireless.
– For every linux device and connection-type, I created a shell command in configuration.yaml, see example:
(you need alsa installed on your linux machine, and SSH configured like in this article )
general_volume_pavilion_wired: 'ssh -i /config/ssh/id_rsa -o StrictHostKeyChecking=no USERNAME@IP_ADDRESS amixer -D pulse sset Master {{ states.input_number.laptop_volume.state }}%'
Whenever one or multiple machines are turned on, they get their volume adjusted whenever the volume slider is moved within HA 🙂