Hi I want to try and convert a message receieved from MQTT into a variable so I can do an "IF script" using messages sent from a certain topic on mqtt.

I tried this at top of script but didn't work:

Message = mosquitto_sub -d -t "test/test"

Thank you

  • The mosquitto_sub command runs indefinitely, right? Are you okay with using an option like -C 5 to disconnect after receiving 5 messages? – Haxiel Jan 3 at 14:24
  • I want it to run in background constantly. I want the script to play a sound if the message is the correct one or if something is received via that topic. – securitytestman Jan 3 at 16:03

If you want the result of the command in a variable you've to write the line between cote character like this :

Message=`mosquitto_sub -d -t "test/test"`

If you want to use the variable in place of the command you should write the line like this :

Message="mosquitto_sub -d -t 'test/test'"

You've to look forward the special character in the bash script

  • unfortunately it didn't work with MQTT I might use node red in the end. – securitytestman Jan 3 at 16:44

Simply execute:

mosquitto_sub -h localhost -t "#"  -C 1  > /var/spool/mosquitto/value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.