Jump to content

Export the Disk-Temperatures via MQTT or anything else


Go to solution Solved by Milvus,

Recommended Posts

Hey,

I realized that my disks are getting quite hot sometimes, so I want to cool them down with an external Fan, by automatically turning it on via my Smarthome (iobroker on another hardware than the UNRAID system). To do so, I need the disk temperatures exported somehow to my iobroker. What I found out so far is, that the temperatures are nowhere to be found in a file, but can only be read via a smartctl command:

#!/bin/bash

for dev in /dev/sd?; do

  temp=$(smartctl -a "$dev" | grep -i 'Temperature_Celsius' | awk '{print $10}')

  if [ -n "$temp" ]; then

     echo "$dev: ${temp}°C"

  fi

done

I also found out, that dockers generally don't have access to the smartctl commands, which makes sense. I already set up a script via User Scripts App, but I have no clue, how now to send it somehow? 

Can anybody help? Please explain your answer a bit, as I am really a newbie.
Thanks in advance!

Link to comment

I'm even a greater newbie, but could "glances" help pass the information? I've read (not tested) that UnRAID supports it well. I've thought about (im)putting the data to my Home Assistant.

 

Mr. Grey.

 

 

Link to comment
Posted (edited)

Just try something, I surprise HA could get those CPU & disk temperature directly. I use HA docker and under HA console, you can easy got the reading as below. So, don't need MQTT.

 

/config # cat /sys/class/hwmon/hwmon3/temp1_input
41000
/config # cat /sys/class/hwmon/hwmon1/temp1_input
44850
/config # cat /sys/class/hwmon/hwmon1/temp1_input
44850
/config # cat /sys/class/hwmon/hwmon1/temp1_input
45850
/config # cat /sys/class/hwmon/hwmon1/temp1_input
44850
/config # cat /sys/class/hwmon/hwmon1/temp1_input
44850
/config # cat /sys/class/hwmon/hwmon0/temp1_input
47125
/config # cat /sys/class/hwmon/hwmon0/temp1_input
46875
/config # cat /sys/class/hwmon/hwmon0/temp1_input
46625
/config # cat /sys/class/hwmon/hwmon0/temp1_input
46500

 

When you type sensors in Unraid console, it will show something as below

 

root@5600H:~# sensors
amdgpu-pci-0500
Adapter: PCI adapter
vddgfx:        1.02 V  
vddnb:       781.00 mV 
edge:         +45.0°C  
PPT:          13.00 W  

k10temp-pci-00c3
Adapter: PCI adapter
Tctl:         +46.8°C  

drivetemp-scsi-2-0
Adapter: SCSI adapter
temp1:        +41.0°C  

nvme-pci-0100
Adapter: PCI adapter
Composite:    +45.9°C  (low  = -273.1°C, high = +81.8°C)
                       (crit = +84.8°C)
Sensor 1:     +45.9°C  (low  = -273.1°C, high = +65261.8°C)

 

Below is the methods how you know the path

 

https://www.baeldung.com/linux/hdd-ssd-temperature

image.png.de23b2edc0759f27311d9ae589ee099b.png

 

image.png.93a037a6431120f5811912940c913d3e.png

 

And last is convert it to sensors

 

https://www.home-assistant.io/integrations/command_line/

image.png.1b0eee028bcd61a3a20e07962b370b30.png

 

Edited by Vr2Io
Link to comment
Posted (edited)

Thank you both very much! Very cool ideas! Haven't noticed this glances yet, as I am using Dynamix tools for internal surveillance. Home-Assistant as a local solution on the unraid-system to send it then via a mqtt to the other hardware might also lead to a point. I will definitely check that out and report in the next one and a half weeks!

Thank you two!

Edited by Milvus
Link to comment
  • 2 weeks later...

Alright, sorry for the delay, I tried glances now, but it unfortunately does not tell the temperatures of the Disks. So I went to the Home Assistant idea: Installed a Docker, did a bunch to get it to work, but it refuses to show me anything in HA. 

Here is what I've done: 
1. setup a sh file which would get me the temperatures. File is in the docker container. Content:

#!/bin/bash
devices=(/dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde)

for dev in "${devices[@]}"; do
    temp=""
    
    # Try different device types
    for type in sat usbsunplus scsi; do
        smartctl_output=$(smartctl -a -d $type "$dev" 2>/dev/null)
        if echo "$smartctl_output" | grep -qi 'Temperature_Celsius'; then
            temp=$(echo "$smartctl_output" | grep -i 'Temperature_Celsius' | awk '{print $10}')
            if [ -n "$temp" ]; then
                break
            fi
        fi
    done

    if [ -z "$temp" ]; then
        echo "$dev: N/A"
    else
        echo "$dev: ${temp}°C"
    fi
done

 

2. edited the configuration.yaml with following:

sensor:
  - platform: command_line
    name: Disk sda Temperature
    command: "/config/disk_temps.sh | grep '/dev/sda' | awk -F: '{print $2}' | xargs"
    unit_of_measurement: "°C"
    scan_interval: 300
    value_template: "{{ 'unavailable' if value == 'N/A' else value }}"
    
  - platform: command_line
    name: Disk sdb Temperature
    command: "/config/disk_temps.sh | grep '/dev/sdb' | awk -F: '{print $2}' | xargs"
    unit_of_measurement: "°C"
    scan_interval: 300

  - platform: command_line
    name: Disk sdc Temperature
    command: "/config/disk_temps.sh | grep '/dev/sdc' | awk -F: '{print $2}' | xargs"
    unit_of_measurement: "°C"
    scan_interval: 300

  - platform: command_line
    name: Disk sdd Temperature
    command: "/config/disk_temps.sh | grep '/dev/sdd' | awk -F: '{print $2}' | xargs"
    unit_of_measurement: "°C"
    scan_interval: 300

  - platform: command_line
    name: Disk sde Temperature
    command: "/config/disk_temps.sh | grep '/dev/sde' | awk -F: '{print $2}' | xargs"
    unit_of_measurement: "°C"
    scan_interval: 300

 

3. restarted HomeAssistant, hoping to see the datapoints. Nothing happened. 

 

Took me also a while to make the Docker able to use smartctl. @Vr2Io, how did you manage to get the data in? Could you give a more detailed input? Would be thankful for any help!
 

Link to comment
Posted (edited)

Below are the sensor config which I input to configuration.yaml. That's all.

 

command_line:
  - sensor:
      name: CPU Temperature
      command: "cat /sys/class/hwmon/hwmon0/temp1_input"
      # If errors occur, make sure configuration file is encoded as UTF-8
      state_class: measurement
      device_class: temperature
      unit_of_measurement: °C
      value_template: "{{ value | multiply(0.001) | round(1) }}"

  - sensor:
      name: SATA Temperature
      command: "cat /sys/class/hwmon/hwmon1/temp1_input"
      # If errors occur, make sure configuration file is encoded as UTF-8
      state_class: measurement
      device_class: temperature
      unit_of_measurement: "°C"
      value_template: "{{ value | multiply(0.001) | round(1) }}"

  - sensor:
      name: Nvme Temperature
      command: "cat /sys/class/hwmon/hwmon3/temp1_input"
      # If errors occur, make sure configuration file is encoded as UTF-8
      state_class: measurement
      device_class: temperature
      unit_of_measurement: "°C"
      value_template: "{{ value | multiply(0.001) | round(1) }}"

 

It work.

 

image.thumb.png.08dc7cfb92631e0b3a9d9ba5bf546ad4.png

 

image.thumb.png.5412728e614f72998b17e610fc4d8deb.png

Edited by Vr2Io
Link to comment

Thank you for sharing the code. The problem I face is, that the temperature of the disks are not stored in a file, but need to be taken via the smartctl. But maybe I am able to create a file and use that one then. Something to try in the next week.

Thank you so far!

 

Link to comment
  • Solution
Posted (edited)

Edit 06/15/2024: editet the user-script cronjob: the original made the disks spin up. Now the  "hdparm" checks, if the disks are alive. If not, the temperature will not be taken, so the disks stay asleep.

 

Alright, I got it, with the help of ChatGPT:

 

What I did in brief words:

  • Unraid System collects the disc-temperatures and writes them into a .txt file.
  • Home Assistant Docker has access to the .txt file
  • Mosquitto Docker installed
  • Setup HomeAssistant MQTT
  • Created an automatism in HA, which takes the Info from the file and sends it via MQTT to my SmartHome
  • Smart Home evaluates, if Discs get too hot, and turns on a Fan, if temp of one disc is too high

For anyone who faces the same issue, here a small manual until sending the data via MQTT. The processing to turn on a Fan is very individual I assume, that is why I leave it out here ;-) 

 

  • create a folder "disktemps" (or any other name you like) in folder /mnt/user/appdata/
  • Install the app "User Scripts"
    1. Configure a CronJob in User scripts, with the following script:
#!/bin/bash
output_file="/mnt/user/appdata/disktemps/disk_temps.txt"
mkdir -p $(dirname "$output_file")
> "$output_file" # Datei leeren

# Liste der existierenden Geräte filtern
for dev in /dev/sd?; do
    if [ -b "$dev" ]; then
        # Prüfe, ob die Festplatte im Standby-Zustand ist
        status=$(hdparm -C "$dev" | grep -i 'standby')
        if [ -z "$status" ]; then
            temp=$(smartctl -a "$dev" | grep -i 'Temperature_Celsius' | awk '{print $10}')
            if [ -n "$temp" ]; then
                echo "$dev: ${temp}°C" >> "$output_file"
            fi
        fi
    fi
done

 

           This creates a .txt file with the disk-temps in it

 

  • Install HomeAssistant Docker
    • Add an additional Path "Add another Path, Port, Variable, Lable or Device"
    • image.png.34b42e27f1462ef47f91d743a1e78135.png
    • The host path from the Unraid system, which we have created and in which the temperatures are written by the “User Scripts” app, is therefore routed to the container path. In other words, the files in the disktemps folder become available in the container (normally containers do not have access to data outside the container).

 

  • Set Up HomeAssistant
    • Now we have to edit the “configuration.yaml” of the HomeAssistant Docker. Here we have to tell HA that we want to have a new data point in Home-Assistant in which we want to have the temperature data. Important: We do this via the Unraid console, not from the Docker! The Docker does not support Sudo or Nano.
    • Open the console in Unraid
    • Navigate to the installation folder of the HA container:
cd /mnt/user/appdata/Home-Assistant-Container

 

  • Open the configuration.yaml
nano configuration.yaml

 

  • Copy the following command one line below the existing content:
		command_line:
		  - sensor:
		      name: Disk Temperatures
		      command: "cat /config/disktemps/disk_temps.txt"
		      scan_interval: 120

     The scan_interval: 120 means that a new temperature is checked every 2 minutes. Set it according to your wishes

 

  • restart Home Assistant in WebUI via “Settings->System->Restart” (top right corner)
  • In HA WebUI, go to Developer tools and then States. The entity “sensor.disk_temperature” should now be found here.

 

 

  • install MQTT in HomeAssistant
    • Settings -> Devices and services -> Add integration -> MQTT 
      Now enter the IP and credentials of the external system (like IoBroker or else) in the settings! So name and password.

 

  • set up new automation in HomeAssistant
    • Settings -> Automations & scenes -> Create automation -> Create new automation
      Enter the following (sorry for being in German, you surely can translate easily via a translator ;-) ) :

image.thumb.png.af445e07bd656a942fc83e9ebb7a6f7b.png

 

image.thumb.png.12060db8e3dc7342698f02a6bf704463.png

 

Safe, close, try. Now, the data should be send to the other system via MQTT.

 

Thanks for your replies earlier, they set me on the correct path!

Edited by Milvus
correction of cronjob in User-Scripts-App
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...