Skip to content

JosefJantzen/MMM-AlexaControl

Repository files navigation

MMM-AlexaControl

The MMM-AlexaControl module allows you to control your MagicMirror² with Alexa. You can turn it on and off, change the page and send notifications to other modules. For that it uses node-fauxmo nodejs module. As a result of this only an Amazon Alexa is necessary to use the module. Not even an Alexa Skill is required. It emulates an Wemos device. So acutally every smart home automation device that supports Wemos devices should be able to control your Mirror. But I only test it with an Echo Dot Gen. 3 and a Raspberry Pi 3B+.
There is a hole example configuration and examples for other modules in the Wiki. So you don't need the Readme absolutely.
Note: If you have problems look at the end of the Readme

Disclaimer

The module has some issues with the dicovering of devices. The reason for that is the npm package I used for that. I don't really know how to fix it and the developer of the package isn't active since a long time now. So if you have that problem I can't really help you. Sometimes these steps help, but not always. If you are new, give it a try with the default config shown below. Because of these issues I'm not actively developing the module.

Installation

  1. Navigate to your modules folder in a terminal:
cd ~/MagicMirror/modules
  1. Clone this repository:
git clone https://github.com/JoChef2/MMM-AlexaControl.git
  1. Navigate into the module folder and install the node modules. This step could use a few minutes.
cd ~/MMM-AlexaControl
npm install
  1. Configure the module in your config file. For that use the steps below.

  2. Start your Mirror

  3. Search in the Alexa App for new devices or say: "Alexa, discover my devices".
    Note: For some of the devices e.g. restart restarts your mirror wether or not you say on or off happen the same thing. For the monitorToggle device it isn't indifferent.
    Note: If you have problems to discover your devices or other problems look at the end of the Readme.

  4. Say "Alexa turn [device name] on/off"

Using the module

To use this module, add it to the modules array in the config/config.js file:

modules:[
    {
        module: 'MMM-AlexaControl',
        position: 'middle_center',
        config:{
            image: true,
            pm2ProcessName: "mm",
            vcgencmd: "vcgencmd"
        }
    }
]

Configuration options

The following properties can be configured:

Option Description
image Icon
Make this image visible.
Default Value: true
Possilbe Values: true or false
height Here you can change the image height.
Default Value: 265 Note: The unit is px.
width Here you can change the image width.
Default Value: 265 Note: The unit is px.
pm2ProcessName If you want to restart your Mirror with PM2 change here your PM2 processname. Here you can configure PM2 for your Mirror.
Default value: mm
vcgencmd This option chose the command to toggle your monitor on and off. I found two commands. Test them before in the terminal.
Default value: vcgencmd
Possible values:
vcgencmd = vcgencmd display_power 0 and vcgencmd display_power 1
tvservice = tvservice --off and tvservice --preferred
cec-client = echo \'standby <cecAddress> \' cec-client -s -d 1 and echo \'on <cecAddress> \' cec-client -s -d 1
hide = uses the module MMM-SleepWake to hide all modules.
xrandr = xrandr --output HDMI-1 --off and xrandr --output HDMI-1 --auto
cecAddress This option is only necessary if use cec-client.
Default value: 0.0.0.0
deviceName This option allows you to provide a name for this MM installation, useful if u have more than one Mirror installation. The alexa device names will include this name.
Default value: not used
startPort First Port for the devices. The port identify the device. So delete old devices in the Alexa App to prevent issues. You have to change it if you have two mirrors with this module. If you have set the port to 11000 the ports 10900 - 11200 are reserved. So the easiest way is to set this option on your second mirror to 12000.
Default value: 11000

Control devices

These are configured devices you can use. If you want to change their name you must edit the translation file inside /translations/en.json. You can also add languages.

Option Description
refresh This refreshs your Mirror.
Default value: true
Possible value: true and false
restart This restarts your Mirror with PM2. So make sure the pm2ProcessName is right.
Default value: true
Possible values: true and false
stop This stops your Mirror with PM2. So make sure the pm2ProcessName is right.
Default value: true
Possible values: true and false
reboot This reboots your Pi.
Default value: false
Possible values: true and false
shutdown This shutdowns your Pi. Note: When you shutdown the Pi your devices aren't available. So you must start your Pi manually.
Default value: false
Possible values: true and false
monitorToggle This can switch your monitor on and off. Make sure the used command work for you. Look at the option vcgencmd
Default value: true
Possible values: true and false
usb This device can toggle the usb power of your pi.
Note: It uses the following commands:
echo '1-1' sudo tee /sys/bus/usb/drivers/usb/unbind
echo '1-1' sudo tee /sys/bus/usb/drivers/usb/bind
Default value: false
Possible values: true and false

Page devices

If you are using the MMM-Pages module you can change the page with this module.
You only have to type your number of pages in th config file. Note: There are 100 pages available. I think that's enougth.

Option Description
pages The number of your pages you would like to control. Note: Chose 0 if you won't use this.
Default value: 0

Notification devices

Notification devices allows you to send notifications to other modules.
You can send the same notification when you turn the device on and off like this:
Note: You have to put the notifications array you chose inside the config part.

notifications: [
    {
        name: 'Notificaition 1',
        port: 11100,
        OnOff: false,
        notification: ["NOTIFICATION", "PAYLOAD"]
    }
]

You can send different notifications when you turn a device on or off like this:

notifications: [
    {
        name: 'Notification 2',
        port: 11101,
        OnOff: true,
        notification: [["NOTIFICATION ON", "PAYLOAD ON"],["NOTIFICATION OFF", "PAYLOAD OFF"]]
    }
]

Note: NOTIFICATION ON and NOTIFICATION OFF stay for the different notifications you can send when you turn on or off the device. The same thing applies to PAYLOAD ON and PAYLOAD OFF.

These are the configuration options for a notification device:
Note: They are all necessary.

Option Description
name Here you can name your device. Make sure you didn't used the name before. Delete first the old device.
port Here you can give the device a static Port. I suggest that to you because then there aren't overlaps. Preferably start with the port 11100.
OnOff If you want to send the same notification when you turn a device on and off chose true. Otherwise if you want to send different notifications chose false.
Possible values: true and false
notification If OnOff is true:
notification: [["NOTIFICATION ON","PAYLOAD"],["NOTIFICATION OFF", "PAYLOAD"]]
Replace the notifications and the payloads.
If OnOff is false:
notification: ["NOTIFICATION", "PAYLOAD"]
Replace the notification and payload.

Command device

Command devices allows you to run commands.
You can run the same command when you turn the device on and off like this:

commands: [
    {
        name: 'Command 1',
        port: 11102,
        OnOff: false,
        command: "<COMMAND>"
    }
]

You can run different commands when you turn a device on or off like this:

commands: [
    {
        name: 'Command 2',
        port: 11103,
        OnOff: true,
        command: ["COMMAND ON", "COMMAND OFF"]
    }
]

These are the configuration options for a notification device.
***Note:*** They are all necessary.
Option Description
name Here you can name your device. Make sure you didn't used the name before. Delete first the old device.
port Here you can give the device a static Port. I suggest that to you because then there aren't overlaps. Preferably start with the port 11100.
OnOff If you want to run the same command when you turn a device on and off chose true. Otherwise if you want to run different commands chose false.
Possible values: true and false
command If OnOff is true:
command: ["COMMAND ON", "COMMAND OFF"]
Replace both commands.
If OnOff is false:
command: "<COMMAND>"
Replace the command.

Custom devices

You can also create custom devices. I'm not really sure if it's useful but probably somebody need it. For that you need the option devices. Here are two examples:
If you want to do the same thing when you turn on and off your devices use this:
Note: You have to put the devices array you chose inside the config part.

devices: {
    devices: [{
        name: 'Custom 1',  
        port: 11104,              
        handler: `_this.sendSocketNotification("PAGE_CHANGED", 1)`
    }  
]}

If you want to do different things when you turn on or off your device use this:

devices: {
    devices: [{
            {
                name: 'Custom 2',
                port: 11105,
                handler: `if(action === 1){  // 1 means on
                            _this.sendSocketNotification("PAGE_CHANGED", 0)
                        }else{
                            _this.sendSocketNotification("PAGE_CHANGED", 1)      
                        }`
            }
        ]
    }
}

These are the configuration options for a custom device:

Option Description
name Here you can name your device. Make sure you didn't used the name before. Delete first the old device.
port Here you can give the device a static Port. I suggest that to you because then there aren't overlaps. Preferably start with the port 11100.
handler Use the handler for every thing you want to do. If you need this in your code use _this. Inside the examples above you can see how to differentiate between on and off.

Problems

  1. Devices not found:

    • Make sure that your Alexa and Pi are in the same Wifi. I had also the problem that my Pi was in the 2.4GHz Wifi and my Alexa in the 5GHz Wifi. It's only a problem when you discover the devices. To solve the problem disable one of the Wifis for a moment or turn the Wifi on your Pi on and off till it works.

    • Have you used the port before? Try every devices from the mirror. Probably the name is wrong. If that happen delete the device and discover for new devices. Define yourself ports for notification and custom devices to prevent this problem.

  2. Wrong action happen:

    • You used the Port before. To solve the problem delete old devices and define a custom port for the device if it's a notification or custom device.
  3. Need help for the configuration:

  4. Don't know how to interact with other modules:

    • Here could help the wiki page with example configurations for other modules.
  5. If you have still problems create an issue.