Hello
I started reading the Apator MBus meter using the configuration and description from the website Taking Your M-Bus To The Next Levelā¦ MQTT ā MolesBlog but I have a small problem because I get 2 errors that I canāt deal with .
pi@raspberrypi:~/bin $ read_send_meters_mqtt2.sh
Getting data from 0025244901061507ā¦ 2875 bytes sent
water/0025244901061507 -> "252449"
water/0025244901061507 -> "APA"
water/0025244901061507 -> "21"
water/0025244901061507 -> "88"
sed: -e expression #1, char 6: unknown option to `s'
sed: -e expression #1, char 7: unterminated `s' command
water/0025244901061507 ā> {
"MBusData": {
"SlaveInformation": {
"Id": "252449",
"Manufacturer": "APA",
"Version": "21",
"ProductName": null,
"Medium": "Water",
"AccessNumber": "74",
"Status": "88",
"Signature": "0000"
},
"DataRecord": [
{
"_id": "0",
"Function": "Instantaneous value",
"StorageNumber": "0",
"Unit": "Fabrication number",
"Value": "252449",
"Timestamp": "2023-02-16T16:44:41Z"
},
{
"_id": "1",
"Function": "Instantaneous value",
"StorageNumber": "0",
"Unit": "Time Point (time & date)",
"Value": "2023-02-16T17:44:00",
"Timestamp": "2023-02-16T16:44:41Z"
},
{
"_id": "2",
"Function": "Instantaneous value",
"StorageNumber": "0",
"Unit": "Volume (m m^3)",
"Value": "20",
"Timestamp": "2023-02-16T16:44:41Z"
},
{
"_id": "3",
"Function": "Instantaneous value",
"StorageNumber": "0",
"Unit": "Volume flow (m m^3/h)",
"Value": "0",
"Timestamp": "2023-02-16T16:44:41Z"
},
{
"_id": "4",
"Function": "Instantaneous value",
"StorageNumber": "1",
"Unit": "Volume (m m^3)",
"Value": "53",
"Timestamp": "2023-02-16T16:44:41Z"
},
{
"_id": "5",
"Function": "Instantaneous value",
"StorageNumber": "1",
"Unit": "Time Point (date)",
"Value": "2023-02-01",
"Timestamp": "2023-02-16T16:44:41Z"
},
{
"_id": "6",
"Function": "Instantaneous value",
"StorageNumber": "0",
"Unit": "Operating time (days)",
"Value": "1575",
"Timestamp": "2023-02-16T16:44:41Z"
},
{
"_id": "7",
"Function": "Instantaneous value",
"StorageNumber": "0",
"Unit": "Error flags",
"Value": "262660",
"Timestamp": "2023-02-16T16:44:41Z"
},
{
"_id": "8",
"Function": "Instantaneous value",
"StorageNumber": "0",
"Unit": "Manufacturer specific",
"Value": "263172",
"Timestamp": "2023-02-16T16:44:41Z"
},
{
"_id": "9",
"Function": "Instantaneous value",
"StorageNumber": "0",
"Unit": "Manufacturer specific",
"Value": "0",
"Timestamp": "2023-02-16T16:44:41Z"
},
{
"_id": "10",
"Function": "Instantaneous value",
"StorageNumber": "0",
"Unit": "Manufacturer specific",
"Value": "-4980702",
"Timestamp": "2023-02-16T16:44:41Z"
},
{
"_id": "11",
"Function": "Manufacturer specific",
"Value": "00 05 15 39 FF 00 00 00 00 00",
"Timestamp": "2023-02-16T16:44:41Z"
}
]
}
}
sed: -e expression #1, char 6: unknown option to `s'
sed: -e expression #1, char 7: unterminated `s' command
#!/bin/bash
# Code Basis stammt von https://the78mole.de/taking-your-m-bus-online-with-mqtt/
# Ich habe es nur ergƤnzt um alle Datenfelden flexibel zum MQTT Server zu Ć¼bertragen
# Getestet nur mit einem WƤrmezƤhler. Ich hoffe es felxibel genug auch fĆ¼r andere GerƤte
ADDRESS_FILE=~/addresses.txt
BAUDRATE=2400
DEVICE=/dev/serial0
MQTT_HOST=19***********
MQTT_USER=mqttbroker
MQTT_PASS=**********
MQTT_TOPIC=water
#Basis Informationen
modbus_array[0]=.MBusData.SlaveInformation.Id
modbus_array[1]=.MBusData.SlaveInformation.Manufacturer
modbus_array[2]=.MBusData.SlaveInformation.Version
modbus_array[3]=.MBusData.SlaveInformation.Status
#āāāāāāāāāāāāāāāāāāā-
while read ameter
do
echo -n "Getting data from $ameterā¦"
# The sed is for replacing the @ with _ to be able to match on it in HASS templates
METER_DATA=$(mbus-serial-request-data-multi-reply -b $BAUDRATE $DEVICE $ameter | xq . | sed -e "s/@/_/")
/usr/bin/mosquitto_pub -h $MQTT_HOST -u $MQTT_USER -P $MQTT_PASS -t $MQTT_TOPIC/$ameter -m "${METER_DATA}"
BYTCNT=$(echo "$METER_DATA" | wc -c)
echo " $BYTCNT bytes sent"
# Basis Parameter auslesen
for Parameter in "${modbus_array[@]}"
do
ausgabe=$(echo $METER_DATA | jq "$Parameter" )
#Punkte in / konvertieren fĆ¼r mqtt
Parameter=$(echo $Parameter | sed -e "s/.//g")
/usr/bin/mosquitto_pub -h $MQTT_HOST -u $MQTT_USER -P $MQTT_PASS -t $MQTT_TOPIC/$ameter$Parameter -m "$ausgabe"
#Ausgabe des MQTT Pfades und Wertes
echo "$MQTT_TOPIC/$ameter$Parameter -> $ausgabe"
done
#Schleife Ć¼ber alle Data Records
for row in $(echo $METER_DATA | jq '.MBusData.DataRecord | keys'); do
#Leere Datenfelder am Anfang und Ende abfangen
if [[ ${row} != "[" ]] && [[ ${row} != "]" ]]
then
#Das Komma am Ende entfernen
id=$(echo "${row}" | sed -e "s/,//")
#Einen Int Wert daraus machen
nummer=$(("$id"+0))
#Alle Keys (Datenpunkte) auslesen
for name in $(echo $METER_DATA | jq ".MBusData.DataRecord[$nummer] | keys"); do
#Leere Datenfelder am Anfang und Ende abfangen
if [[ ${name} != "[" ]] && [[ ${name} != "]" ]]
then
# Ein paar Sonderzeichen aus dem Pfad entfernen
pfad=".MBusData.DataRecord[$nummer].${name}"
pfad=$(echo $pfad | sed "s/,//g")
pfad=$(echo $pfad | sed "s/.//g")
#Den richtigen Wert aus dem json holen
wert=$(echo $METER_DATA | jq $pfad)
#Umformatieren fĆ¼r eine MQTT Pfad
pfad=$(echo $pfad | sed "s/.///g")
pfad=$(echo $pfad | sed "s/[/_/g")
pfad=$(echo $pfad | sed "s/]//g")
#Zum MQTT Server senden
/usr/bin/mosquitto_pub -h $MQTT_HOST -u $MQTT_USER -P $MQTT_PASS -t $MQTT_TOPIC/$ameter$pfad -m "$wert"
echo "$MQTT_TOPIC/$ameter$pfad ā> $wert"
fi
done
fi
done
done < <(cat $ADDRESS_FILE)