MagicMirror² v2.14.0 is available! For more information about this release, check out this topic.

Python in to the magic mirror.

  • Hello,
    I heave a raspberry running with python and a magic mirror modules.
    In the python project I heave a string variable that I want to display in a line of the mirror.
    I m a little bit of noob with all this and im learning bit by bit.

    Can anyone tell me how to do this. what I need to program in the python code and what I need to use for module.

    Please let me know.

    0_1548242086835_Schermafbeelding 2019-01-23 om 12.12.30.png

  • Module Developer

    If possible, Instead image, post code itself.
    Anyway, You’d better to read docs more carefully.


    /// MMM-P2000.js
    Module.register("MMM-P2000", {
      getDom: function() {
        var e = document.createElement("div") = "DISPLAY"
        return e
      notificationReceived: function(notification, payload, sender) {
        switch(notification) {
          case "DOM_OBJECTS_CREATED":
            var timer = setInterval(()=>{
            }, 1000)
      socketNotificationReceived: function(notification, payload) {
        switch(notification) {
          case "HERE_IS_DATA":
            var e = document.getElementById("DISPLAY")
            e.innerHTML = payload


    /// node_helper.js
    const spawn = require("child_process").spawn
    var NodeHelper = require("node_helper")
    module.exports = NodeHelper.create({
      socketNotificationReceived: function(notification, payload) {
        switch(notification) {
          case "GIVE_ME_DATA":
      job: function() {
        var process = spawn("python", ["/home/pi/src/"])
        process.stdout.on("data", (data)=>{
          this.sendSocketNotification("HERE_IS_DATA", data)

    I didn’t test. I wrote this code with only my brain. so there could be errors, but you can catch the concept.

  • Module Developer

  • @Sean
    Hello Sean,
    Thank you very much for your replay. But can you be more specific over the module I need to use?
    And how to fit in the variable in the magic mirror Module

  • Module Developer

    Your question contains two independent issues.

    1. First you have to make your own module to display SOMETHING. That is not so difficult if you have some basic knowledge of Javascript, but not so simple to describe all of module development here. You can learn how to develop your module with these docs;
    1. in node_helper.js of your custom module; you can do that like this;
    var spawn = require("child_process").spawn;
    var process = spawn('python',[""]);
    process.stdout.on('data', function(stdOutput_from_py){
      // do your job with stdOutput from your python program.
      // Usually, you will send this value to module with `.sendSocektNotification()`

    You can catch the value with .socketNotificationReceived() in your module.

  • @sean
    Again Thank you very much for this information. Im getting there with the code and learning bij Trail and Error 😉
    I have made my own working module but still had some Questions.

    1. The .py project is in the map src/ How do I fill in that it is there? Ore is this enough ?
      var process = spawn(‘python’,[“”]);

    2. Do I need to run the Python project aswell ? or is then node_helper.js running it ?

    3. I made this in the module but is looks like it is not working… 😞
      0_1548256068689_Schermafbeelding 2019-01-23 om 16.07.16.png

    4. I add this to my code to test. it is seems to work. Can you please check if this stdout is ok?
      0_1548256130223_Schermafbeelding 2019-01-23 om 15.58.31.png

  • Module Developer


    1. Try absolute path of your target py script.

    2. When your child_process.spawn is called, python script will be executed.

    3. Well, node_helper.js is also needed.
      3-1. Add your module into your config.js either.

    4. When your python script is executed on the shell, normal stdout will be printed out on the shell.

  • @sean
    1.Try absolute path of your target py script.
    ( Oke I add the Path )

    2.When your child_process.spawn is called, python script will be executed.
    ( oke that’s Clear.)

    3 Well, node_helper.js is also needed. (* the codeYou send me I add in the node_helper.js.
    3-1. Add your module into your config.js either. ( Yes I did. The example you send me was working. )
    I can print something on the magic mirror on my own build Module.

    4 When your python script is executed on the shell, normal stdout will be printed out on the shell.
    ( Yes this is working wenn I executed on the shell it will print what I give in so that site wil look fine.

    So thank you for the answers. Im almost sure the Python project is oke. because it wil “print” thrue the sys.stdout.write function.
    This is the Node_helper.js . I think this is fine aswell.
    0_1548258168526_Schermafbeelding 2019-01-23 om 16.40.56.png
    This is the MMM_. Module. im not sure I did this one oke.
    0_1548258277282_Schermafbeelding 2019-01-23 om 16.43.55.png

    And ofcourse I add the config. file.

    0_1548258338888_Schermafbeelding 2019-01-23 om 16.45.29.png

    Can you Please have a look what is going wrong because my magic mirror is now black and I can only restart bij ctrl alt del. so please help.

  • Module Developer

    Your node_helper.js is not correct.

    See other’s module as reference.

    Or you can get a hint of executing python script in node_helper.js
    (I’m using PythonShell instead child_process in that module.)

  • @paulvanderheijden90 start the mirror in developer mode

    npm start dev

    and look at the console tab for errors (red text)

    now in your node_helper spawn data.on() routine, you need to add a sendSocketNotification(:I_DID", some_data) to get the info to the Module.js socketNotificationReceived() method so you can do something to display it (call updateDom(), which will schedule a call to getDom()

  • @sean said in Python in to the magic mirror.:

    Your node_helper.js is not correct.

    Hello Sean,
    Why is it wrong? I copy it from your post earlier.
    I started in Dev mode. I noticed that wenn I add the Node_helper.js module in the MMM-P2000 map the Magic mirror crashes and give only black screen.
    I studyed the Node_helper file. I see in the first line you define the function and in the second line you define the settings for that function. So that’s clear to me. In the next lines you define the stdout of the process and copy it in de Console Log. so that’s also clear for me. So I down’t know why is it not working and crashes.

    I made in the same map ( home/pi/src/ ) a file called Here I defined a string and I write the string to the stdout so as you say before the python project don’t need a separate call. So that should work as well.

    0_1548328532225_Schermafbeelding 2019-01-24 om 12.08.18.png
    Python project:
    0_1548328572185_Schermafbeelding 2019-01-24 om 12.10.09.png

    I also getting in to the module setup so I changed the MMM-P2000.js and I think it is oke like this.
    First the start function restart every 1000mS the dom function.
    then the getdom Function is now showing the test “Hello World” in the module
    so that should be oke aswell. I

    How do I need to link the socked notification message to the Module string?
    0_1548329124803_Schermafbeelding 2019-01-24 om 12.25.08.png

Log in to reply