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

Timer based on python script

  • Hello,
    I’ve just got my MM up and running but sadly struggle with making a timer showing how long until screen goes off.

    My MM has python script that controls when to turn on/off screen based on some sensors and time. It starts internal timer 60s when there is nobody in front of the mirror, turn screen off when timer goes off and resets it when it registers movement.

    Although I’m terrible with JS I have somehow managed to create new module, that simply show remaining time.

    I’d love to ask for some tips how to get get remaining time from python script to MM module. So far my only idea was to write the number into a file and let the module read it, but I’m sure there must be better way as this just sounds terrible considering I’d like to update it at least once a second.

  • Module Developer

    I’m not with my pc at this moment, so I cannot
    give you the details, but google these;

    • nodejs child-process exec and spawn

    Your python script might spit stdout as a result of running. exec or spawn can control runnig and termination of child-process(e.g your script) and get stdout or stdere from it. You can make to display those in your mm module.

  • Thank for reply, but you can’t used it for already running script, can you? What I understood is, that is runs new py script.

    Although it would probably work, I was looking for something that can listen/request data from already running script.

    I was hoping I can call some function MagicFunction(localhost, port?, data) from python… and my module could have something like listen(localhost,port?); .onReceive(){ data = fromPython; }

    I hope it makes sense.

    ps: I understand there are better and simpler solutions how to turn on/off monitor, but this is supposed to be only beginning of what I’m trying to accomplish

    EDIT: I just discovered there is “Socket.IO”. It sounds like exactly what I’m looking for. But honestly the more I read about it the more I’m confused how would I used it in my module.

  • Module Developer

    Hmmm. I think controlling external script in nodeJs with child-process is better, but choice is yours. 😃
    As you said, via HTTP could be one solution.

    • Your Python(Python 3) sender sript :
    import urllib.request
    contents = urllib.request.urlopen("http://localhost:8080/GATEWAY?something").read()
    • MM module
      • your module XXXX.js
    socketNotificationReceived: function(noti, payload) {
      if (noti == "RECEIVED_DATA") {
    • it’s node_helper.js
    const NodeHelper = require("node_helper")
    const bodyParser = require("body-parser")
    module.exports = NodeHelper.create({
      start: function() {
        this.expressApp.use(bodyParser.urlencoded({extended: true}))
        this.expressApp.get("/GATEWAY", (req, res) => {
          this.sendSocketNotification("RECEIVED_DATA", req.body)
          res.status(200).send({status: 200})

    If you think handling node_helper.js is somehow difficult, consider MMM-NotificationTrigger. It has already a feature to receive from HTTP request then relay to other module via notification.

  • This is perfect!

    I need few hours on google now, to at least begin to comprehend what you just wrote…

    Thank you!! 🙂 I’ll post when I get it somehow work (or get stuck 😃 )

Log in to reply