sis2mqtt.sh 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/bin/bash
  2. MQTT_Host="172.30.81.130"
  3. MQTT_User="raspberry"
  4. MQTT_Pass="8Wv2fxBgrSfO"
  5. MQTT_Client="nhi-sz-rl-harb"
  6. Prefix="HA/NHI/SZ/HARB/SISPMCTL"
  7. SISPMCTLBIN="/usr/local/bin/sispmctl"
  8. function SendStatus {
  9. for SIS_Device in $(${SISPMCTLBIN} -s | grep "serial" | cut -d " " -f6); do
  10. OutletNr=1
  11. for Outlet in $(${SISPMCTLBIN} -q -D ${SIS_Device} -n -g all); do
  12. if [ $Outlet -ne 0 ] ; then
  13. OutletText="ON"
  14. else
  15. OutletText="OFF"
  16. fi
  17. mosquitto_pub -h ${MQTT_Host} -u ${MQTT_User} -P ${MQTT_Pass} -i ${MQTT_Client} -t "${Prefix}/tele/${SIS_Device}/POWER${OutletNr}" -m ${OutletText}
  18. OutletNr=$((OutletNr+1))
  19. done
  20. done
  21. }
  22. SendStatus
  23. while true; do
  24. mosquitto_sub -h ${MQTT_Host} -u ${MQTT_User} -P ${MQTT_Pass} -i ${MQTT_Client} -t ${Prefix}/cmd/# -v | while read -r payload; do
  25. Topic=$(echo $payload | cut -d " " -f1)
  26. Message=$(echo $payload | cut -d " " -f2)
  27. DeviceID=$(echo $Topic | awk -F/ '{print $(NF-1)}')
  28. OutletID=$(echo $Topic | awk -F/ '{print $NF}')
  29. OutletID=$(echo $OutletID | tr -d "POWER")
  30. Message=$(echo $Message | awk '{ print toupper($0) }')
  31. if [ "x$Message" == "xON" ] || [ "x$Message" == "x1" ] ; then
  32. Param="o"
  33. SchaltWort="on"
  34. else
  35. Param="f"
  36. SchaltWort="off"
  37. fi
  38. ${SISPMCTLBIN} -q -D $DeviceID -${Param} $OutletID
  39. if [ $? -eq 0 ] ; then
  40. mosquitto_pub -h ${MQTT_Host} -u ${MQTT_User} -P ${MQTT_Pass} -i ${MQTT_Client} -t "${Prefix}/lastresult" -m "Outlet $OutletID on Device $DeviceID successfully turned $SchaltWort"
  41. else
  42. mosquitto_pub -h ${MQTT_Host} -u ${MQTT_User} -P ${MQTT_Pass} -i ${MQTT_Client} -t "${Prefix}/lastresult" -m "Error while switching Outlet $OutletID on Device $DeviceID"
  43. fi
  44. SendStatus
  45. done
  46. sleep 10
  47. done