One time rule with arbitrary input arguments

You might often need a rule that just gets executed once, based on the input arguments that are provided within a request. For instance, connected appliances might send towards a waylay the ERROR code together with an asset identity that might need different processing in the backend (sending emails, creating work orders or alarms). This can be achieved in two different ways: one is using onetime tasks (which means that after the execution, tasks are kept and persisted in waylay), while the other approach is to run rules against templates using batch API, in which case there will be no tasks created in the backend. The second approach is better if you will use this approach for large amount of data, like in the previous example, where you would collect error codes of many machines which you all want to process at once.

Approach using one time task:

In order to illustrate this use case, we created a simple template that sends different email subject depending on the payload provided in the HTTP request:

image

Here is the template, written in simplified JSON format and please note that the actuator is using {{JSON_1.foo}} as a subject line.

{
  "sensors": [
    {
      "label": "JSON_1",
      "name": "JSON",
      "version": "0.0.7",
      "dataTrigger": false,
      "tickTrigger": true,
      "position": [
        851,
        263
      ],
      "sequence": 1,
      "properties": {
        "message": "{ \"foo\" : \"bar\"}"
      }
    }
  ],
  "actuators": [
    {
      "label": "templateMail_1",
      "name": "templateMail",
      "version": "0.0.9",
      "position": [
        1021,
        201
      ],
      "properties": {
        "subject": "{{JSON_1.foo}}",
        "reply": "",
        "to": "veselin@waylay.io",
        "pdfURL": "",
        "from": "veselin@waylay.io",
        "message": "hello world",
        "send_at": ""
      }
    }
  ],
  "relations": [],
  "triggers": [
    {
      "destinationLabel": "templateMail_1",
      "sourceLabel": "JSON_1",
      "invocationPolicy": 0,
      "statesTrigger": [
        "done"
      ]
    }
  ],
  "name": "JSON_email"
}

If we just want to test this template in a debugger, the email subject will always be bar. If we start a one time task from this template and want to change the subject of this email to for instance bar1, this would be the REST call:

 curl --user apiKey:apiSecret -H "Content-Type:application/json" -X POST \n
    -d '{
    "name": "test email",
    "template": "JSON_email",
    "start": true,
    "type": "onetime",
    "nodes": [{
      "name": "JSON_1",
      "properties": {
        "sensor": {
          "name": "JSON",
          "version": "0.0.7",
          "label": "JSON_1",
          "requiredProperties": [ { "message": "{ \"foo\" : \"bar1\"  } " }]
        }
      }
    }]}' https://sandbox.waylay.io/api/tasks

Approach using Bulk request

First we need slightly to change the existing template, and add a flag execute on data to the JSON_1 node. Reason is that Bulk request template run feature is originally designed to process bulk logs, where different nodes might consume different collected data. Otherwise, node JSON_1 would not be called.

image

which in the template will be presented this way:

 "sensors": [
    {
      "label": "JSON_1",
      "name": "JSON",
      "version": "0.0.7",
      "dataTrigger": true,
      "tickTrigger": true, ...
    }
  ]

Now we can execute the following command:

curl --user apiKey:apiSecret -H "Content-Type:application/json" -X POST -d '{
      "data": [[{"resource": "resource1"}]],
      "conf": {
        "resource": "resource1",
        "resetObservations": false,
        "executeActuators": true,
         "nodes":[
          {
            "name": "JSON_1",
            "properties": {
              "resource":  "resource1",
              "sensor" : {
                "requiredProperties": [ { "message": "{ \"foo\" : \"bar2\"  } " }]
            }}
          }]
      }
    }' "https://sandbox.waylay.io/api/templates/JSON_email/run"