Advertisement
YasuroK

Propagate Value from One Topic to Another in MQTT Broker

Sep 18th, 2021
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.23 KB | None | 0 0
  1. #!/data/data/com.termux/files/usr/bin/sh -f
  2.  
  3. TAB=$(printf '\t')
  4.  
  5. # The output format specified by the -F option of 'mosquitto_sub'
  6. # needs to match how it is subsequently 'read'.
  7. mosquitto_sub -t 'openScaleSync/measurements/#' -F '%t\t%p\t@s\t%I' \
  8.               -i "Scale Val Sub Client $$" \
  9. | while true; do
  10.     new_payload=$( IFS="$TAB" read -r topic payload time_processed time_processed_readable;
  11.                    (>&2 echo $topic, $payload, $time_processed $time_processed_readable);
  12.                echo "$payload" \
  13.                | jq ". + { \
  14.                            \"time\": ( .date / 1000 ), \
  15.                            \"time_readable\": \
  16.                       ( ( .date / 1000 ) \
  17.                                    | strflocaltime(\"%Y-%m-%dT%H:%M:%S+0900\") \
  18.                                  ), \
  19.                        \"orig_topic\": \"${topic}\", \
  20.                               \"time_processed\": ${time_processed}, \
  21.                               \"time_processed_readable\": \"${time_processed_readable}\" \
  22.                             } \
  23.                         | del(.date)" \
  24.              )
  25.     mosquitto_pub -r -t 'xxxx/scale/weight' -m "$new_payload" \
  26.                   -i "Scale Val Pub Client $$" ;
  27.   done
  28. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement