I am currently fiddeling around with AWS GreenGrass and I have gone through most of the basic examples. Now the problem I have understanding how I would perform different actions in my lambda function depending on the MQTT topic it was triggered by. I could not really find a documentation on how to differentiate which topic triggered the function handler in my python script. The thing I am trying to build is a small controller for my sonos system which uses the following very basic topics:
sonos/play
Which just tries playing a certain song I hardcoded.
sonos/pause
Which should be pausing the system.
I know I could create several lambdas for each command but that seems pretty finicky. Can someone please explain how one would tackle this sort of issue?
Based on this dev guide, in your lambda you can get the topic from the context:
def get_input_topic(context):
try:
topic = context.client_context.custom['subject']
except Exception as e:
logging.error('Topic could not be parsed. ' + repr(e))
return topic
action = get_input_topic(context).split('/')[-1]
I don't actually think this is possible. If you look at both of the event and context objects passed into your lambda function, neither of them contain the topic address.
What I have had to do is to have a separate lambda function to respond for messages for each MQTT topic.
Happy to be corrected if this can be done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With