MQTT Volume Control

The MQTT Volume Control Android app allows you to control the volume levels and mute settings of an Android device from HomeAssistant via MQTT.

The app supports HomeAssistant MQTT auto discovery, so the volume control will automatically appear as number (slider) entities, and mute as switches.

You can configure a few simple things:

  • Server address: The server name or IP address of your MQTT broker. You must configure this field for the app to work.
  • Port number: The MQTT broker’s port number – the default is 1883.
  • Username: If you need a username to log on to your MQTT broker.
  • Password: If you need a password to log on to your MQTT broker.
  • Connect using SSL/TLS: Connect to the MQTT broker using an encrypted SSL/TLS connection. This will also authenticate the server’s SSL/TLS certificate, unless you disable it (see below).
  • Trust any SSL/TLS certificate (insecure): Do not authenticate the MQTT broker’s SSL/TLS certificate, and accept any certificate. This option is strongly discouraged as it makes the MQTT communication insecure, as you cannot truly trust that the server is what it claims to be.
  • Automatically connect on app start & reboot: Check this to automatically attempt to connect when the app is started or after a device reboot.
  • Reconnect indefinitely: If the connection is lost, try to reconnect indefinitely. If unchecked, retry up to 3 times.

Once you’ve configured this, click Connect and then have a look in HomeAssistant — you should see your volume- and mute settings as new entities.

Google Play Store description

This description is from MQTT Volume Control on Google Play Store:

Remote control the audio volume of the Android device where this app is running – from HomeAssistant via MQTT.

The app solves a home automation issue I’ve had for years: In my house we have a wall-mounted Android tablet in the kitchen. This tablet is used for things like grocery lists, looking up recipes – and as our “internet radio” (via a set of active loudspeakers). However, I couldn’t mute or control the volume while eating at the dinner table – at least not until now. This is the specific problem MQTT Volume Control app solves: Remote control the audio volume in HomeAssistant.

Once the application is connected, it will launch a service that stays connected in the background so you don’t need to keep the app open. The service will try to keep the device alive, so it may cause the power usage to increase. Personally for me, this is fine as the wall-mounted tablet is always connected to a charger. You can also tell the application to automatically start when the device has booted.

The app uses HomeAssistant MQTT auto discovery. This means the volume control entities should automatically appear in HomeAssistant (see screenshot). The app provides volume level controls for the media-, call-, alarm- and notifications audio streams, as well as a mute/unmute for media and notifications – depending on what the particular device supports.

Prerequisites: You will need an MQTT broker and the HomeAssistant home automation application. HomeAssistant must also be configured to use the MQTT broker. If you don’t know what MQTT or HomeAssistant is, this app is probably not for you.

MQTT Volume Control support both unencrypted MQTT, as well as MQTT over SSL/TLS.

SSL/TLS

MQTT Volume Control supports SSL/TLS encrypted connections, which implies that you’ll need a SSL/TLS certificate installed on your MQTT broker. This basically leaves you with four options:

  1. Use a proper, trusted certificate. That is, you get your server’s certificate signed by a certificate authority that Google (Android) trusts. A few free options exist, for example https://letsencrypt.org/. This can, unfortunately, be problematic if your home network isn’t publicly available and Let’s Encrypt isn’t able to validate your server.
  2. Use a self signed certificate. This is what I use in my personal setup. See e.g. http://www.steves-internet-guide.com/mosquitto-tls/ for a guide on how to do this. Note that you’ll need to add your self-signed CA certificate (and possibly also the server certificate) to your Android device.
  3. Disable authentication using the “Trust any SSL/TLS certificate (insecure)” option. This instructs MQTT Volume Control to ignore any certificates problems, including invalid names, dates, and signatures. You really shouldn’t use this, as this is almost as unsecure as the option below.
  4. Don’t use SSL/TLS. If you’re on your trusted home network, this might be good enough.

Problems? Suggestions? Comments?

Drop me an email on makob@makob.dk.

Please also leave a review on the Play Store 🙂

Privacy Policy

The app does not communicate with anything but the configured MQTT broker.

The data you enter (server name, port, username, password, etc.) will be saved on the local Android device, and will be sent to the configured MQTT broker.

The MQTT data contains:

  • device identifiers;
    • the Android device name (Bluetooth name)
    • the Android ID
    • device model and manufacturer
  • volume control level settings
  • volume mute settings

The device identifiers are used for HomeAssistant MQTT auto discovery. Factory resetting your Android device should generate a new Android ID.

MQTT Technical Details

If you want to subscribe (read) or publish (write) volume- or mute values yourself, this section describes what MQTT Volume Control sends/receives to/from the MQTT broker.

The MQTT topics and messages are defined by HomeAssistant’s MQTT integration, and in particular by the Number and Switch MQTT device definitions:

When MQTT Volume Control starts it publishes discovery messages for each audio stream it detects; typically a volume- and mute-control for each such stream. HomeAssistant uses these discovery messages to detect what actual state- and command topics it needs to use. The discovery messages are somewhat complicated, while the state- and command messages are extremely simple.

The examples below are copy-pasted from my personal wall-mounted Asus B3-A40 tablet.

Volume Discovery Message

Topic

homeassistant/number/ee0759c05230d160/media_level/config

Message

{
    "availability":[
        {
            "topic":"dk.makob.mqttvolumecontrol/ee0759c05230d160"
        }
    ],
    "device":{
        "identifiers": ["ee0759c05230d160"],
        "manufacturer":"samsung",
        "model":"SM-T580",
        "name":"Galaxy Tab A (2016)"
    },
    "name":"Galaxy Tab A (2016) Media Volume",
    "unique_id":"ee0759c05230d160_media_level",
    "icon":"mdi:volume-high",
    "command_topic":"dk.makob.mqttvolumecontrol/ee0759c05230d160_media/level/command",
    "state_topic":"dk.makob.mqttvolumecontrol/ee0759c05230d160_media/level/state",
    "min":"0",
    "max":"15",
    "step":"1"
}

The message is a JSON document.

The “ee0759c05230d160” is the Android Device ID. You’ll want to take note of the “command_topic” and “state_topic”, which is what you need to read and write the volume level of this particular audio stream. The discovery message is sent with the retain flag. Since the message contains “min”, “max” and “step”, HomeAssistant detects this as a number control.

Mute Discovery Message

Topic

homeassistant/switch/ee0759c05230d160/media_mute/config

Message example

{
    "availability":[
        {
            "topic":"dk.makob.mqttvolumecontrol/ee0759c05230d160"
        }
    ],
    "device":{
        "identifiers": ["ee0759c05230d160"],
        "manufacturer":"samsung",
        "model":"SM-T580",
        "name":"Galaxy Tab A (2016)"
    },
    "name":"Galaxy Tab A (2016) Media Mute",
    "unique_id":"ee0759c05230d160_media_mute",
    "icon":"mdi:volume-mute",
    "command_topic":"dk.makob.mqttvolumecontrol/ee0759c05230d160_media/mute/command",
    "state_topic":"dk.makob.mqttvolumecontrol/ee0759c05230d160_media/mute/state",
    "payload_on":"on",
    "payload_off":"off"
}

This is very similar to the volume level control discovery message, except the “payload_on” and “payload_off” fields means HomeAssistant detects this as a switch control.

Again, you want to take note of the “command_topic” and “state_topic”.

Volume Control Message

Topic

dk.makob.mqttvolumecontrol/ee0759c05230d160_media/level/command

Message example

10

Values/range

From "min" to "max" as defined in the discovery message

This is how the volume is set for the specific audio stream.

Volume State Message

Topic

dk.makob.mqttvolumecontrol/ee0759c05230d160_media/level/state

Message example

7

Values/range

From "min" to "max" as defined in the discovery message

This tells you the volume level of the specific audio stream.

Mute Control Message

Topic

dk.makob.mqttvolumecontrol/ee0759c05230d160_media/mute/command

Message example

off

Values/range

"on" or "off"

This is how you control the actual mute for the specific audio stream.

Mute State Message

Topic

dk.makob.mqttvolumecontrol/ee0759c05230d160_media/mute/state

Message example

on

Values/range

"on" or "off"

This tells you whether mute is on or off for the given audio stream.

Availability State Message

Topic

dk.makob.mqttvolumecontrol/ee0759c05230d160

Message example

online

Values/range

"online" or "offline"

As you might have seen in the discovery messages, there’s a third topic involved; the “availability_topic”. This is used to inform HomeAssistant whether the device is online or offline. When MQTT Volume Control has started, it publishes the value “online” with a retain message. It also sends an “offline” last-will-and-testament message, such that the MQTT broker automatically publishes this value in case the connection is abnormally disconnected.