Files
Home-Assistant-Addons/rtlamr/run.sh
T

72 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
export LANG=C
PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin"
echo "/usr/local/lib64/" > /etc/ld.so.conf
export LD_LIBRARY_PATH=/usr/local/lib64/
# touch /var/log/rtl_433.log
CONFIG_PATH=/data/options.json
MQTT_HOST="$(jq --raw-output '.mqtt_host' $CONFIG_PATH)"
MQTT_USER="$(jq --raw-output '.mqtt_user' $CONFIG_PATH)"
MQTT_PASS="$(jq --raw-output '.mqtt_password' $CONFIG_PATH)"
MSG_TYPE="$(jq --raw-output '.msg_type // empty' $CONFIG_PATH)"
METER_IDS="$(jq --raw-output '.meter_ids // empty' $CONFIG_PATH)"
# Start the listener and enter an endless loop
echo "Starting RTLAMR with parameters:"
echo "MQTT Host =" $MQTT_HOST
echo "MQTT User =" $MQTT_USER
#MQTT_PASS_REDACTED=$(sed 's/^......./*******/' <<<$MQTT_PASS)
#echo "MQTT Password =" $MQTT_PASS_REDACTED
echo "Message Type =" $MSG_TYPE
echo "Meter IDs =" $METER_IDS
#set -x ## uncomment for MQTT logging...
/usr/local/bin/rtl_tcp &>/dev/null &
# /usr/local/bin/rtl_tcp > /var/log/rtl_tcp.log 2>&1 &
# Sleep to fill buffer a bit
sleep 15
# set a time to listen for. Set to 0 for unliminted
#tail -f /dev/null
if [ -z ${MSG_TYPE+x} ] || [${MSG_TYPE} = ""]; then
MSG_TYPE="all"
fi
if [ -z ${METER_IDS+x} ] || [${METER_IDS} = ""]; then
ID_FILTER_OPTION=""
else
ID_FILTER_OPTION="-filterid=$METER_IDS"
fi
# Do this loop, so will restart if buffer runs out
while true; do
/go/bin/rtlamr -format json -msgtype=$MSG_TYPE $ID_FILTER_OPTION | while read line
do
VAL="$(echo $line | jq --raw-output '.Message.Consumption')"
METERID="$(echo $line | jq --raw-output '.Message.ID')"
MSGTYPE="$(echo $line | jq --raw-output '.Type')"
MSG="$(echo $line | jq --raw-output '.Message')"
MQTT_PATH="rtlamr/$METERID"
# Create file with touch /var/log/rtl_433.log if logging is needed
[ -w /var/log/rtl_433.log ] && echo $line >> /var/log/rtl_433.log
# Publish to MQTTT
echo $VAL | /usr/bin/mosquitto_pub -h $MQTT_HOST -u $MQTT_USER -P $MQTT_PASS -i RTL_433 -r -l -t $MQTT_PATH/reading
echo $MSGTYPE | /usr/bin/mosquitto_pub -h $MQTT_HOST -u $MQTT_USER -P $MQTT_PASS -i RTL_433 -r -l -t $MQTT_PATH/type
echo $MSG | /usr/bin/mosquitto_pub -h $MQTT_HOST -u $MQTT_USER -P $MQTT_PASS -i RTL_433 -r -l -t $MQTT_PATH/message
done
sleep 60
done