Skip to content

MMRIZE/MMM-NotificationTrigger

Repository files navigation

MMM-NotificationTrigger

MMM-NotificationTrigger is a simple notification relay for MagicMirror² which can convert notifications from TRIGGER_NOTIFICATION to FIRE_NOTIFICATION.

symbolization

Many MagicMirror² modules have their own incoming and outgoing notification messages. But most of them are not compatible. This module can translate notifications among other modules.

You can use this module to chain modules to work together.

Screenshot

This module works in background, so there is no screenshot.

Installation

To install this module, clone the repository into your ~/MagicMirror/modules directory:

cd ~/MagicMirror/modules
git clone https://github.com/MMRIZE/MMM-NotificationTrigger

Update

To update this module, navigate to the module directory and pull the latest changes:

cd ~/MagicMirror/modules/MMM-NotificationTrigger
git pull

Configuration

Configuration Sample

    {
      module: 'MMM-NotificationTrigger',
      //This module works in Background, so you don't need to describe `position`.
      config: {
        useWebhook: false, // If you want to activate webhook as Notification emitter, set true. (eg. IFTTT)
        triggers:[ // Array of triggers.
          {
            trigger: "INCOMING_NOTIFICATION", //REQUIRED
            triggerSenderFilter: (sender) => { //OPTIONAL should return true or false
              if (sender == "....") {
                return true
              }
              return false
            },
            triggerPayloadFilter: (payload) => { //OPTIONAL should return true or false
              if (typeof payload.value !== 'undefined' && payload.value > 0) {
                return true
              }
              return false
            },
            fires: [ // Array of fires. You can enable multi-firing with one trigger.
              {
                fire:"OUTGOING_NOTIFICATION", //REQUIRED
                payload: (payload) => { //OPTIONAL. transform received payload to what your target module wants.
                  return payload
                },
                delay: 1000, //OPTIONAL, if this is set, your outgoing notification will be fired after delay.
                exec: "ls -l" //OPTIONAL, if exists, this script will be executed, and the result will be returned with "OUTGOING_NOTIFICATION_RESULT" and payload. Can also be a function: (payload) => "command " + payload.option
              },
            ],
          },
        ]
      },
    },

Sample for relaying a notification to the alert module with payload transformation.

    {
      module: "MMM-NotificationTrigger",
      config: {
        triggers:[
          {
            trigger: "CALENDAR_EVENTS",
            triggerPayloadFilter: (payload) => {
              return Array.isArray(payload) && payload.length > 0
            },
            fires: [
              {
                fire:"SHOW_ALERT",
                payload: (payload) => {
                  return {
                    type: "notification",
                    title: "Calendar",
                    message: `You have ${payload.length} upcoming event(s)`
                  }
                },
              },
            ],
          },
        ]
      }
    },

sample for MMM-Motion-Detection. This smaple just relay notification to ALERT module to display message, but you can modify for your purpose.

    {
      module: "MMM-NotificationTrigger",
      config: {
        useWebhook:true,
        triggers:[
          {
            trigger: "motion-detected",
            fires: [
              {
                fire:"SHOW_ALERT",
                payload: {
                  type:"notification",
                  title:"motion detector",
                  message: "motion detected"
                },
              }
            ]
          },
          {
            trigger: "motion-stopped",
            fires: [
              {
                fire:"SHOW_ALERT",
                payload: {
                  type:"notification",
                  title:"motion detector",
                  message: "motion stopped"
                }
              }
            ]
          },
        ]
      }
    },

useWebhook

You can use this module as endpoint of webhook for IFTTT. Set your IFTTT recipe like this. (as Make a web request part)

  • URL: your mirror static IP or domain/webhook
  • Method: post or get
  • Content-Type: anything or application/json
  • Body:
{
  "sender": {
    "name":"..."
  },
  "notification": "...",
  "payload":{
     ...
  }
 }

Configuration Example for IFTTT relaying to ALERT module.

//in your configuration
    {
          module: "MMM-NotificationTrigger",
          config: {
            useWebhook:true,
            triggers:[
              {
                trigger: "IFTTT_COMMAND",
                fires: [
                  {
                    fire:"SHOW_ALERT",
                    payload: function(payload) {
                      return payload
                    },
                  },
                ],
              },
      ]
          }
    }

In your IFTTT Applet setting Body

  {
    "sender": {
      "name": "IFTTT"
    },
    "notification": "IFTTT_COMMAND",
    "payload": {
      "title": "From IFTTT",
      "message": "This message is comming from IFTTT",
      "timer": 5000
    }
  }

exec Example

  {
    module: "MMM-NotificationTrigger",
    config: {
      triggers:[
        {
          trigger: "DOM_OBJECTS_CREATED",
          fires: [
            {
              fire:"MY_COMMAND",
              exec: "sleep 5; ls -l"
            },
          ],
        },
      ]
    }
  },

When trigger is emitted, MY_COMMAND notification will be fired. then, MY_COMMAND_RESULT notification will be fired with the payload which contains result of command.

You can also use a function as exec like this:

exec: (payload) => {
  return "/somewhere/my/script.sh " + payload.option;
}

Note: The payload, exec, triggerSenderFilter and triggerPayloadFilter properties support functions. These functions are evaluated in the browser context and work when MagicMirror loads the config as a JavaScript file (the default). If your config is loaded through a different mechanism that serializes it to JSON, functions will be lost.

Changelog

All notable changes to this project will be documented in the CHANGELOG.md file.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

ko-fi

About

MMM-NotificationTrigger is a simple notification relay.

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

Packages

 
 
 

Contributors