Notifications from Alerts
-
First, it will never be null, so that's not a valid test (I mentioned that here).
For your message text, you probably need to convert it from an array to a formatted string that is actually readable. This has been covered a bunch as well (even in this very thread), so I won't give away an answer... better to practice!
-
@toggledbits said in Notifications from Alerts:
it will never be null
Maybe I can use Variable value "is not empty" instead? As the trigger.
I also tried using a Join expression to get some formatted text out of it but I still just get this:
-
Here's what I have... I want to be notified on all alerts, and I only want to include the message from the most recent alert in the notification. I also needed to remove the escaped quotes (") from the message string because it was messing with my notification where I was inserting the expression into the http call.
So:
g_alerts_array = get all the alerts
g_last_alert = get only the alert where the timestamp == alert_last timestamp
g_last_alert_replaced = remove the " from the alert message string
I realize there is probably some way to combine all of these into one expression, but I was getting errors when I tried wrapping the "first" and "replace" functions around the alerts array expression.
Then in my rule:
If you only want http errors, then you'll have to modify the above expressions to find only those, as Patrick showed above. You'd also have to use a different trigger, as you don't want to fire the event every time the alert_last changes, as some of those won't be http errors.
-
toggledbitsreplied to cw-kid on Dec 24, 2021, 9:09 PM last edited by toggledbits Dec 24, 2021, 4:12 PM
@cw-kid said in Notifications from Alerts:
I also tried using a Join expression to get some formatted text out of it but I still just get this:
The reason your
join()
doesn't work as expected is that the array contains objects, not strings. @Alan_F gives an option, but he usesfirst
so only one alert, not in array, would result.Good efforts all around though. Let's fill in some details...
When writing complex expressions, and particularly as you're learning the expression language, it's often better to break it down into pieces as @Alan_F has done. For what you seem to want to do, you started with filtering the array:
Alerts_HTTP_Failed = each alert in getEntity( 'reactor_system>system' ).attributes.reactor_system.alerts: find( alert.message, "HTTP request", "i" ) >= 0 ? alert : null
This gives you an array of objects (as we've established) of only the HTTP request errors. From there, you want to get to just the message strings, but all of them, not just one/the first, so you'll use
each
again:HTTP_Fail_Messages = each alert in Alerts_HTTP_Failed: alert.message
That simple expression loops over the objects in the array and extracts just the
message
field. The result of that is a new array of strings, rather than an array of objects. From there, join...HTTP_Messages_String = join( HTTP_Fail_Messages, "; " )
And as they say, Robert is your mother's brother.
Doing it all together:
join( each alert in getEntity( 'reactor_system>system' ).attributes.reactor_system.alerts: find( alert.message, "HTTP request", "i" ) >= 0 ? alert.message : null, "; " )
If it looks like I left out a step, I did, in a way... rather than having the filter loop return objects, this version just returns the message (strings) directly as an array, so it eliminates that middleman and can then be passed directly into
join()
.@alan_f said in Notifications from Alerts:
g_last_alert_replaced = remove the " from the alert message string
Although the quotes look funny in the display, I don't think they will bother anything in use, as the notifiers are built to digest/handle such special characters properly.
-
Alan_Freplied to toggledbits on Dec 24, 2021, 10:17 PM last edited by Alan_F Dec 24, 2021, 5:18 PM
@toggledbits I'm using a self-hosted notification system (Gotify) so when I said 'send notification' it would have been more precise to say "use as the JSON content in an http post request". The " definitely broke the request when it was substituted in to the middle of the request body. I also tried to urlencode the string but while that didn't break the request, it was super ugly as it passed all the encoded characters into the displayed message. I'm not very proficient at regex, so it took an online regex tester and a bit of trial and error to arrive at the right number of backslashes to finally make it work.
Another lesson learned: turn off 'auto evaluate expression' when trying to write expressions that refer to the alerts, 'cause if you get the expression wrong and throw an alert that causes the expression to reevaluate which throws an alert... I had to restart my Reactor Docker container a few times before I turned off 'auto evaluate' ¯_(ツ)_/¯
-
@alan_f said in Notifications from Alerts:
it would have been more precise to say "use as the JSON content in an http post request". The " definitely broke the request when it was substituted in to the middle of the request body.
That would depend on how you are building it. If you are just jamming it in as a string, that's definitely going to create problems. A better approach may be to build the payload as an actual object in the expressions, and then at the end, right before sending to Gotify, put it through the
stringify()
function, which will convert the object to a JSON string, and that will also handle the quotes correctly to produce valid JSON with the quotes embedded. That will spare you the agony of having to sanitize every string you might ever send. -
Thanks I think its working now, here is what I have in the rule:
Trigger: Still not sure if I am using the best or correct trigger?
Local Expressions:
Action:
Send text message to Telegram app:
And this is what I now see on my phone in Telegram:
Thanks
-
I am seeing this alert sometimes to do with this rule
Rule "Alerts - HTTP Request Failed" is being throttled because its update rate exceeds 60 updates per minute. Check for logic errors that may be causing an evaluation loop, or devices that are "flapping".
-
Patrick, is there anyway to fire a reaction based on what you are reporting in your status - current alerts?
-
@rogero See this thread: https://smarthome.community/topic/706/notifications-from-alerts/3
-
-
I am running a z-wave.me hat on a rpi. That is the device that is loosing connectivity occasionally. Since it is intermittent, it is hard to find a time when it occurs that I can look at the error and try to figure out the syntax of local expression. Anyone know of a commend that I can run from the terminal in HASS OS to kill the connection to force the error that won't screw up my zwave after a reboot?
-