MagicMirror integration into Home Assistant with MQTT

Just a small update, the mirror finally arrived a while ago, and I got it set up on the bathroom.
This is the result:


I’ve had to modify the configurations a bit, as there have been quite a few updates to the system.

The mirror itself needs to accept remote commands from whitelisted ips, so this added to the top of the config:

var config = {
  address: '0.0.0.0',
  port: 8080,
  ipWhitelist: ['127.0.0.1','localhost','192.168.0.0/24'],

Then there is the MMM-Remote-Control module, which has a very simple config, it accepts commands via the notification system in MagicMirror, and I also show a webmenu to controll it on http://magicmirror:8080/remote.html:

    {
      module: 'MMM-Remote-Control',
      config: {
        showModuleApiMenu: true,
      }
    },

Then there is the MMM-MQTT-module, it connects to the MQTT server on HomeAssistant, and also makes it listen to MQTT, and notifications, and then it silences some modules that sends out a lot of uninteresting notifications:

    {
      module: 'MMM-MQTTbridge',
      disabled: false,
      config: {
        mqttServer: 'mqtt://username:password@mosquitto:1883',
        mqttConfig:
        {
          listenMqtt: true,
          interval: 2000,
        },
        notiConfig:
        {
          listenNoti: true,
          ignoreNotiId: ['CLOCK_MINUTE', 'NEWS_FEED'],
          ignoreNotiSender: ['clock','newsfeed','currentweather','calendar']
        }
      }
    }

The real work is in the dictionaries for the MMM-MQTTbridge (which are located under the modules folder in the noti folder)
mqttDictionary.js this translates from MQTT to notifications, I’ve created 4 commands, monitor on/off and dim/undim:

var mqttHook = [
    {
      mqttTopic: "cmnd/bathroom/mirror",
      mqttPayload: [
        {
          payloadValue: "MONITOROFF",
          mqttNotiCmd: ["MONITOROFF"]
        },
        {
          payloadValue: "MONITORON",
          mqttNotiCmd: ["MONITORON"]
        },
        {
          payloadValue: "UNDIM",
          mqttNotiCmd: ["UNDIM"]
        },
        {
          payloadValue: "DIM",
         mqttNotiCmd: ["DIM"]
        },
      ],
    },
  ];

var mqttNotiCommands = [
    {
      commandId: "MONITOROFF",
      notiID: 'REMOTE_ACTION',
      notiPayload: {action: 'MONITOROFF'}
    },
    {
      commandId: "MONITORON",
      notiID: 'REMOTE_ACTION',
      notiPayload: {action: 'MONITORON'}
    },
    {
      commandId: "DIM",
      notiID: 'REMOTE_ACTION',
      notiPayload: {action: 'BRIGHTNESS', value: '50'}
    },
    {
      commandId: "UNDIM",
      notiID: 'REMOTE_ACTION',
      notiPayload: {action: 'BRIGHTNESS', value: '255'}
    },
  ];

module.exports = { mqttHook,  mqttNotiCommands};

Then I need to report status back to HomeAssistant so I can make a statefull switch. The USER_PRESENCE indicates if the HDMI is turned on or off.

var notiHook = [
  {
    notiId: "USER_PRESENCE",
    notiPayload: [
      {
        payloadValue: '1',
        notiMqttCmd: ["SCREENON"]
      },
      {
        payloadValue: '0',
        notiMqttCmd: ["SCREENOFF"]
      }
    ]
  }
];
var notiMqttCommands = [
  {
    commandId: "SCREENOFF",
    mqttTopic: "state/bathroom/mirror/monitor",
    mqttMsgPayload: "OFF"
  },
  {
    commandId: "SCREENON",
    mqttTopic: "state/bathroom/mirror/monitor",
    mqttMsgPayload: "ON"
  }
];

module.exports = { notiHook, notiMqttCommands };

In HomeAssistant I then have a binary sensor:

- platform: mqtt
    name: "MagicMirror Bathroom"
    state_topic: 'state/bathroom/mirror/monitor'
    device_class: window

Which indicates if the HDMI is on or off, as I send the ON/OFF directly from MagicMirror, the binary sensor is very simple.
Final step is a switch in HomeAssistant, the switch has two images representing the mirror being on or off, these are placed in the HomeAssistant config folder under /config/www/images.:

- platform: template
    switches:
      magicmirror_bathroom:
        friendly_name: "Magicmirror"
        value_template: "{{ is_state('binary_sensor.magicmirror_bathroom', 'on') }}"
        turn_on:
          service: mqtt.publish
          data:
            topic: "cmnd/bathroom/mirror"
            payload: "MONITORON"
        turn_off:
          service: mqtt.publish
          data:
            topic: "cmnd/bathroom/mirror"
            payload: "MONITOROFF"
        entity_picture_template: >-
          {% if is_state('binary_sensor.magicmirror_bathroom', 'on') %}
            /local/images/mirror_on.png
          {% else %}
            /local/images/mirror_off.png
          {% endif %}
2 Likes