Tom DeCricle
Published © GPL3+

Home-Assistant Rasperry Pi 5 Custom Sensor

Grab any data from a Raspberry-Pi 5 and display it as a sensor on Home Assistant

IntermediateFull instructions provided4 hours237
Home-Assistant Rasperry Pi 5 Custom Sensor

Things used in this project

Hardware components

Raspberry Pi 5
Raspberry Pi 5
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian

Story

Read more

Schematics

RPi-Home-Assistant-Sensor_Schematic

Workflow for moving data from a raspberry pi to the home assistant dashboard

Code

read_cpu_temp.py

Python
Run this python script to write the CPU temperature to the Apache server
#!/usr/bin/python3
import os

# Path to CPU Temp file
temperature_path = "/sys/class/hwmon/hwmon0/temp1_input"
# Path to apache server
apache_path = "/var/www/html/cpu_data/temperature.txt"
# Debug Cron Path
cron_temp = "/home/tom/Documents/home-assistant/read_cpu_temp/temperature.txt"

def read_cpu_temp():
	with open(temperature_path, 'r') as temp_file:
		temp_str = temp_file.readline().strip()
		temp_celsius = int(temp_str) / 1000 # converting millidegrees to degrees Celsius 
	return temp_celsius

def write_to_file(filename, data):
	with open(apache_path, 'w') as file:
	        file.write(str(data))

if __name__ == "__main__":
	cpu_temp = read_cpu_temp()
	write_to_file(apache_path, cpu_temp)

Credits

Tom DeCricle

Tom DeCricle

3 projects • 0 followers
Electrical Engineer with experience in Schematic Design, PCB Layout, PCB Fabrication & Assembly Procurement, Hardware/Firmware/Software Dev
Thanks to Thomas Ward .

Comments