adds sending of sensor values to homeassistant via mqtt
This commit is contained in:
5
mqtt/Makefile
Normal file
5
mqtt/Makefile
Normal file
@@ -0,0 +1,5 @@
|
||||
systemd:
|
||||
sudo cp ./ina219.service /etc/systemd/system
|
||||
sudo cp ./ina219.timer /etc/systemd/system
|
||||
sudo systemctl enable ina219.timer
|
||||
sudo systemctl start ina219.timer
|
||||
9
mqtt/ina219.service
Normal file
9
mqtt/ina219.service
Normal file
@@ -0,0 +1,9 @@
|
||||
[Unit]
|
||||
Description=Read sensor values
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
User=root
|
||||
Group=root
|
||||
WorkingDirectory=/root/git/rpi-ina219/mqtt
|
||||
ExecStart=/root/git/rpi-ina219/mqtt/send
|
||||
12
mqtt/ina219.timer
Normal file
12
mqtt/ina219.timer
Normal file
@@ -0,0 +1,12 @@
|
||||
[Unit]
|
||||
Description=Periodically update sensor readings
|
||||
|
||||
[Timer]
|
||||
#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
|
||||
32
mqtt/send
Executable file
32
mqtt/send
Executable file
@@ -0,0 +1,32 @@
|
||||
#!/bin/bash
|
||||
|
||||
ROUNDS=5
|
||||
|
||||
VOLTAGE=0
|
||||
CURRENT=0
|
||||
POWER=0
|
||||
|
||||
for i in $(seq ${ROUNDS}); do
|
||||
RESULT=$(/usr/bin/ina219)
|
||||
|
||||
VOLTAGE_RAW=$(echo $RESULT | /bin/sed 's/\([0-9]\+\)mV \+\([0-9\.]\+\)mA/\1/')
|
||||
VOLTAGE=$(echo "scale=3; $VOLTAGE+$VOLTAGE_RAW" | /usr/bin/bc)
|
||||
|
||||
CURRENT_RAW=$(echo $RESULT | /bin/sed 's/\([0-9]\+\)mV \+\([0-9\.]\+\)mA/\2/')
|
||||
CURRENT=$(echo "scale=3; $CURRENT+$CURRENT_RAW" | /usr/bin/bc)
|
||||
|
||||
done
|
||||
|
||||
VOLTAGE=$(echo "scale=0; $VOLTAGE/$ROUNDS" | /usr/bin/bc)
|
||||
CURRENT=$(echo "scale=0; $CURRENT/$ROUNDS" | /usr/bin/bc)
|
||||
POWER=$(echo "scale=0; $VOLTAGE*$CURRENT/1000" | /usr/bin/bc)
|
||||
|
||||
|
||||
echo "voltage.value $VOLTAGE"
|
||||
echo "current.value $CURRENT"
|
||||
echo "power.value $POWER"
|
||||
|
||||
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