- Newest
- Most votes
- Most comments
Hi. You will likely want to make use of wildcards in the FROM clause and the topic() function in the SELECT clause. To end up with something like this as the SQL statement for your rule:
SELECT light, topic(2) as thingName FROM 'sensor_values/+' WHERE light < 30
That way, one rule can process the 30% condition for all light sensors. If the 30% value is configurable, there are many possible ways to solve it.
More information here: https://docs.aws.amazon.com/whitepapers/latest/designing-mqtt-topics-aws-iot-core/best-practices-for-using-mqtt-topics-in-the-aws-iot-rules-engine.html
Hi,
As an alternative to DynamoDB to store the sensor specific thresholds, you can leverage named device shadows and use the following SQL:
SELECT light FROM 'sensor_values/+' WHERE light < get_thing_shadow(topic(2), "threshold", <IAM role arn>).state.desired.level
The content of the threshold named shadow for one of the sensors would be:
{
"state": {
"reported": {
"level": 30
}
}
}
Documentation for get_thing_shadow
Cheers,
Massimiliano
Hello Greg,
I understand what you mean by wildcard entries for select query, but our where condition is configurable and will be dynamic and different in every smart bulb.
Example for 3 bulbs:
SELECT light FROM 'sensor_values/Light_Sensor_1' WHERE light < 30
SELECT light FROM 'sensor_values/Light_Sensor_2' WHERE light < 45
SELECT light FROM 'sensor_values/Light_Sensor_3' WHERE light < 80
How to achieve this?
Response in comment trail above.
Hello,
Thank you for your answers, gave us clarity for few things.
Here are few followups that might help us completely understand the problem statement.
Firstly, the using of function like get_thing_shadow & get_dynamodb in the rule sounds great to solve one-to-one relationships (1 Light_Sensor triggering 1 Smart_Bulb) but how to tackle one-to-many relationships (1 Light_Sensor triggering 3 Smart_Bulb).
Light_Sensor publishes real time light intensity on topic 'sensor_values/Light_Sensor'
Smart_Bulb_1 has threshold 30%
Smart_Bulb_2 has threshold 45%
Smart_Bulb_3 has threshold 80%
Now using the rule statement as you suggested has one problem
SELECT light FROM 'sensor_values/+' WHERE light < get_thing_shadow(topic(2), "threshold", <IAM role arn>).state.desired.level
The problem is that it will return the thing name as Light_Sensor, and whose shadow will have no information about the threshold of individual smart bulbs. As thresholds of individual smart_bulbs will be stored in their respective shadows.
The same problem can be iterated in using the get_dynamodb function instead of get_thing_shadow.
Can we use rules to scale up in this situation ? Is there any chance AWS allows to increase the quota limit on requests for specific use.
Thanks.
Relevant content
- asked 2 years ago
- AWS OFFICIALUpdated 4 months ago

Hello Greg,
I understand what you mean by wildcard entries for select query, but our where condition is configurable and will be dynamic and different in every smart bulb.
Example for 3 bulbs:
SELECT light FROM 'sensor_values/Light_Sensor_1' WHERE light < 30
SELECT light FROM 'sensor_values/Light_Sensor_2' WHERE light < 45
SELECT light FROM 'sensor_values/Light_Sensor_3' WHERE light < 80
How to achieve this?
Where will the threshold be stored? You might use get_dynamodb in the rule. Or you might check the threshold in the Lambda instead of the rule. Or you might send the threshold setting to the bulb so it only publishes a message when the value traverses the threshold. But I wouldn't recommend you dynamically replace/delete/create the rules because
ReplaceTopicRulehas a hard limit of just 5 TPS.