Check if string contains

Hello,

Trying to check if a string contains a certain value. I would love to do this check inside a single conditional block but it doesn’t seem like I can.

The best way I’ve thought of doing this is to split on that value, and if I get two values back, means the split worked. I think I might be able to use the function block as well, but then I need to store the result and use a conditional against the result to control my flow.

Is there an easier way for string contains comparison, in a workflow?

Hi @Kyle_Stokes2,

Welcome to the Losant Forums! Happy to help out. Would it be possible to get a screenshot of your payload and some more details about your particular use case:

  • What is the payload path of the string you want to assess?
  • What value are checking for?
  • What is your desired output - do you only need to know if the value is present (i.e. True/False) or do you need to store the value for later use?

Having this info will help us to better assist you.

Thanks,
@Aidan_Zebertavage

Hi Aidan,

Sure, thank you for the reply:

I am trying to setup some alerting. i have a sensor that measures vibration in three axes, with three values (min/max/rms) for each. The sensor also returns a temperature measurement. This gives me 10 measurements onto which I would like to set a threshold, so when they go above, I will generate an alert.
I would also like to (in the future) set a minimum threshold, to generate an alert. However that would mean 20 thresholds to maintain per device.

What is the payload path of the string you want to assess?
I am not sure if this is the right way to set a threshold for alerting, but I decided to try a ‘Device Tag’ with a key of ‘temp_f_threshold’ and a value of ‘100’.
image

My workflow triggers on Device State receive. It looks at the deviceTags, and since there are many, I begin looping through them. Since my device has non- threshold related tags, (like recipe, location, Experience Access groups,etc) I need a way to look at the name of the tag, and see if it contains ‘_threshold’ and if it does, I know that will be a threshold value to use in my workflow.

What value are checking for?
I imagine a device with several tags that are _threshold.
If I split on _threshold, to tell if it has threshold in the name, then split on the _ I should end up with
(0) - temp_f
(1) - threshold
I can use the 0th value to determine which property this threshold applies to, in this case the min_x measurement, and then use a math to check if the min_x measurement on this payload is > or < (in the future I may need to tag this as temp_f_threshold_greaterthan)

What is your desired output - do you only need to know if the value is present (i.e. True/False) or do you need to store the value for later use?
If we are talking about whether the string contains the string, Yes, desired output is True or False.

If I could iterate through the keys, and string search, I feel like I could make a workflow that finds all of the _threshold tags, splits them to determine the attribute they are thresholding against, and then still within that loop, make the comparison and raise an alert if the condition is > threshold.

In typing this up, I wonder if I could instead use a datatable with columns for:
deviceId, attribute, evaluation, threshold
And instead, loop through each payload attribute sent, and compare it to the values in the data table?

Hi @Kyle_Stokes2,

Thanks very much for all the additional detail! It’s really helpful for us to have this context when troubleshooting.

I have a better sense of what you are trying to do overall now, which is to set thresholds for different device attributes (that are relatively static) but then upon reporting of Device: State, compare the reported value of the attribute against the defined threshold and then trigger an alert if it exceeds the threshold - please correct me if I’m wrong here.

Rather than looking to maintain a variety of device tags, that may have similar names, which would increase the complexity of any string manipulation you would need - I’d like to recommend you look into leveraging Attribute Tags instead.

When you configure your device, after you assign attributes and update, you have the ability to further refine your attributes using tags. In the case of a temperature attribute, you could set the min and max temperature thresholds as attribute tags, as seen below:

Once you have these attribute tags set up, it will allow for better management of these thresholds at the device level.

Once you have the attribute tags set with the proper values, you can now reference these values from the payload in a workflow for conditional assessment. I’ve attached an example workflow for how that might work. See attached below.

This workflow is simulating device data and then proceeds to loop through each attribute defined on your device. It will compare the attribute tags min/max values against the real-time data and then based on the results will trigger the necessary alert (in this workflow the Alerts are represented by additional Debug nodes, but you can change out the alert type node for your needs, SMS, email, Slack, etc.).

I do agree that you could handle this problem with a Data Table as well, however, with that approach you may run up against the storage limit depending upon how many devices you are looking to manage.

threshold-example-develop.flow (16.5 KB)

If you’re not familiar with importing a workflow into your application, see the docs here:

There are additional notes within this workflow as well - but please let me know if anything is confusing or further direction is needed.

Best,
@Aidan_Zebertavage

Thank you Aidan! I imported the workflow and looked around, it does make sense, thank you!

I think this will do what I want it to do with some additonal logic that I haveused elsewhere to not raise an event, if one already exists for the given combination of deviceId + metric being alerted to.

Hi Aidan,

Still working through this. A new ask from my team was if Losant could display the threshold on the graph?

It looks like the Time Series Block can only graph the Attribute itself, and not an Attribute Tag.
To graph a line for the threshold, it would need to be an Attribute itself.

Just wanted to see if there was another way to show the Attribute Tag on the graph like the screenshot below.

I have some ideas to have the Threshold workflow that you sent over, to mirror the value for the Attribute Tag, over to the Attribute that represents the threshold for the given attribute.

@Kyle_Stokes2,

Great question, unfortunately, that’s now possible within that block today. However, I can make a new feature request for you and let you know if it gets released!

One workaround we’ve suggested in the past is to set an attribute to a static value. Since it’s a device state, you can display it on a dashboard.

A more advanced route to accomplish this today would be to use the Custom HTML Block:

This gives you the top flexibility in HTML, CSS, and JS.