Skip to content

Instantly share code, notes, and snippets.

@dmslabsbr
Created November 4, 2023 01:59
Show Gist options
  • Save dmslabsbr/08970d068e2e021312055e7560bcac9a to your computer and use it in GitHub Desktop.
Save dmslabsbr/08970d068e2e021312055e7560bcac9a to your computer and use it in GitHub Desktop.
Bash scripts to send pve sensors temperature to home assistant
#!/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
@Pastaloverzzz
Copy link

Pastaloverzzz commented May 17, 2024

I setup this script a few weeks back but today i also installed my smtp server on proxmox and i constantly get emails from crondaemon with:

% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 100 825 100 509 100 316 274k 170k --:--:-- --:--:-- --:--:-- 805k
{"entity_id":"sensor.pve_cpu_temperature","state":"29.0","attributes":{"friendly_name":"CPU Package Temperature","icon":"mdi:cpu-64-bit","state_class":"measurement","unit_of_measurement":"°C","device_class":"temperature","unique_id":"pve_cpu_temp"},"last_changed":"2024-05-17T21:34:01.426719+00:00","last_reported":"2024-05-17T21:34:01.426719+00:00","last_updated":"2024-05-17T21:34:01.426719+00:00","context":{"id":".......","parent_id":null,"user_id":"........"}}

EDIT: I FOUND THE SOLUTION: (i will leave my comment here in case anyone else has this problem)
First you need to open the crontab
crontab -e

Then you add >/dev/null 2>&1 behind the task
*/1 * * * * /root/ha_post_temp.sh >/dev/null 2>&1

Then save it(ctrl+X/yes) and restart it again
systemctl restart cron.service

@Rust-Shch
Copy link

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:

  • CPU Temp
  • Fan RPM
  • Battery voltage
  • Other available info (optional, I willtry to manage myself)

Thank you in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment