Skip to main content
5-Regular Member
March 18, 2016
Solved

Generating an Alert from a subscription/service - getting 'unable to publish local event'

  • March 18, 2016
  • 14 replies
  • 9747 views

Rather than generating an alert based on a threshold value I set via the 'Managed Alerts' UI of Composer I want to generate an alert from a subscription or service that I run. I found what appears to be the right service in the snippets, e.g. I have a service that uses the Alerts() service like this:


var params = {

alertType: "EqualTo" /* STRING */,

sourceProperty: "myProperty" /* STRING */,

name: "MyAlert" /* STRING */,

description: "description for MyAlert" /* STRING */,

priority: 3 /* INTEGER */,

message: "Message for MyAlert" /* STRING */

};

me.Alert(params);

I have also defined 'MyAlert' via the Managed Alerts' UI of the Composer for 'myProperty'. But when I run the above service it generates no alert. I get a pair of Warning that say 'Unable to publish local event in testThing : Event Alert not found' and 'Unable to publish event in testThing : Event Alert not found'.

How does one use the Alert() service to create a new alert on a property as part of service or subscription javascript?

Best answer by pschmaltz

My colleague Ian shared an update on this topic with me recently I thought I would share here as well. It turns out that while me.Alert(params) doesn't work, there is a service me.AddOrUpdateAlert(params) that allows you to generate an alert for a property as part of a service or subscription. Better than that, you don't even have to predefine that alert on the property's alert manager page.

Note - the distinction to be made here is between creating a new alert instance (i.e. fire an alert) vs. defining some new alert. It would seem that me.Alert() is just about defining an alert, whereas AddOrUpdateAlert() generates an alert instance (whether it was predefined or not), which is the situation I wanted to address in the original posting here.

An example Ian shared is below for others' reference. The attributes data shape for this is perhaps the tricky detail that may not be obvious (green). I've not had a chance to try it myself but hope it is helpful to folks!

---------------------------

var params = {

      infoTableName : "InfoTable",

      dataShapeName : "IntegerCompareAlert"

};

// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(BooleanMatchAlert)

var AttIT = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

// BooleanMatchAlert entry object

var newEntry = new Object();

  newEntry.limit = 100;

  newEntry.limitInclusive=true;

  AttIT.AddRow(newEntry);

var params = {

      alertType: "Above" /* STRING */,

      alertName: "DutyHours_ABOVE_ALERT" /* STRING */,

      property: "DutyHours" /* STRING */,

      description: "DutyHours property above threshold!" /* STRING */,

      attributes: AttIT /* INFOTABLE */,

      priority: 1 /* INTEGER */,

      persistent: true /* BOOLEAN */,

      enabled: true /* BOOLEAN */

};

    me.AddOrUpdateAlert(params);

​

14 replies

22-Sapphire I
March 21, 2016

Hi Peter, I think that unfortunately is a bug, probably a method that shouldn't be exposed.

Best next thing, create your own custom event and fire that one.

Very easy to do, only requires a DataShape, and if you want it to function like the built-in Alerting, use the same DataShape and then log your information to the AlertHistoryStream. Although I recommend a dedicated stream for your own events.

The built-in Alerts that most likely the Alert event ties into, has some system way of firing and tracking In/Out of Alert state (based on Property values and the Alert definitions) so I think that is why you can't fire it manually.

pschmaltz5-Regular MemberAuthorAnswer
5-Regular Member
April 28, 2016

My colleague Ian shared an update on this topic with me recently I thought I would share here as well. It turns out that while me.Alert(params) doesn't work, there is a service me.AddOrUpdateAlert(params) that allows you to generate an alert for a property as part of a service or subscription. Better than that, you don't even have to predefine that alert on the property's alert manager page.

Note - the distinction to be made here is between creating a new alert instance (i.e. fire an alert) vs. defining some new alert. It would seem that me.Alert() is just about defining an alert, whereas AddOrUpdateAlert() generates an alert instance (whether it was predefined or not), which is the situation I wanted to address in the original posting here.

An example Ian shared is below for others' reference. The attributes data shape for this is perhaps the tricky detail that may not be obvious (green). I've not had a chance to try it myself but hope it is helpful to folks!

---------------------------

var params = {

      infoTableName : "InfoTable",

      dataShapeName : "IntegerCompareAlert"

};

// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(BooleanMatchAlert)

var AttIT = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

// BooleanMatchAlert entry object

var newEntry = new Object();

  newEntry.limit = 100;

  newEntry.limitInclusive=true;

  AttIT.AddRow(newEntry);

var params = {

      alertType: "Above" /* STRING */,

      alertName: "DutyHours_ABOVE_ALERT" /* STRING */,

      property: "DutyHours" /* STRING */,

      description: "DutyHours property above threshold!" /* STRING */,

      attributes: AttIT /* INFOTABLE */,

      priority: 1 /* INTEGER */,

      persistent: true /* BOOLEAN */,

      enabled: true /* BOOLEAN */

};

    me.AddOrUpdateAlert(params);

​

1-Visitor
April 29, 2016

Hi Peter,

The script you have posted defines an alert. It does not fire the alert.

I am interested in firing an alert programmatically from a service without the need of defining an alert.  

Does anybody know how to use the Alert() service?

22-Sapphire I
April 30, 2016

You can't necessarily fire all the Alerts available, some of these are done by the system.

However you can create your own Custom Alerts with a DataShape in the Alerts section.

Once you have that defined, you can fire it from either a Subscription or Service.

Just start a script and then in the code helper section, click on me or browse to an entity, go tot he Alerts section and double click to see the snippet required to fire the alert.

Since it needs a DataShape, you'll be defining an object with values to pass with the Event, that becomes accessible in the Subscription.