Created
November 4, 2023 01:59
-
-
Save dmslabsbr/08970d068e2e021312055e7560bcac9a to your computer and use it in GitHub Desktop.
Bash scripts to send pve sensors temperature to home assistant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Configurações do Home Assistant | |
url_base="http://192.168.50.201:8123/api/states" | |
token="lzRSxMt1dtk" | |
# Nome do servidor | |
srv_name="pve" | |
# Constants for device info | |
DEVICE_IDENTIFIERS='["pve_server"]' | |
DEVICE_NAME="PVE Server" | |
DEVICE_MANUFACTURER="HP" | |
DEVICE_MODEL="Prodesk" | |
# Função para enviar dados ao Home Assistant | |
send_to_ha() { | |
local sensor_name=$1 | |
local temperature=$2 | |
local friendly_name=$3 | |
local icon=$4 | |
local unique_id=$5 | |
local url="${url_base}/${sensor_name}" | |
local device_info="{\"identifiers\":${DEVICE_IDENTIFIERS},\"name\":\"${DEVICE_NAME}\",\"manufacturer\":\"${DEVICE_MANUFACTURER}\",\"model\":\"${DEVICE_MODEL}\"}" | |
local payload="{\"state\":\"${temperature}\",\"attributes\": {\"friendly_name\":\"${friendly_name}\",\"icon\":\"${icon}\",\"state_class\":\"measurement\",\"unit_of_measurement\":\"°C\",\"device_class\":\"temperature\",\"unique_id\":\"${unique_id}\"},\"device\":${device_info}}" | |
curl -X POST -H "Authorization: Bearer ${token}" -H 'Content-type: application/json' --data "${payload}" "${url}" | |
} | |
# Enviar temperatura do pacote da CPU | |
cpu_temp=$(sensors | grep 'Package id 0' | awk '{print $4}' | sed 's/+//;s/°C//') | |
send_to_ha "sensor.${srv_name}_cpu_temperature" "${cpu_temp}" "CPU Package Temperature" "mdi:cpu-64-bit" "${srv_name}_cpu_temp" | |
# Enviar temperatura do Chipset (ajustar o dispositivo se necessário) | |
chipset_temp=$(sensors | grep 'temp1:' | awk '{print $2}' | sed 's/+//;s/°C//') | |
if [[ $chipset_temp != "" ]]; then | |
send_to_ha "sensor.${srv_name}_chipset_temperature" "${chipset_temp}" "Chipset Temperature" "mdi:chip" "${srv_name}_chipset_temp" | |
fi | |
# Enviar temperatura composta do NVMe/SSD (ajustar dispositivo se necessário) | |
nvme_temp=$(sensors | grep 'Composite' | head -1 | awk '{print $2}' | sed 's/+//;s/°C//') | |
if [[ $nvme_temp != "" ]]; then | |
send_to_ha "sensor.${srv_name}_nvme_temperature" "${nvme_temp}" "NVMe/SSD Temperature" "mdi:harddisk" "${srv_name}_nvme_temp" | |
fi | |
# Enviar temperatura da GPU (ajustar o dispositivo se necessário) | |
gpu_temp=$(sensors | grep 'GPU Temp' | awk '{print $2}' | sed 's/+//;s/°C//') | |
if [[ $gpu_temp != "" ]]; then | |
send_to_ha "sensor.${srv_name}_gpu_temperature" "${gpu_temp}" "GPU Temperature" "mdi:gpu" "${srv_name}_gpu_temp" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!
My output in manual mode:
root@shchapovo:~# sensors
coretemp-isa-0000
Adapter: ISA adapter
Package id 0: +41.0°C (high = +105.0°C, crit = +105.0°C)
Core 0: +40.0°C (high = +105.0°C, crit = +105.0°C)
Core 1: +40.0°C (high = +105.0°C, crit = +105.0°C)
thinkpad-isa-0000
Adapter: ISA adapter
fan1: 0 RPM
CPU: +40.0°C
GPU: +0.0°C
temp3: +0.0°C
temp4: +0.0°C
temp5: +0.0°C
temp6: +0.0°C
temp7: +0.0°C
temp8: +0.0°C
BAT0-acpi-0
Adapter: ACPI interface
in0: 12.24 V
iwlwifi_1-virtual-0
Adapter: Virtual device
temp1: N/A
pch_wildcat_point-virtual-0
Adapter: Virtual device
temp1: +41.0°C
acpitz-acpi-0
Adapter: ACPI interface
temp1: +40.0°C
How to modify this script to get:
Thank you in advance.