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
Copy link

ghost commented Dec 27, 2023

Hi, I adjusted my devices and the commands work manually ( like when i do sensors | grep 'Tctl:' it gives me a Temp output) but in the sript returns 404: Not found

This is my Script: `#!/bin/bash

Home Assistant Settings

url_base="http://192.168.178.175:8123/a/api/states"
token="

Server name

srv_name="pve"

Constants for device info

DEVICE_IDENTIFIERS='["3400G"]'
DEVICE_NAME="AMD 3400G"
DEVICE_MANUFACTURER="AMD"
DEVICE_MODEL="AMD 3400G"

Function to send data to 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}"
}

Send CPU package temperature

cpu_temp=$(sensors | grep 'Tctl:' | 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"

Send Chipset temperature (adjust device if necessary)

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

Send NVMe/SSD composite temperature (adjust device if necessary)

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

Send GPU temperature (adjust device if necessary)

gpu_temp=$(sensors | grep 'edge:' | 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`

and when I run it after I made it executable I get 404: Not Found404: Not Found404: Not Found404: Not Found

@Pharizna
Copy link

Hi. I need some help ... really, a lot of :(

The output of sensors in my OMV in the CM3588 is "strange":


npu_thermal-virtual-0
Adapter: Virtual device
temp1:        +42.5°C  (crit = +115.0°C)

test_battery-virtual-0
Adapter: Virtual device
in0:           4.00 mV 
temp:         +26.0°C  
curr1:        -2.00 mA (avg =  -0.00 A)

center_thermal-virtual-0
Adapter: Virtual device
temp1:        +42.5°C  (crit = +115.0°C)

bigcore1_thermal-virtual-0
Adapter: Virtual device
temp1:        +43.5°C  (crit = +115.0°C)

soc_thermal-virtual-0
Adapter: Virtual device
temp1:        +42.5°C  (crit = +115.0°C)

test_battery-virtual-0
Adapter: Virtual device
ERROR: Can't get value of subfeature in0_input: Kernel interface error
in0:              N/A  
temp1:        +26.0°C  
ERROR: Can't get value of subfeature curr1_input: Kernel interface error
ERROR: Can't get value of subfeature curr1_average: Kernel interface error
curr1:            N/A  (avg =  +0.00 A)

tcpm_source_psy_6_0022-i2c-6-22
Adapter: rk3x-i2c
in0:           0.00 V  (min =  +0.00 V, max =  +0.00 V)
curr1:         0.00 A  (max =  +0.00 A)

gpu_thermal-virtual-0
Adapter: Virtual device
temp1:        +42.5°C  (crit = +115.0°C)

littlecore_thermal-virtual-0
Adapter: Virtual device
temp1:        +43.5°C  (crit = +115.0°C)

bigcore0_thermal-virtual-0
Adapter: Virtual device
temp1:        +43.5°C  (crit = +115.0°C)

How do I have to modify the previous script to be able to "extract" the temp1 from the bigcore1_thermal-virtual-0 temperature, for example?

@Pharizna
Copy link

I found the solution with the help of Microsoft copilot :)

cpu_temp=$(sensors | awk '/bigcore1_thermal-virtual-0/{getline; getline; print $2}' | awk -F'[+°C]' '{print $2}')

@dmslabsbr
Copy link
Author

I found the solution with the help of Microsoft copilot :)

cpu_temp=$(sensors | awk '/bigcore1_thermal-virtual-0/{getline; getline; print $2}' | awk -F'[+°C]' '{print $2}')

Thank you!

@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