Reputation: 1
I am trying to create a script in bash in order to republish 1 MQTT message for 3 virtual sensors in Domoticz. I am running into a trouble while creating message part for mossquito_pub command
#!/bin/bash
# This script subscribes to a MQTT topic using mosquitto_sub.
# On each message received, you can execute whatever you want.
while true # Keep an infinite loop to reconnect when connection lost/broker unavailable
do
mosquitto_sub -h "192.168.1.30" -t "tele/tasmota_D875F9/SENSOR" | while read -r payload
do
# Here is the callback to execute whenever you receive a message:
str="${payload}"
voltage1= echo $str | cut -f 22 -d ','| tr -dc '0-9'
echo $voltage1
mosquitto_pub -h 192.168.1.30 -p 1883 -t 'domoticz/in' -m '{"idx" : 33, "nvalue" : 0, "svalue" : "$voltage1" }'
voltage2= echo $str | cut -f 23 -d ','| tr -dc '0-9'
echo $voltage2
mosquitto_pub -h 192.168.1.30 -p 1883 -t 'domoticz/in' -m '{"idx" : 34, "nvalue" : 0, "svalue" : "$voltage2" }'
voltage3= echo $str | cut -f 24 -d ','| tr -dc '0-9'
echo $voltage3
mosquitto_pub -h 192.168.1.30 -p 1883 -t 'domoticz/in' -m '{"idx" : 35, "nvalue" : 0, "svalue" : "$voltage3" }'
done
sleep 10 # Wait 10 seconds until reconnection
done # & # Discomment the & to run in background (but you should rather run THIS script in background)
sleep 10 # Wait 10 seconds until reconnection
done # & # Discomment the & to run in background (but you should rather run THIS script in background)
Currently, terminal echo output is:
230
231
233
Message transmitted:
2020-12-01 13:51:27.952 MQTT: Topic: domoticz/in, Message: {"idx" : 33, "nvalue" : 0, "svalue" : "$voltage1" }
2020-12-01 13:51:28.057 MQTT: Topic: domoticz/in, Message: {"idx" : 34, "nvalue" : 0, "svalue" : "$voltage2" }
2020-12-01 13:51:28.161 MQTT: Topic: domoticz/in, Message: {"idx" : 35, "nvalue" : 0, "svalue" : "$voltage3" }
I need variables voltage1
, voltage2
, voltage3
to be replaced by their values as they are seen in terminal echo output including quotation marks in front and right after.
Please help me.
Upvotes: 0
Views: 27