Reputation: 601
I'm working on Iot enabled automatic machine industry which is using aws IoT core. Each machine has it's own benchmark temperature level which varies and I want to trigger an AWS SNS based on their benchmark temperature level . Each of the machine publish the data to the same topic separated by their ID. The topic name looks like bellow
machine/+/data
Where the + sign indicates the machine_id I have my rules written like bellow.
select * from machine/+/data where temperature > 25
which works for all the topics now the problem the temperature level is not same for all the machines for example a machine with id = 1 may have temperature level 30 another machine with id = 2 may have 28.
My question is that, is it possible to trigger SNS based on the machine temperature using Rules or are their any other way to do this?
Thanks in advance for your reply.
Upvotes: 0
Views: 1150
Reputation: 4946
The IoT rule can be made dynamic by using the get_dynamodb
, aws_lambda
or get_thing_shadow
functions in the WHERE
clause.
From https://forums.aws.amazon.com/thread.jspa?messageID=932907󣰫
You have 3 ways to fetch data in AWS IoT Topic Rule. All can be used in the WHERE clause evaluation.
- aws_lambda() function (https://docs.aws.amazon.com/iot/latest/developerguide/iot-sql-functions.html#iot-func-aws-lambda) which would return data that you need from your lambda in your Rule evaluation. Most useful for getting value from another service.
- get_dynamodb() function (https://docs.aws.amazon.com/iot/latest/developerguide/iot-sql-functions.html#iot-sql-function-get-dynamodb) which lets you fetch data from your DynamoDB table. You can keep a per-thing threshold values in a table.
- get_thing_shadow() function (https://docs.aws.amazon.com/iot/latest/developerguide/iot-sql-functions.html#iot-sql-function-get-thing-shadow) which lets you get data from your Thing Shadow documents. If you are already using a Thing Shadow, this could be easiest to keep per-thing threshold value.
As an example, the get_thing_shadow
documentation has:
SELECT * from 'a/b'
WHERE
get_thing_shadow("MyThing","arn:aws:iam::123456789012:role/AllowsThingShadowAccess")
.state.reported.alarm = 'ON'
If your benchmark temperature levels were in the thing registry then a rule similar to this could be used:
SELECT * FROM machine/+/data
WHERE
temperature > get_thing_shadow(topic(2),"arn:aws:iam::123456789012:role/AllowsThingShadowAccess")
.state.threshold
Upvotes: 0
Reputation: 961
The issue with your approach is the way AWS IoT Queries work -- once a query is activated, AWS will try to validate/execute it for every single message based on the topic filter you provide in the SELECT clause.
So, you cannot have a single query to achieve what you want (with varying temperature_level values).
There are two variables in your problem - machine_id, temperature_level. Both are related to each other.
You may want to approach your problem with:
Option-1::Rudimentary - Create multiple queries (one each for machine_id, temperature combination) and deploy.
Option-2::Dynamic - Store the variables as tuples in storage of your choice, e.g. AWS DynamoDB. Have a DB Stream / Trigger to notify table changes and handle them with AWS Lambda which re-creates AWS IoT Rules dynamically (using AWS SDK / APIs) as and when the data changes in DynamoDB.
Hope it helps!
Upvotes: 1