cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

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

pschmaltz
10-Marble

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

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?

1 ACCEPTED SOLUTION

Accepted Solutions

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);

View solution in original post

14 REPLIES 14
PaiChung
22-Sapphire I
(To:pschmaltz)

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.

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);

stanislav
6-Contributor
(To:pschmaltz)

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?

PaiChung
22-Sapphire I
(To:stanislav)

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.

stanislav
6-Contributor
(To:PaiChung)

Thanks for the solution Pai. This seems to be what I am looking for.

But how do I create a Custom Alert and where should I find the Alert section?

PaiChung
22-Sapphire I
(To:stanislav)

Our built-in Alerts are basically an extension to Event/Subscription logic. So you can't create 'custom' Alerts, but you can do an Event/Subscription to achieve the same effect.

What you can do and was already talked about, is set up Alerts dynamically at runtime. You will need something like a pre-created AnyAlert subscription to handle what you want to do when the Alert fires.

stanislav
6-Contributor
(To:PaiChung)

I do not want to handle anything, I want to fire an Alert from a subscription/service and be able to add register a new alert in the "Alert Summary" section.

The TW alert system is pretty basic for our needs. We do not have simple properties like temperature and pressure, we have complex datapoints that needs to be analyzed(with algorithm) in order to detect an alert.  The system does not provide such mechanism therefore I want do this an service and then record an alert in the system.

PaiChung
22-Sapphire I
(To:stanislav)

A Thingworx Alert is a system generated and monitored state and can't just be 'fired'.

If you want, you can create event/subscription pairs that add an entry into the AlertSummaryStream.

What is your use case? Why would you need to use the AlertSummary vs. entering Event (aka Alert) information into a Stream of your choice?

stanislav
6-Contributor
(To:PaiChung)

We want to use the default TW Alert mechanism (monitor, acknowledge etc) but trigger the alerts on demand from a custom service.

The TW system has a big drawback because the Alert "conditions" are pretty basic. In a real life scenario you might have to compare multiple properties in the same time in order to detect an alert.

I would do that, but there is no such stream. The active alerts(aka Summary) seems to be persisted on the thing. Just the history is persisted in the stream (AlertHistoryStream).


PaiChung
22-Sapphire I
(To:stanislav)

I totally agree that complex alerts are something that doesn't come OOTB in the Platform. This is exactly why being able to do custom Event/Subscriptions is so powerful.

I recommend you create an additional stream with a DataShape that fits your use case.

And if you like the way we display basic alerts in our Summary, you can just duplicate those mashups.

dcbb
1-Newbie
(To:pschmaltz)

But I want display the pop up alert, can u tell me

PaiChung
22-Sapphire I
(To:dcbb)

Pop Up alert is something you'll have to create in a Mashup. So you will need a service to check for the condition and then pop that up.

dcbb
1-Newbie
(To:PaiChung)

i created the service and i was took gauge for temperature value.if temp<40 it will show the alert "High", I wrote the condition like this

var temp=temp;

if(this.temp<30)

{

    this.temp=true;

    result="Ok";

}

else{

this.temp=false;

result = "Temperature very High";

}

In i created the service and i was took gauge for temperature value.how to show in mashup alert..........

PaiChung
22-Sapphire I
(To:dcbb)

My first thought is that State Based formatting is very useful for this.

You can have that cut off show on your gauge and show values in different colors depending on the value.

Besides that, I would probably take that value of the service into a Validator widget and then into a Status Message widget or Pop-Up Mashup using a Navigation Widget

Top Tags