How to get the last `active_audio_output` of a mobile app?

I have a few mobile devices connect to a home assistant docker container. One of the devices is named ‘MBP’ and I can get the state of the audio ouput by the following template:

{{ states('sensor.mbp_active_audio_output') }}

It shows the selected audio output device only when MBP is making noise. The state changes to inactive once MBP is silence. In order to keep track of the last active audio output device, I have created a SQL entity with the following select query:

SELECT
	state
FROM
	states
WHERE
	metadata_id = (
		SELECT
			metadata_id
		FROM
			states_meta
		WHERE
			entity_id = 'sensor.mbp_active_audio_output'
		)
	AND state <> 'Inactive'
ORDER BY
	last_changed_ts DESC
LIMIT
	1;

The SQL entity worked fine until I have upgraded home assistant to 2025.06.0. The state of sensor.mbp_active_audio_output is updated whenever a noise is made but the states does not. It stuck with a value before the update was done.

How do I fix that?