A MagicMirror² module that creates an MQTT device with Home Assistant autodiscovery.
Note: This module was greatly inspired by MMM-Remote-Control.
Example: MagicMirror entities in Home Assistant
When the MagicMirror starts, the first browser client to load the module will establish the MQTT connection and handle all communication with Home Assistant. This ensures only one active MQTT connection per MagicMirror instance, even if multiple browsers are open.
The module uses MQTT autodiscovery to automatically create entities in Home Assistant, allowing you to control your MagicMirror from the Home Assistant dashboard or automations without manual configuration.
- MQTT device integration with Home Assistant via autodiscovery
- Control MagicMirror monitor (on/off)
- Adjust MagicMirror brightness
- Control visibility of individual MagicMirror modules as switches
- Restart MagicMirror process via Home Assistant
- Execute custom bash commands/scripts via Home Assistant
In your terminal, go to your MagicMirror² Module folder and clone MMM-HomeAssistant:
cd ~/MagicMirror/modules
git clone https://github.com/ambarusa/MMM-HomeAssistant/
cd MMM-HomeAssistant
npm installcd ~/MagicMirror/modules/MMM-HomeAssistant
git pull
npm installTo use this module, add it to the modules array in the config/config.js file:
{
module: 'MMM-HomeAssistant',
config: {
mqttServer: 'mqtt://localhost',
mqttPort: 1883,
username: 'mqtt_username',
password: 'mqtt_password',
deviceName: 'My MagicMirror',
autodiscoveryTopic: 'homeassistant',
monitorControl: true,
brightnessControl: true,
moduleControl: true,
monitorStatusCommand: 'xrandr --query | awk \'/Screen/ {print ($8 > 320) ? "true" : "false"}\'',
monitorOnCommand: 'xrandr -d :0 --output HDMI-1 --auto --rotate right',
monitorOffCommand: 'xrandr -d :0 --output HDMI-1 --off',
pm2ProcessName: 'mm',
refreshBrowser: true,
customCommands: [
{
name: 'Update MagicMirror',
command: 'bash ~/MagicMirror/modules/MMM-HomeAssistant/custom_commands/update_MM.sh'
}
]
}
},Using Wayland, the xrandr commands to pull monitor status and control on/off do not work. Replace the following in the 'config/config.js' file to use with wayland.
monitorStatusCommand: 'wlr-randr | awk \'/HDMI-A-2/ {found=1} found && /Enabled:/ {print ($2 == "yes") ? "true" : "false"; exit} END {if(!found) print "false"}\'',
monitorOnCommand: 'wlr-randr --output HDMI-A-2 --on',
monitorOffCommand: 'wlr-randr --output HDMI-A-2 --off',Example config for a Raspberry Pi 4B running MagicMirror server and client with PM2
| Option | Type | Default | Description |
|---|---|---|---|
mqttServer |
string | mqtt://localhost |
MQTT server address (e.g., mqtt://localhost). |
mqttPort |
int | 1883 |
MQTT port. |
username |
string | (none) | (Optional) MQTT username. If omitted, connects anonymously. |
password |
string | (none) | (Optional) MQTT password. |
deviceName |
string | My MagicMirror |
MQTT device name. |
autodiscoveryTopic |
string | homeassistant |
Autodiscovery topic for Home Assistant. |
monitorControl |
boolean | false |
Treat the display as an ON/OFF light entity. |
brightnessControl |
boolean | false |
Treat the display as a light entity with brightness. Enables monitorControl! |
monitorStatusCommand |
string | echo true |
Shell command to check the monitor status; must return true/false or 0/1 for correct operation. |
monitorOnCommand |
string | (none) | Shell command to turn on the monitor. |
monitorOffCommand |
string | (none) | Shell command to turn off the monitor. |
moduleControl |
boolean | true |
Make modules controllable as switch entities. |
pm2ProcessName |
string | (none) | If set, allows MagicMirror to be restarted via Home Assistant. |
refreshBrowser |
boolean | true |
If enabled, will programmatically open and close a browser to refresh MagicMirror clients on other instances. |
customCommands |
array | (none) | Array of custom commands to execute. See Custom Commands section below. |
Entities will appear automatically in Home Assistant if MQTT autodiscovery is enabled. You can control your MagicMirror from the Home Assistant dashboard or automations.
You can define custom bash commands or scripts that Home Assistant can trigger. Each command will appear as a button entity in Home Assistant.
The sample script at custom_commands/update_MM.sh performs a full MagicMirror update cycle with logging and safety checks. In short, it:
- Rotates its log every 5 runs and writes to
custom_commands/update_MM.log. - Uses a lock file to prevent concurrent runs.
- Updates the MagicMirror base repo with
git pull, then runsnpm run install-mmif changes were pulled. - Updates every
MMM-*module undermodules/withgit pullandnpm installwhen needed. - Restarts MagicMirror with
pm2 restartif updates were applied (configurable viaPM2_PROCESS_NAME, defaultmm).
Notes:
- The script does a
git reset --hard HEADin the base repo and in each module to avoid conflicts. If you keep local changes, they will be discarded. - You can pass a MagicMirror path as the first argument; otherwise it defaults to
~/MagicMirror. - If
pm2is not installed, the script will skip restart and log a reminder.
Add a customCommands array to your config:
customCommands: [
{
name: 'Update MagicMirror',
command: 'bash ~/MagicMirror/modules/MMM-HomeAssistant/custom_commands/update_MM.sh'
},
{
name: 'Say Hello',
command: 'echo "Hello from MagicMirror!"'
}
]| Property | Type | Required | Description |
|---|---|---|---|
name |
string | Yes | Display name for the command in Home Assistant. Internal name will be automatically converted to lowercase with underscores. |
command |
string | Yes | Bash command or script path to execute. |
- Open your browser's developer console to check for JavaScript errors or warnings.
- Check the MagicMirror logs for errors or warnings (run
npm startnpm run serverorpm2 restart xx; pm2 logs xxfrom your MagicMirror directory and watch the terminal output). - Use MQTT Explorer or a similar tool to easily investigate MQTT messages and topics.
- Optionally, you can temporarily change the
autodiscoveryTopicin your config to something likedebugto see what messages are intended to be sent for Home Assistant autodiscovery.
npm install- Install devDependencies like ESLint.npm run lint- Run linting and formatter checks.npm run lint:fix- Fix linting and formatter issues.