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

Murmelbaerchen69 commented Dec 23, 2024

Hello,
when I run the script I get the message: "Invalid JSON specified".
I can query the temperatures in Putty under Proxmox.
Where is the error? I can't find it.

Putty:
root@esmltcm:/usr/local/bin# sensors | grep 'temp1:' | awk '{print $2}' | sed 's/+//;s/°C//'
27.8
root@esmltcm:/usr/local/bin# sensors | grep 'Package id 0' | awk '{print $4}' | sed 's/+//;s/°C//'
25.0
root@esmltcm:/usr/local/bin# sensors | grep 'Core 1:' | awk '{print $3}' | sed 's/+//;s/°C//'
22.0
root@esmltcm:/usr/local/bin# sensors | grep 'Core 2:' | awk '{print $3}' | sed 's/+//;s/°C//'
25.0
root@esmltcm:/usr/local/bin# sensors | grep 'Core 3:' | awk '{print $3}' | sed 's/+//;s/°C//'
25.0
root@esmltcm:/usr/local/bin# sensors | grep 'Core 4:' | awk '{print $3}' | sed 's/+//;s/°C//'
24.0
root@esmltcm:/usr/local/bin# tempha.sh
{"message":"Invalid JSON specified."}{"message":"Invalid JSON specified."}{"message":"Invalid JSON specified."}{"message":"Invalid JSON specified."}{"message":"Invalid JSON specified."}{"message":"Invalid JSON specified."}root@esmltcm:/usr/local/bin#

The script:
#!/bin/bash

Home Assistant Settings

url_base="http://192.168.10.203:8123/api/states"
token="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"

Server name

srv_name="esmltcm"

Constants for device info

DEVICE_IDENTIFIERS='["esmltcm"]'
DEVICE_NAME="ThinkCentre M720q"
DEVICE_MANUFACTURER="Lenovo"
DEVICE_MODEL="M720q"

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 'Package id 0' | awk '{print $4}' | sed 's/+//;s/°C//')
send_to_ha "sensor.${srv_name}_cpu_temperatur" "${cpu_temp}" "CPU Package Temperatur" "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_temperatur" "${chipset_temp}" "Chipset Temperatur" "mdi:chip" "${srv_name}_chipset_temp"
fi

Send Core 1 temperature (adjust device if necessary)

core1_temp=$(sensors | grep 'Core 1' | awk '{print $3}' | sed 's/+//;s/°C//')
if [[ $core1_temp != "" ]]; then
send_to_ha "sensor.${srv_name}_core1_temperatur" "${core1_temp}" "Core1 Temperatur" "mdi:cpu-64-bit" "${srv_name}_core1_temp"
fi

Send Core 2 temperature (adjust device if necessary)

core2_temp=$(sensors | grep 'Core 2' | awk '{print $3}' | sed 's/+//;s/°C//')
if [[ $core2_temp != "" ]]; then
send_to_ha "sensor.${srv_name}_core2_temperatur" "${core2_temp}" "Core2 Temperatur" "mdi:cpu-64-bit" "${srv_name}_core2_temp"
fi

Send Core 3 temperature (adjust device if necessary)

core3_temp=$(sensors | grep 'Core 3' | awk '{print $3}' | sed 's/+//;s/°C//')
if [[ $core3_temp != "" ]]; then
send_to_ha "sensor.${srv_name}_core3_temperatur" "${core3_temp}" "Core3 Temperatur" "mdi:cpu-64-bit" "${srv_name}_core3_temp"
fi

Send Core 4 temperature (adjust device if necessary)

core4_temp=$(sensors | grep 'Core 4' | awk '{print $3}' | sed 's/+//;s/°C//')
if [[ $core4_temp != "" ]]; then
send_to_ha "sensor.${srv_name}_core4_temperatur" "${core4_temp}" "Core4 Temperatur" "mdi:cpu-64-bit" "${srv_name}_core4_temp"
fi

@Murmelbaerchen69
Copy link

Murmelbaerchen69 commented Dec 30, 2024

i found the Error. For anyone else who has the same problem, it's because of this line:

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}}"

The problem is: "unit_of_measurement":"°C" this must be changed as follows: "unit_of_measurement":"\u00B0C"
I think he can't handle the ° sign

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