Lower volume when headphones are connected.

Is there a trigger similar to: Lower the volume when the speakers change. To protect my ears I would like to automatically lower the volume when headphones are connected to the Mac.

Hi @Frank1, I currently use Hammerspoon to monitor audio speaker changes to trigger workflows. Specifically, I've configured Hammerspoon to show a HUD notification of my Airpods battery on my Mac whenever I put them on or take them off (i.e. connect or disconnect from my MacBook Pro).

-- Audio device watcher
local audioWatcher = hs.audiodevice.watcher.setCallback(function(event)
	local deviceType
	if event == "dOut" then
		deviceType = hs.audiodevice.defaultOutputDevice()
	elseif event == "dIn" then
		deviceType = hs.audiodevice.defaultInputDevice()
	else
		return
	end
    -- Safety check: deviceType can be nil during transitions
    if not deviceType then return end
	local deviceName = deviceType:name()
    
    -- Safety check: deviceName can be nil
    if not deviceName then return end
	if string.find(deviceName, "MYAIRPODSPRO") then
        -- Use hs.task instead of hs.execute to avoid blocking the main thread
        -- The system_profiler command can take seconds, which would freeze HS otherwise
		local script = "system_profiler -json SPBluetoothDataType | jq -r '.SPBluetoothDataType[0].device_connected[0][\"" ..
			deviceName .. "\"] | \"L: \\(.device_batteryLevelLeft)\\nR: \\(.device_batteryLevelRight)\"'"
		
        local task = hs.task.new("/bin/bash", function(exitCode, stdOut, stdErr)
            if exitCode == 0 and stdOut then
                -- Trim whitespace from output
                local cleanOutput = stdOut:gsub("^%s*(.-)%s*$", "%1")
                if #cleanOutput > 0 and cleanOutput ~= "null" then
                    hs.alert.show(cleanOutput, nil, nil, 5)
                end
            end
        end, {"-c", script})
        
        task:start()
	end
end)

hs.audiodevice.watcher.start()
hs.alert.show("Audio device watcher started")

I don't know if this is achievable in BTT.

Thanks for the tip, @fortred2 . Unfortunately, I'm not familiar with Hammspoon ... and it looks pretty complicated :slight_smile:

But I found out that Keyboard Maestro has a trigger like that. So I'll use that, even though I'd rather do it with BTT.

Yep, good idea to add some audio related triggers!

1 Like