Query Message and Trigger an Action

#1

Hi,

I am getting from an API Integration a long message payload:

{"customerId":"xxx","customerName":"xxx","username":"xxx","userId":"xxx","settings":{"type":"ALARM_ONLY","version":"CUSTOM","screenSaverVersion":"NO_SCREEN_SAVER","recipientsType":"ALL","participantSortOrder":"NN_VN_COMMENT","mapType":"HYBRID","screenSaverTimeout":120,"defaultMapZoomFactor":13,"defaultMapLongitude":null,"defaultMapLatitude":null,"showFunctions":true,"showMultipleAlarms":false,"customLayout":"{\"layout\":\"SINGLE\",\"tiles\":[{\"urlExtra\":\"?Bereich=all\",\"provider\":\"zamg\",\"province\":\"\",\"hideTitle\":true,\"showFunctions\":false,\"mapType\":\"terrestris\",\"showOfm\":false,\"showRoute\":false,\"recipientsType\":\"ALL\",\"zoomLevel\":17,\"maxWasserkarteItems\":50,\"type\":\"last-alarm-text\"}],\"showHeader\":false}","customScreenSaverLayout":"{\"layout\":\"DUAL_VERTICAL\",\"tiles\":[{\"urlExtra\":\"?Bereich=all\",\"provider\":\"zamg\",\"province\":\"\",\"hideTitle\":true,\"showFunctions\":false,\"mapType\":\"terrestris\",\"showOfm\":false,\"showRoute\":false,\"recipientsType\":\"ALL\",\"zoomLevel\":17,\"maxWasserkarteItems\":50,\"type\":\"future-alarms\"},{\"urlExtra\":\"?Bereich=all\",\"provider\":\"zamg\",\"province\":\"\",\"hideTitle\":true,\"showFunctions\":false,\"mapType\":\"terrestris\",\"showOfm\":false,\"showRoute\":false,\"recipientsType\":\"ALL\",\"zoomLevel\":17,\"maxWasserkarteItems\":50,\"type\":\"blackboard\"}],\"showHeader\":true}","defaultMapCoordinates":null,"printingEnabled":false},"customerLocation":{"administrativeArea":"Kärnten","latitude":46.9801471,"longitude":14.2123172,"southBoundLatitude":46.9256805,"northBoundLatitude":47.0585828,"westBoundLongitude":14.0119019,"eastBoundLongitude":14.2648153},"integrations":[],"alarms":[{"productType":"BLAULICHT","customerId":"xxx","alarmId":"995c1b40-56b7-46f5-8653-22e05f86714a","scenarioId":null,"indexNumber":78973,"alarmGroups":[{**"groupId":"G1","groupName":"Sirenenalarm"**}],"alarmDate":"2019-02-03T15:21:02.020Z","endDate":"2019-02-04T15:21:02.020Z","authorName":"LAWZ Klagenfurt","alarmText":"Sirenenalarm (Techn.Einsatz/AST1) für Metnitz:\nEO Unteralpe 18,\nGDE Metnitz 16:20","audioUrl":null,"needsAcknowledgement":true,"usersAlertedCount":44,"geolocation":{"coordinates":{"lat":46.97994689999999,"lon":14.1689192},"positionSetByAuthor":false,"radius":50,"distance":5122,"duration":7,"address":"Unteralpe 18, 9363, Österreich"},"coordinates":{"lat":46.97994689999999,"lon":14.1689192},"recipients":[{xxx}],"futureAlarms":[{"productType":"BLAULICHT","customerId":"xxx","alarmId":"xxx","scenarioId":null,"indexNumber":null,"alarmGroups":[],"alarmDate":"2019-03-08T18:30:00.000Z","endDate":"2019-03-08T18:30:00.000Z","authorName":"xxx","alarmText":"Termin am 08.03.2019 19:30 - Vollversammlung","audioUrl":null,"needsAcknowledgement":false,"usersAlertedCount":0,"geolocation":{"coordinates":null,"positionSetByAuthor":false,"radius":null,"distance":null,"duration":null,"address":null},"coordinates":null,"recipients":[],"pointsOfInterest":[]}],"blackboard":{"content":null,"lastUpdateDate":null,"lastUpdateAuthorId":null}}

Inside this Message you will find the following Information:

[{**"groupId":"G1","groupName":"Sirenenalarm"**}]

I want to trigger an Action when "groupName" is either "Sirenenalarm" or "Stiller Alarm".

How could I achive this?

Br,
Johannes

0 Likes

#2

You can use a Switch node to examine the contents of the particular property you are interested in and only pass on a message if it matches the values you are interested in.

The first step is to identify the full path to the property you want - you can use the Debug sidebar to help you do that.

https://nodered.org/docs/user-guide/messages#understanding-the-structure-of-a-message

0 Likes

#3

errr. is {**"groupId":"G1", valid JSON ? or is that something you have added to try to add markdown for emphasis here ?

0 Likes

#4

Hi, I think it was a Copy/PAste error

"alarmGroups":[{"groupId":"G1","groupName":"Sirenenalarm"}],

0 Likes

#5

Hi,

Ok, I got it now working.

I have now only one issue. The API is only receiving a new message when there is an update. Every reload/restart is recogniced as an update so I will get one message, although it might be old.

I have now one idea. The Alarm included also a timestamp:

payload.alarms[0].alarmDate
2019-02-03T15:21:02.020Z

I am thinking of taking this timestamp and block the message if it is older then 5 minutes.The question is how I could achive this?

Br,
JOhannes

0 Likes

#6

In a Function node you can do something like:

var alarmDate = new Date(msg.payload.alarms[0].alarmDate);
var now = Date.now();
var delta = now - alarmDate.getTime();
if (delta > (1000 * 60 * 5)) {
    // the gap is greater than 5 minutes (in milliseconds)
    // don't pass on the message
    return null;
}
return msg;
0 Likes

#7

Many thanks, works as expected!

Br,
Johannes

0 Likes