I’m building a MagicMirror2 for my bathroom.
Up until now I’ve had to have some REST commands defined in the configuration to have it turn the monitor on and off from within HA, but now @bugsounet and
@sergge1 has released a MQTT module for it, and after a lot of errors and misunderstandings on my side, it’s working perfectly.
So now I can send MQTT commands from HA to turn the screen on and off, and also receive MQTT status messages from the screen, so I can configure a switch in HA with the state from that.
I use two MagicMirror2 modules, MMM-Remote-Control and MMM-MQTTbridge.
First off, the config.js from magicmirror:
config.js
{
module: 'MMM-Remote-Control',
config: {
}
},
{
module: 'MMM-MQTTbridge',
disabled: false,
config: {
mqttServer: "mqtt://mqttuser:[email protected]:1883",
mqttConfig:
{
listenMqtt: true,
useMqttBridgeFromatOnly: false,
interval: 300000,
topicSubscr: ["cmnd/bathroom/mirror"],
},
notiConfig:
{
listenNoti: true,
useMqttBridgeFromatOnly: false,
ignoreNotiSender: ["system","clock","newsfeed","currentweather","calendar"],
},
// set "NOTIFICATIONS -> MQTT" dictionary at /dict/notiDictionary.js
// set "MQTT -> NOTIFICATIONS" dictionary at /dict/mqttDictionary.js
}
}
(yes, the typos are on purpose, it will be fixed in a later version of the module).
Then there are the ‘dictionaries’ from the module.
First, the MQTT translation to Notifications:
mqttDictionary.js
var mqttHook = [
{
mqttPayload: "MONITOROFF",
mqttNotiCmd: ["MONITOROFF"]
},
{
mqttPayload: "MONITORON",
mqttNotiCmd: ["MONITORON"]
},
{
mqttPayload: "UNDIM",
mqttNotiCmd: ["UNDIM"]
},
{
mqttPayload: "DIM",
mqttNotiCmd: ["DIM"]
},
];
var mqttNotiCommands = [
{
commandId: "MONITOROFF",
notiID: 'REMOTE_ACTION',
notiPayload: {action: "MONITOROFF"}
},
{
commandId: "MONITORON",
notiID: 'REMOTE_ACTION',
notiPayload: {action: "MONITORON"}
},
{
commandId: "UNDIM",
notiID: 'REMOTE_ACTION',
notiPayload: {action: "BRIGHTNESS", value: "100"}
},
{
commandId: "DIM",
notiID: 'REMOTE_ACTION',
notiPayload: {action: "BRIGHTNESS", value: "50"}
}
];
module.exports = { mqttHook, mqttNotiCommands};
Then the other way around, from MagicMirror notifications to MQTT:
notiDictionary.js
var notiHook = [
{
notiId: "USER_PRESENCE",
notiPayload: [
{
payloadValue: true,
notiMqttCmd: ["SCREENON"]
},
{
payloadValue: false,
notiMqttCmd: ["SCREENOFF"]
},
],
},
];
var notiMqttCommands = [
{
commandId: "SCREENOFF",
mqttTopic: "home/bathroom/mirror/screen",
mqttMsgPayload: 'OFF'
},
{
commandId: "SCREENON",
mqttTopic: "home/bathroom/mirror/screen",
mqttMsgPayload: 'ON'
},
];
module.exports = { notiHook, notiMqttCommands };
Then in Home Assistant, I set up a binary sensor:
- platform: mqtt
name: "MagicMirror Bathroom"
state_topic: 'home/bathroom/mirror/screen'
device_class: window
And a switch:
- platform: template
switches:
magicmirror_bathroom:
friendly_name: "Magicmirror Badeværelse"
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 %}
The images I’ve created:
Then I’ve placed the switch in a lovelacecard:
- entities:
- entity: switch.magicmirror_bathroom
name: Mirror
type: entities
So I can turn it on and off manually.
I also have some presence detection that turns on light, and I’ve chosen that the mirror is to follow the light on the toilet, so the automations I have created looks like this:
- id: '1583539250849'
alias: Bath - Turn on MagicMirror when toilet light is on - daytime
description: ''
trigger:
- entity_id: light.toilet
from: 'off'
platform: state
to: 'on'
condition:
- after: '8:00'
before: '22:59'
condition: time
action:
- entity_id: switch.magicmirror_bathroom
service: switch.turn_on
- data:
payload: UNDIM
topic: cmnd/bathroom/mirror
service: mqtt.publish
- id: '1583539089923'
alias: Bath - Turn on magic mirror when light on bathroom turns on at night
description: ''
trigger:
- entity_id: light.toilet
from: 'off'
platform: state
to: 'on'
condition:
- after: '23:00'
before: 07:59
condition: time
action:
- entity_id: switch.magicmirror_bathroom
service: switch.turn_on
- data:
payload: DIM
topic: cmnd/bathroom/mirror
service: mqtt.publish
- id: '1583515369036'
alias: Bath - Turn off Magic Mirror when toilet light on bathroom turns off
description: ''
trigger:
- entity_id: light.toilet
from: 'on'
platform: state
to: 'off'
condition: []
action:
- entity_id: switch.magicmirror_bathroom
service: switch.turn_off
This works perfectly, and the switch is updated very fast (no lag)