Skip to content

Instantly share code, notes, and snippets.

@dmslabsbr
Created November 4, 2023 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
@dmslabsbr
Copy link
Author

Use:
chmod +x ha_post_temp.sh

Create a cron job to run the script:
Execute the command crontab -e.

Add to the last line (in this configuration, the sensors will be updated every minute):
*/1 * * * * /usr/ha_post_temp.sh

or

Add to the last line (in this configuration, the sensors will be updated every 10 minutes):
*/10 * * * * /usr/ha_post_temp.sh

Restart the cron service:
sudo systemctl restart cron.service
or
sudo systemctl restart crond.service

Check the cron service
sudo systemctl status cron.service

PS:
device info not working

@KrOnAsK
Copy link

KrOnAsK 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!

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