got rid of direct domoticz API calls in favour of pushing data via MQTT MySensors framework

this way it's auto-configured in domoticz and anything else can utilize it as well
This commit is contained in:
Peter Molnar 2018-10-26 09:33:05 +01:00
parent 768b5deefa
commit 46fb24150f
5 changed files with 258 additions and 39 deletions

View file

@ -1,5 +1,116 @@
#!/usr/bin/env bash
HOSTNAME="${COLLECTD_HOSTNAME:-$(hostname -f)}"
INTERVAL="${COLLECTD_INTERVAL:-60}"
MQTT_PORT="${COLLECTD_MQTTPORT:-1883}"
MQTT_HOST="${COLLECTD_MQTTHOST:-$(hostname -f)}"
MQTT_TOPIC="${COLLECTD_MQTTOPIC:-domoticz/in/MyMQTT}"
I2CBUS=$(dmesg | grep 'connected i2c-tiny-usb device' | head -n1 | sed -r 's/.*\s+i2c-([0-9]+).*/\1/')
S_DOOR=0
S_MOTION=1
S_SMOKE=2
S_BINARY=3
S_DIMMER=4
S_COVER=5
S_TEMP=6
S_HUM=7
S_BARO=8
S_WIND=9
S_RAIN=10
S_UV=11
S_WEIGHT=12
S_POWER=13
S_HEATER=14
S_DISTANCE=15
S_LIGHT_LEVEL=16
S_ARDUINO_NODE=17
S_ARDUINO_REPEATER_NODE=18
S_LOCK=19
S_IR=20
S_WATER=21
S_AIR_QUALITY=22
S_CUSTOM=23
S_DUST=24
S_SCENE_CONTROLLER=25
S_RGB_LIGHT=26
S_RGBW_LIGHT=27
S_COLOR_SENSOR=28
S_HVAC=29
S_MULTIMETER=30
S_SPRINKLER=31
S_WATER_LEAK=32
S_SOUND=33
S_VIBRATION=34
S_MOISTURE=35
S_INFO=36
S_GAS=37
S_GPS=38
S_WATER_QUALITY=39
V_TEMP=0
V_HUM=1
V_STATUS=2
V_PERCENTAGE=3
V_PRESSURE=4
V_FORECAST=5
V_RAIN=6
V_RAINRATE=7
V_WIND=8
V_GUST=9
V_DIRECTION=10
V_UV=11
V_WEIGHT=12
V_DISTANCE=13
V_IMPEDANCE=14
V_ARMED=15
V_TRIPPED=16
V_WATT=17
V_KWH=18
V_SCENE_ON=19
V_SCENE_OFF=20
V_HVAC_FLOW_STATE=21
V_HVAC_SPEED=22
V_LIGHT_LEVEL=23
V_UP=29
V_DOWN=30
V_STOP=31
V_IR_SEND=32
V_IR_RECEIVE=33
V_FLOW=34
V_VOLUME=35
V_LOCK_STATUS=36
V_LEVEL=37
V_VOLTAGE=38
V_CURRENT=39
V_RGB=40
V_RGBW=41
V_ID=42
V_UNIT_PREFIX=43
V_HVAC_SETPOINT_COOL=44
V_HVAC_SETPOINT_HEAT=45
V_HVAC_FLOW_MODE=46
V_TEXT=47
V_CUSTOM=48
V_POSITION=49
V_IR_RECORD=50
V_PH=51
V_ORP=52
V_EC=53
V_VAR=54
V_VA=55
V_POWER_FACTOR=56
function test_i2c_device {
if [ ! -d "/sys/bus/i2c/devices/i2c-${I2CBUS}/${I2CBUS}-00${2}" ]; then
echo "run ${3} 0x${2} > /sys/bus/i2c/devices/i2c-${I2CBUS}/new_device"
exit 2
fi
}
function humidity_to_comfort {
hum=$(echo "scale=0;$1/1" | bc)
@ -19,8 +130,33 @@ function humidity_to_comfort {
function domoticz_send {
SEND="${1}"
if nc -z ${HOSTNAME} ${DOMOTICZ_PORT}; then
#>&2 echo "sending to domoticz: ${SEND}"
curl -s -H "Accept: application/json" "$SEND" >/dev/null
else
>&2 echo "domoticz can't be reaced at ${HOSTNAME} ${DOMOTICZ_PORT}"
fi
}
function mymqtt_test {
if [ ! -x "$(which mosquitto_pub)" ]; then
>&2 echo "mosquitto_pub command is not executable or missing"
exit 2
fi
if ! nc -z ${MQTT_HOST} ${MQTT_PORT}; then
>&2 echo "no MQTT available on ${MQTT_HOST}:${MQTT_PORT}"
exit 2
fi
}
function mymqtt_init {
mymqtt_test
mosquitto_pub -t "${MQTT_TOPIC}/${1}/${2}/0/0/${3}" -m "${4}"
}
function mymqtt_update {
mymqtt_test
mosquitto_pub -t "${MQTT_TOPIC}/${1}/${2}/1/0/${3}" -m "${4}"
}

View file

@ -2,14 +2,34 @@
source /usr/local/lib/collectd/_functions.sh
HOSTNAME="${COLLECTD_HOSTNAME:-$(hostname -f)}"
INTERVAL="${COLLECTD_INTERVAL:-60}"
DOMOTICZ_PORT="${2:-8080}"
DOMOTICZ_IDX="${1:-1}"
DOMOTICZ_URL="http://${HOSTNAME}:${DOMOTICZ_PORT}/json.htm?type=command&param=udevice&idx=${DOMOTICZ_IDX}&nvalue=0&svalue="
ID="99"
TEM_ID="1"
HUM_ID="2"
BAR_ID="3"
RAIN_ID="4"
SUN_ID="5"
mymqtt_init "${ID}" "${TEM_ID}" "${S_TEMP}" "CBG TEMPERATURE"
mymqtt_init "${ID}" "${HUM_ID}" "${S_HUM}" "CBG HUMIDITY"
mymqtt_init "${ID}" "${BAR_ID}" "${S_BARO}" "CBG BAROMETER"
mymqtt_init "${ID}" "${RAIN_ID}" "${S_RAIN}" "CBG RAIN"
mymqtt_init "${ID}" "${SUN_ID}" "${S_CUSTOM}" "CBG SUN"
while true; do
cbg="$(wget -O- -q https://www.cl.cam.ac.uk/research/dtg/weather/current-obs.txt)"
#Cambridge Computer Laboratory Rooftop Weather at 09:03 AM on 26 Oct 18:
#Temperature: 7.6 C
#Pressure: 1010 mBar
#Humidity: 87 %
#Dewpoint: 5.6 C
#Wind: 4 knots from the W
#Sunshine: 0.0 hours (today)
#Rainfall: 0.0 mm since midnight
#Summary: very humid, cold, light winds
prefix="sensors-weather"
suffix="-cambridge"
@ -17,14 +37,18 @@ while true; do
data[temperature]=$(grep "Temperature" <<< "${cbg}" | awk '{print $2}')
data[humidity]=$(echo "scale=0;$(grep "Humidity" <<< "${cbg}" | awk '{print $2}')/1" | bc)
data[pressure]=$(echo "scale=2;$(grep "Pressure" <<< "${cbg}" | awk '{print $2}')/1" | bc)
data[rain]=$(echo "scale=2;$(grep "Rainfall" <<< "${cbg}" | awk '{print $2}')/1" | bc)
data[sunshine]=$(echo "scale=2;$(grep "Rainfall" <<< "${cbg}" | awk '{print $2}')/1" | bc)
for key in "${!data[@]}"; do
echo "PUTVAL $HOSTNAME/${prefix}/${key}${suffix} interval=$INTERVAL N:${data[$key]}"
done
domoticz_data="${data[temperature]};${data[humidity]};$(humidity_to_comfort ${data[humidity]});${data[pressure]};0"
SEND="${DOMOTICZ_URL}${domoticz_data}"
domoticz_send ${SEND}
mymqtt_update "${ID}" "${TEM_ID}" "${V_TEMP}" "${data[temperature]}"
mymqtt_update "${ID}" "${HUM_ID}" "${V_HUM}" "${data[humidity]}"
mymqtt_update "${ID}" "${BAR_ID}" "${V_PRESSURE}" "${data[pressure]}"
mymqtt_update "${ID}" "${RAIN_ID}" "${V_RAIN}" "${data[rain]}"
mymqtt_update "${ID}" "${SUN_ID}" "${V_VAR1}" "${data[sunshine]}"
sleep "$INTERVAL"
done

View file

@ -2,24 +2,28 @@
source /usr/local/lib/collectd/_functions.sh
HOSTNAME="${COLLECTD_HOSTNAME:-$(hostname -f)}"
INTERVAL="${COLLECTD_INTERVAL:-60}"
DOMOTICZ_PORT="${2:-8080}"
DOMOTICZ_IDX="${1:-1}"
DOMOTICZ_URL="http://${HOSTNAME}:${DOMOTICZ_PORT}/json.htm?type=command&param=udevice&idx=${DOMOTICZ_IDX}&nvalue=0&svalue="
ID="77"
NAME="bme280"
i2cdev=$(dmesg | grep 'connected i2c-tiny-usb device' | head -n1 | sed -r 's/.*\s+i2c-([0-9]+).*/\1/')
echo "bme280 0x77" > /sys/bus/i2c/devices/i2c-${i2cdev}/new_device
test_i2c_device "${ID}" "${NAME}"
TEM_ID="1"
HUM_ID="2"
BAR_ID="3"
mymqtt_init "${ID}" "${TEM_ID}" "${S_TEMP}" "BME280 TEMPERATURE"
mymqtt_init "${ID}" "${HUM_ID}" "${S_HUM}" "BME280 HUMIDITY"
mymqtt_init "${ID}" "${BAR_ID}" "${S_BARO}" "BME280 BAROMETER"
while true; do
for sensor in /sys/bus/iio/devices/iio\:device*; do
name=$(cat "${sensor}/name")
if [ "$name" != "bme280" ]; then
sensorname=$(cat "${sensor}/name")
if [ "$sensorname" != "${NAME}" ]; then
continue
fi
prefix="sensors-weather"
suffix="-${name}"
suffix="-${sensorname}"
declare -A data
data[temperature]=$(echo "scale=2;$(cat ${sensor}/in_temp_input)/1000" | bc )
data[pressure]=$(echo "scale=2;$(cat ${sensor}/in_pressure_input)*10/1" | bc)
@ -29,9 +33,9 @@ while true; do
echo "PUTVAL $HOSTNAME/${prefix}/${key}${suffix} interval=$INTERVAL N:${data[$key]}"
done
domoticz_data="${data[temperature]};${data[humidity]};$(humidity_to_comfort ${data[humidity]});${data[pressure]};0"
SEND="${DOMOTICZ_URL}${domoticz_data}"
domoticz_send ${SEND}
mymqtt_update "${ID}" "${TEM_ID}" "${V_TEMP}" "${data[temperature]}"
mymqtt_update "${ID}" "${HUM_ID}" "${V_HUM}" "${data[humidity]}"
mymqtt_update "${ID}" "${BAR_ID}" "${V_PRESSURE}" "${data[pressure]}"
done
sleep "$INTERVAL"

View file

@ -2,33 +2,41 @@
source /usr/local/lib/collectd/_functions.sh
HOSTNAME="${COLLECTD_HOSTNAME:-$(hostname -f)}"
INTERVAL="${COLLECTD_INTERVAL:-60}"
DOMOTICZ_PORT="${2:-8080}"
DOMOTICZ_IDX="${1:-1}"
DOMOTICZ_URL="http://${HOSTNAME}:${DOMOTICZ_PORT}/json.htm?type=command&param=udevice&idx=${DOMOTICZ_IDX}&nvalue=0&svalue="
ID="60"
NAME="si1145"
i2cdev=$(dmesg | grep 'connected i2c-tiny-usb device' | head -n1 | sed -r 's/.*\s+i2c-([0-9]+).*/\1/')
echo "si1145 0x60" > /sys/bus/i2c/devices/i2c-${i2cdev}/new_device
test_i2c_device "${ID}" "${NAME}"
UV_ID="1"
IR_ID="2"
LI_ID="3"
mymqtt_init "${ID}" "${UV_ID}" "${S_UV}" "SI1145 UV"
mymqtt_init "${ID}" "${IR_ID}" "${S_LIGHT_LEVEL}" "SI1145 IR"
mymqtt_init "${ID}" "${LI_ID}" "${S_LIGHT_LEVEL}" "SI1145 LIGHT"
while true; do
for sensor in /sys/bus/iio/devices/iio\:device*; do
name=$(cat "${sensor}/name")
if [ "$name" != "si1145" ]; then
sensorname=$(cat "${sensor}/name")
if [ "$sensorname" != "${NAME}" ]; then
continue
fi
prefix="sensors-weather"
suffix=""
suffix="-${sensorname}"
declare -A data
data[gauge-ir]=$(cat ${sensor}/in_intensity_ir_raw)
data[gauge-visible]=$(cat ${sensor}/in_intensity_raw)
data[gauge-uv]=$(cat ${sensor}/in_uvindex_raw)
data[ir]=$(cat ${sensor}/in_intensity_ir_raw)
data[light]=$(cat ${sensor}/in_intensity_raw)
data[uv]=$(cat ${sensor}/in_uvindex_raw)
for key in "${!data[@]}"; do
echo "PUTVAL $HOSTNAME/${prefix}/${key}${suffix} interval=$INTERVAL N:${data[$key]}"
done
mymqtt_update "${ID}" "${UV_ID}" "${V_UV}" "${data[uv]}"
mymqtt_update "${ID}" "${IR_ID}" "${V_LEVEL}" "${data[ir]}"
mymqtt_update "${ID}" "${LI_ID}" "${V_LEVEL}" "${data[light]}"
done
sleep "$INTERVAL"
done

View file

@ -6,9 +6,17 @@ HOSTNAME="${COLLECTD_HOSTNAME:-$(hostname -f)}"
INTERVAL="${COLLECTD_INTERVAL:-60}"
DOMOTICZ_PORT="${2:-8080}"
DOMOTICZ_IDX="${1:-1}"
DOMOTICZ_URL="http://${HOSTNAME}:${DOMOTICZ_PORT}/json.htm?type=command&param=udevice&idx=${DOMOTICZ_IDX}&nvalue=0&svalue="
DOMOTICZ_URL="http://${HOSTNAME}:${DOMOTICZ_PORT}/json.htm?type=command&param=udevice"
IFS=';' read -ra KEYVALS <<< "${DOMOTICZ_IDX}"
declare -A idx
for item in "${KEYVALS[@]}"; do
idx[${item%=*}]=${item##*=}
done
while true; do
# battery
ac="$(cat /sys/devices/platform/smapi/ac_connected)"
for battery in /sys/devices/platform/smapi/BAT*; do
@ -16,7 +24,8 @@ while true; do
continue
fi
prefix="thinkpad-$(basename $battery | tr '[:upper:]' '[:lower:]')"
bname="$(basename $battery | tr '[:upper:]' '[:lower:]')"
prefix="thinkpad-${bname}"
suffix=""
declare -A data
data[gauge-ac_connected]=${ac}
@ -27,12 +36,50 @@ while true; do
data[temperature-temperature]=$(echo "scale=2;$(cat ${battery}/temperature)/1000" | bc)
data[voltage-voltage]=$(echo "scale=2;$(cat ${battery}/voltage)/1000" | bc)
data[power-power]=$(echo "scale=2;$(cat ${battery}/power_now)/1000" | bc)
data[current-current]==$(echo "scale=2;$(cat ${battery}/current_now)/1000" | bc)
data[current-current]=$(echo "scale=2;$(cat ${battery}/current_now)/1000" | bc)
for key in "${!data[@]}"; do
echo "PUTVAL $HOSTNAME/${prefix}/${key}${suffix} interval=$INTERVAL N:${data[$key]}"
done
value=${data[$key]}
echo "PUTVAL $HOSTNAME/${prefix}/${key}${suffix} interval=$INTERVAL N:${value}"
if [ ! ${idx[$key]+test} ]; then
continue
fi
sensor=${idx[$key]}
SEND="${DOMOTICZ_URL}&idx=${sensor}&battery=${data[percent-percent]}"
if grep -q "ac_connected" <<< "${key}"; then
svalue="ONLINE"
if [ "$value" == "0" ]; then
value=4
svalue="OFFLINE"
fi
SEND="${SEND}&nvalue=${value}&svalue=${svalue}"
else
SEND="${SEND}&nvalue=0&svalue=${value}"
fi
domoticz_send ${SEND}
done
done
# fan
prefix="thinkpad-fan"
suffix=""
declare -A data
status="$(cat /proc/acpi/ibm/fan)"
data[fanspeed-speed]=$(grep 'speed:' <<< "$status" | awk '{print $2}')
data[gauge-fanlevel]=$(grep 'level:' <<< "$status" | awk '{print $2}')
for key in "${!data[@]}"; do
value=${data[$key]}
echo "PUTVAL $HOSTNAME/${prefix}/${key}${suffix} interval=$INTERVAL N:${data[$key]}"
if [ ! ${idx[$key]+test} ]; then
continue
fi
sensor=${idx[$key]}
SEND="${DOMOTICZ_URL}&idx=${sensor}&battery=${data[percent-percent]}&svalue=${value}"
domoticz_send ${SEND}
done
sleep "$INTERVAL"