adds first shot add providing 1 wire temps to homeassistant
This commit is contained in:
11
mqtt/1wire
Normal file
11
mqtt/1wire
Normal file
@@ -0,0 +1,11 @@
|
||||
alias_001415daa6ff=Vorlauf
|
||||
alias_000802775a88=Ext_1
|
||||
alias_0008027777d5=Ext_2
|
||||
alias_00000614a13f=Schlafzimmer_1
|
||||
alias_00000614a9dd=Schlafzimmer_2
|
||||
alias_00042c37a7ff=Wohnzimmer_1
|
||||
alias_000802774f41=Wohnzimmer_2
|
||||
alias_00042e0076ff=Arbeitszimmer
|
||||
alias_000006140045=Bad
|
||||
alias_0000061451b3=Küche
|
||||
alias_0008027774a6=Esszimmer
|
||||
5
mqtt/Makefile
Normal file
5
mqtt/Makefile
Normal file
@@ -0,0 +1,5 @@
|
||||
systemd:
|
||||
sudo cp ./ds18b20.service /etc/systemd/system
|
||||
sudo cp ./ds18b20.timer /etc/systemd/system
|
||||
sudo systemctl enable ds18b20.timer
|
||||
sudo systemctl start ds18b20.timer
|
||||
9
mqtt/ds18b20.service
Normal file
9
mqtt/ds18b20.service
Normal file
@@ -0,0 +1,9 @@
|
||||
[Unit]
|
||||
Description=Read sensor values
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
User=root
|
||||
Group=root
|
||||
WorkingDirectory=/root/git/rpi-w1-ds18b20/mqtt
|
||||
ExecStart=/root/git/rpi-w1-ds18b20/mqtt/send
|
||||
17
mqtt/ds18b20.timer
Normal file
17
mqtt/ds18b20.timer
Normal file
@@ -0,0 +1,17 @@
|
||||
[Unit]
|
||||
Description=Periodically update sensor readings
|
||||
|
||||
[Timer]
|
||||
# Verhindert, dass systemd den Timer minutengenau bündelt
|
||||
AccuracySec=1s
|
||||
# Wählt eine zufällige Verzögerung im angegebenen Intervall
|
||||
RandomizedDelaySec=15s
|
||||
|
||||
#OnCalendar=*-*-* 10:45:00
|
||||
#OnBootSec=30sec
|
||||
#OnUnitActiveSec=30sec
|
||||
OnCalendar=*:0/1
|
||||
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
127
mqtt/mqtt
Executable file
127
mqtt/mqtt
Executable file
@@ -0,0 +1,127 @@
|
||||
#!/bin/bash
|
||||
|
||||
# MQTT configuration attributes
|
||||
# https://www.home-assistant.io/integrations/sensor.mqtt/
|
||||
# https://www.home-assistant.io/integrations/switch.mqtt/
|
||||
|
||||
# Explanation
|
||||
# https://www.youtube.com/watch?v=VHiCtZqllU8
|
||||
# https://resinchemtech.blogspot.com/2023/12/mqtt-auto-discovery.html
|
||||
|
||||
# Prerequesite
|
||||
#apt install mosquitto-clients
|
||||
|
||||
LC_NAME="${DEVICE_NAME,,}"
|
||||
|
||||
# Device attriutes
|
||||
DEVICE_ID="${LC_NAME// /_}"
|
||||
DEVICE_MANUFACTURER="custom"
|
||||
DEVICE_MODEL="${DEVICE_NAME}" #"custom"
|
||||
|
||||
# Sensor attributes
|
||||
case ${SENSOR_UNIT} in
|
||||
"W"|"mW")
|
||||
SENSOR_ICON="mdi:lightning-bolt"
|
||||
SENSOR_TYPE="power"
|
||||
;;
|
||||
"V"|"mV")
|
||||
SENSOR_ICON="mdi:sine-wave"
|
||||
SENSOR_TYPE="voltage"
|
||||
;;
|
||||
"A"|"mA")
|
||||
SENSOR_ICON="mdi:current-dc"
|
||||
SENSOR_TYPE="current"
|
||||
;;
|
||||
"C")
|
||||
SENSOR_UNIT="°C"
|
||||
;;&
|
||||
"°C")
|
||||
SENSOR_ICON="mdi:thermometer"
|
||||
SENSOR_TYPE="temperature"
|
||||
;;
|
||||
"%")
|
||||
SENSOR_ICON="mdi:water-percent"
|
||||
SENSOR_TYPE="humidity"
|
||||
;;
|
||||
*)
|
||||
echo "Unknown sensor unit: ${SENSOR_UNIT}" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
SENSOR_ID="${DEVICE_ID}_${SENSOR_TYPE}"
|
||||
SENSOR_EXPIRE_AFTER_S="600"
|
||||
|
||||
# MQTT attributes
|
||||
MQTT_CONFIG_TOPIC="homeassistant/sensor/${SENSOR_ID}/config"
|
||||
MQTT_STATE_TOPIC="${LC_NAME// //}/${SENSOR_NAME}/state"
|
||||
|
||||
|
||||
|
||||
function send {
|
||||
TOPIC=$1
|
||||
PAYLOAD=$2
|
||||
echo "Sending payload to ${MQTT_BROKER}:${MQTT_PORT}/${TOPIC} ..."
|
||||
echo "TOPIC=$TOPIC"
|
||||
echo "PAYLOAD=$PAYLOAD"
|
||||
if [ -n "${MQTT_USERNAME}" ] && [ -n "${MQTT_PASSWORD}" ]; then
|
||||
mosquitto_pub -h "${MQTT_BROKER:=localhost}" -p "${MQTT_PORT:=1883}" -u "${MQTT_USERNAME}" -P "${MQTT_PASSWORD}" \
|
||||
-t "$TOPIC" \
|
||||
-m "$PAYLOAD"
|
||||
else
|
||||
mosquitto_pub -h "${MQTT_BROKER:=localhost}" -p "${MQTT_PORT:=1883}" \
|
||||
-t "$TOPIC" \
|
||||
-m "$PAYLOAD"
|
||||
fi
|
||||
}
|
||||
|
||||
function config {
|
||||
send "${MQTT_CONFIG_TOPIC}" "$1"
|
||||
}
|
||||
|
||||
function state {
|
||||
send "${MQTT_STATE_TOPIC}" "$1"
|
||||
}
|
||||
|
||||
function setupEntity {
|
||||
config "{\
|
||||
\"name\": \"${SENSOR_NAME}\",\
|
||||
\"unique_id\": \"${SENSOR_ID}\",\
|
||||
\"device_class\": \"${SENSOR_TYPE}\",\
|
||||
\"unit_of_measurement\": \"${SENSOR_UNIT}\",\
|
||||
\"state_topic\": \"${MQTT_STATE_TOPIC}\",\
|
||||
\"expire_after\": \"${SENSOR_EXPIRE_AFTER_S}\",\
|
||||
\"icon\": \"${SENSOR_ICON}\",\
|
||||
\"device\": {\
|
||||
\"name\": \"${DEVICE_NAME}\",\
|
||||
\"identifiers\": [\"${DEVICE_ID}\"],\
|
||||
\"manufacturer\": \"${DEVICE_MANUFACTURER}\",\
|
||||
\"model\": \"${DEVICE_MODEL}\"\
|
||||
}}"
|
||||
|
||||
#\"firendly_name\": \"${NAME} power\",\
|
||||
# "hw_version":"1.02",
|
||||
# "sw_version":"2.45",
|
||||
# "configuration_url":"http://192.168.1.226"
|
||||
}
|
||||
|
||||
function deleteEntity {
|
||||
config ""
|
||||
}
|
||||
|
||||
if [ "$1" == "r" ]; then
|
||||
deleteEntity
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [ -n "$1" ]; then
|
||||
setupEntity
|
||||
state "$1"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [ -z "$1" ]; then
|
||||
setupEntity
|
||||
exit 0
|
||||
fi
|
||||
|
||||
exit 1
|
||||
38
mqtt/send
Executable file
38
mqtt/send
Executable file
@@ -0,0 +1,38 @@
|
||||
#!/bin/bash
|
||||
|
||||
source ./1wire
|
||||
|
||||
FILES=$(find -L /sys/bus/w1/devices/ -maxdepth 2 -name temperature)
|
||||
|
||||
SENSORS=()
|
||||
for f in $FILES
|
||||
do
|
||||
s=${f%/*}
|
||||
s=${s##*-}
|
||||
v=$(cat $f)
|
||||
|
||||
SENSORS+=("${s} ${v}")
|
||||
done
|
||||
|
||||
for ROW in "${SENSORS[@]}"; do
|
||||
ENTRY=( $ROW )
|
||||
SENSOR=${ENTRY[0]}
|
||||
VALUE=${ENTRY[1]/#[0-9][0-9]/&.}
|
||||
|
||||
# Overrides SENSOR - Example: env.sensor_0_0 Batterie
|
||||
ENV_SENSOR_VAR_NAME="alias_${SENSOR}"
|
||||
ENV_SENSOR_NAME=${!ENV_SENSOR_VAR_NAME}
|
||||
|
||||
if [ -n "${ENV_SENSOR_NAME}" ]; then
|
||||
echo "${ENV_SENSOR_NAME}.value ${VALUE}"
|
||||
else
|
||||
echo "${SENSOR}.value ${VALUE}"
|
||||
fi
|
||||
done
|
||||
|
||||
|
||||
|
||||
#SENSOR_NAME="voltage" SENSOR_UNIT="mV" DEVICE_NAME="heat-pi ina219" ./mqtt $VOLTAGE
|
||||
#SENSOR_NAME="current" SENSOR_UNIT="mA" DEVICE_NAME="heat-pi ina219" ./mqtt $CURRENT
|
||||
#SENSOR_NAME="power" SENSOR_UNIT="mW" DEVICE_NAME="heat-pi ina219" ./mqtt $POWER
|
||||
|
||||
Reference in New Issue
Block a user