# Advanced Rules Mode

### Rules in Advanced Mode

In advanced rules, you can write conditions and actions in javascript language.

In this javascript code, it is possible to call some defined functions to execute the same actions that you can do in easy mode. On the other hand, we also have some help functions. We will explain them below.


The main function will receive a flattened entity representation, adding the _\_received_ and _\_previous_ values to each datastream. The _\_received_ field is a simple object in provision datastreams and an array object in any other case.

We will use the following entity as an example:

```javascript
entity = {
  "provision.administration.channel": {
    "_value": {
      "_current": {
        "value": "battery_channel",
        "date": "2017-12-01T08:52:37.563Z",
        "at": "2017-12-01T08:52:37.563Z"
      },
      "_previous": {
        "value": "battery_channel",
        "date": "2017-12-01T08:52:37.563Z",
        "at": "2017-12-01T08:52:37.563Z"
      }
    }
  },
  "provision.administration.identifier": {
    "_value": {
      "_current": {
        "value": "device_battery_id",
        "provType": "MONITORING",
        "date": "2017-12-01T08:52:37.57Z",
        "at": "2017-12-01T08:52:37.57Z"
      },
      "_previous": {
        "value": "battery_organization",
        "provType": "MONITORING",
        "date": "2017-12-01T08:52:37.566Z",
        "at": "2017-12-01T08:52:37.566Z"
      }
    }
  },
  "provision.administration.organization": {
    "_value": {
      "_current": {
        "value": "battery_organization",
        "provType": "MONITORING",
        "date": "2017-12-01T08:52:37.566Z",
        "at": "2017-12-01T08:52:37.566Z"
      },
      "_previous": {
        "value": "battery_organization",
        "provType": "MONITORING",
        "date": "2017-12-01T08:52:37.566Z",
        "at": "2017-12-01T08:52:37.566Z"
      }
    }
  },
  "provision.administration.plan": {
    "_value": {
      "_current": {
        "value": "FLOW_RATE_100",
        "provType": "MONITORING",
        "date": "2017-12-01T08:52:37.565Z",
        "at": "2017-12-01T08:52:37.565Z"
      },
      "_previous": {
        "value": "FLOW_RATE_100",
        "provType": "MONITORING",
        "date": "2017-12-01T08:52:37.565Z",
        "at": "2017-12-01T08:52:37.565Z"
      }
    }
  },
  "provision.administration.serviceGroup": {
    "_value": {
      "_current": {
        "value": "emptyServiceGroup",
        "provType": "MONITORING",
        "date": "2017-12-01T08:52:37.561Z",
        "at": "2017-12-01T08:52:37.561Z"
      },
      "_previous": {
        "value": "emptyServiceGroup",
        "provType": "MONITORING",
        "date": "2017-12-01T08:52:37.561Z",
        "at": "2017-12-01T08:52:37.561Z"
      }
    }
  },
  "provision.device.communicationModules[].identifier": [
    {
      "_index": {
        "path": "provision.device.communicationModules[].identifier",
        "value": {
          "_current": {
            "value": "commsMod_battery_id",
            "provType": "MONITORING",
            "date": "2017-12-01T08:52:37.577Z",
            "at": "2017-12-01T08:52:37.577Z"
          }
        }
      },
      "_value": {
        "_current": {
          "value": "commsMod_battery_id",
          "provType": "MONITORING",
          "date": "2017-12-01T08:52:37.577Z",
          "at": "2017-12-01T08:52:37.577Z"
        },
        "_previous": {
          "value": "commsMod_battery_id",
          "provType": "MONITORING",
          "date": "2017-12-01T08:52:37.577Z",
          "at": "2017-12-01T08:52:37.577Z"
        }
      }
    }
  ],
  "provision.device.communicationModules[].subscription.address": [
    {
      "_index": {
        "path": "provision.device.communicationModules[].identifier",
        "value": {
          "_current": {
            "value": "commsMod_battery_id",
            "provType": "MONITORING",
            "date": "2017-12-01T08:52:37.577Z",
            "at": "2017-12-01T08:52:37.577Z"
          }
        }
      },
      "_value": {
        "_current": {
          "value": {
            "type": "IPV4",
            "value": "99.1.1.71",
            "apn": "myapnprov.es"
          },
          "provType": "REFERENCE",
          "date": "2017-12-01T08:52:37.624Z",
          "at": "2017-12-01T08:52:37.624Z"
        },
        "_previous": {
          "value": {
            "type": "IPV4",
            "value": "99.1.1.71",
            "apn": "myapnprov.es"
          },
          "provType": "REFERENCE",
          "date": "2017-12-01T08:52:37.624Z",
          "at": "2017-12-01T08:52:37.624Z"
        }
      }
    }
  ],
  "provision.device.communicationModules[].subscription.identifier": [
    {
      "_index": {
        "path": "provision.device.communicationModules[].identifier",
        "value": {
          "_current": {
            "value": "commsMod_battery_id",
            "provType": "MONITORING",
            "date": "2017-12-01T08:52:37.577Z",
            "at": "2017-12-01T08:52:37.577Z"
          }
        }
      },
      "_value": {
        "_current": {
          "value": "subscription_battery_id",
          "provType": "MONITORING",
          "date": "2017-12-01T08:52:37.626Z",
          "at": "2017-12-01T08:52:37.626Z"
        },
        "_previous": {
          "value": "subscription_battery_id",
          "provType": "MONITORING",
          "date": "2017-12-01T08:52:37.626Z",
          "at": "2017-12-01T08:52:37.626Z"
        }
      }
    }
  ],
  "provision.device.identifier": {
    "_value": {
      "_current": {
        "value": "device_battery_id",
        "provType": "MONITORING",
        "date": "2017-12-01T08:52:37.636Z",
        "at": "2017-12-01T08:52:37.636Z"
      },
      "_previous": {
        "value": "device_battery_id",
        "provType": "MONITORING",
        "date": "2017-12-01T08:52:37.636Z",
        "at": "2017-12-01T08:52:37.636Z"
      }
    }
  },
  "provision.device.location": {
    "_value": {
      "_current": {
        "value": {
          "position": {
            "type": "Point",
            "coordinates": [
              -3.7028,
              40.41675
            ]
          },
          "postal": "28013"
        },
        "provType": "MONITORING",
        "date": "2017-12-01T08:52:37.64Z",
        "at": "2017-12-01T08:52:37.64Z"
      },
      "_previous": {
        "value": {
          "position": {
            "type": "Point",
            "coordinates": [
              -3.7028,
              40.41675
            ]
          },
          "postal": "28013"
        },
        "provType": "MONITORING",
        "date": "2017-12-01T08:52:37.64Z",
        "at": "2017-12-01T08:52:37.64Z"
      }
    }
  },
  "resourceType": {
    "_value": {
      "_current": {
        "value": "entity.device",
        "provType": "IDENTIFIER",
        "date": "2017-12-01T08:52:37.643Z",
        "at": "2017-12-01T08:52:37.643Z"
      },
      "_previous": {
        "value": "entity.device",
        "provType": "IDENTIFIER",
        "date": "2017-12-01T08:52:37.643Z",
        "at": "2017-12-01T08:52:37.643Z"
      }
    }
  },
  "device.communicationModules[].identifier": [
    {
      "_index": {
        "path": "device.communicationModules[].identifier",
        "value": {
          "_current": {
            "value": "commsMod_battery_id",
            "date": "2017-12-01T08:52:37.577Z",
            "at": "2017-12-01T08:52:37.577Z"
          }
        }
      },
      "_value": {
        "_current": {
          "value": "commsMod_battery_id",
          "date": "2017-12-01T08:52:37.577Z",
          "at": "2017-12-01T08:52:37.577Z"
        },
        "_previous": {
          "value": "commsMod_battery_id",
          "date": "2017-12-01T08:52:37.577Z",
          "at": "2017-12-01T08:52:37.577Z"
        }
      }
    }
  ],
  "device.communicationModules[].subscription.address": [
    {
      "_index": {
        "path": "device.communicationModules[].identifier",
        "value": {
          "_current": {
            "value": "commsMod_battery_id",
            "date": "2017-12-01T08:52:37.577Z",
            "at": "2017-12-01T08:52:37.577Z"
          }
        }
      },
      "_value": {
        "_current": {
          "value": {
            "type": "IPV4",
            "value": "99.1.1.71",
            "apn": "myapn.es"
          },
          "date": "2017-12-01T08:52:37.624Z",
          "at": "2017-12-01T08:52:37.624Z",
          "source": "DEVICE_OPENGATE_HTTP",
          "sourceInfo": "IoT Data Message Received"
        },
        "_previous": {
          "value": {
            "type": "IPV4",
            "value": "99.1.1.71",
            "apn": "myapn.es"
          },
          "date": "2017-12-01T08:52:37.624Z",
          "at": "2017-12-01T08:52:37.624Z",
          "source": "DEVICE_OPENGATE_HTTP",
          "sourceInfo": "IoT Data Message Received"
        }
      }
    }
  ],
  "device.communicationModules[].subscription.identifier": [
    {
      "_index": {
        "path": "device.communicationModules[].identifier",
        "value": {
          "_current": {
            "value": "commsMod_battery_id",
            "date": "2017-12-01T08:52:37.577Z",
            "at": "2017-12-01T08:52:37.577Z"
          }
        }
      },
      "_value": {
        "_current": {
          "value": "subscription_battery_id",
          "date": "2017-12-01T08:52:37.626Z",
          "at": "2017-12-01T08:52:37.626Z",
          "source": "DEVICE_OPENGATE_HTTP",
          "sourceInfo": "IoT Data Message Received"
        },
        "_previous": {
          "value": "subscription_battery_id",
          "date": "2017-12-01T08:52:37.626Z",
          "at": "2017-12-01T08:52:37.626Z",
          "source": "DEVICE_OPENGATE_HTTP",
          "sourceInfo": "IoT Data Message Received"
        }
      }
    }
  ],
  "device.identifier": {
    "_value": {
      "_received": [{
        "value": "device_battery_id",
        "date": "2017-12-01T08:52:37.636Z",
        "at": "2017-12-01T08:52:37.636Z",
        "source": "DEVICE_OPENGATE_HTTP",
        "sourceInfo": "IoT Data Message Received"
      }],
      "_current": {
        "value": "device_battery_id",
        "date": "2017-12-01T08:52:37.636Z",
        "at": "2017-12-01T08:52:37.636Z",
        "source": "DEVICE_OPENGATE_HTTP",
        "sourceInfo": "IoT Data Message Received"
      },
      "_previous": {
        "value": "device_battery_id",
        "date": "2017-12-01T08:52:37.636Z",
        "at": "2017-12-01T08:52:37.636Z",
        "source": "DEVICE_OPENGATE_HTTP",
        "sourceInfo": "IoT Data Message Received"
      }
    }
  },
  "device.temperature.value": {
    "_value": {
      "_received": [{
        "value": 25.3,
        "date": "2017-12-01T08:52:37.64Z",
        "at": "2017-12-01T08:52:37.64Z",
        "source": "DEVICE_OPENGATE_HTTP",
        "sourceInfo": "IoT Data Message Received"
      }],
      "_current": {
        "value": 25.3,
        "date": "2017-12-01T08:52:37.64Z",
        "at": "2017-12-01T08:52:37.64Z",
        "source": "DEVICE_OPENGATE_HTTP",
        "sourceInfo": "IoT Data Message Received"
      },
      "_previous": {
        "value": 23.3,
        "date": "2017-12-01T05:52:37.64Z",
        "at": "2017-12-01T05:52:37.64Z",
        "source": "DEVICE_OPENGATE_HTTP",
        "sourceInfo": "IoT Data Message Received"
      }
    }
  }
}
  
```
