Which python script are you looking to use?
For myself, I have in most cases 2 separate python script for each Temp probe sensor. One that retrieves the temp in Celsius and the next one that does a Fahrenheit conversion.
This is the Celsius script. Note the Calibration adjustment section - if you don’t want to use this, remark it out. But I would eventually recommend doing a very minimum of 5 probe calibration to a single degree set point (whatever you usually keep your water temp at). When you’re ready to do this step tho, just let me know and I’ll help you more with that.
Also, be sure to insert the UUID # of each 1-Wire Temp probe to the respective file for that temp probe. Where you see the ##-############# in each script is where the 1-Wire UUID / Serial number needs to go.
the file name is, temp_1_C:
#!/usr/bin/python
import os
import glob
import time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '##-##################')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
# Calibration adjustments next line
temp_c = temp_c - 0.062
# temp_c = temp_c - 0.124
# end calibration
temp_c = round(temp_c, 2)
return temp_c
print(read_temp())
#print("Temp: " + read_temp() + unichr(223) + "C")
Here’s my Fahrenheit script, which is called ‘temp_1_F’
#!/usr/bin/python
import os
import glob
import time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '##-##############')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
# Calibration section
temp_c = temp_c - 0.062
# end calibration
temp_f = temp_c * 9.0 / 5.0 + 32.0
temp_f = round(temp_f, 2)
return temp_f
#while True:
# print(read_temp())
# time.sleep(5)
print(read_temp())
#print("Temp: " + read_temp() + unichr(223) + "C")
Now, what I do is I have another script or crontab execute this script to retrieve the 1-wire temp as often as I might need it for that probe. For instance, an unheated culture or salt water mix tank probably doesn’t need to send Temp data more than every 60 seconds. But… the 25watt heater in my 10 litre Fry tank most definitely needs a temp reading every 15 seconds, so I don’t over shoot my target temp.
And that execution is called like this in one of my setups, which is triggered by another script that executes at startup and just loops at a predetermined delay interval.
mosquitto_pub -u USERNAME -P “PASSWORD” -t homeassistant/sensor/fishroom/water/1/temp_C -m “/usr/local/bin/temp_probe_1_C
” -h HASSBIAN
Now… the upside to doing this this way, is it’s great for developing / testing the hardware as you build it because it’s pretty simple, modular and each script is basically specific to each probe. Which, since I’m not the worlds best python programmer (sic) I could imagine any single monolithic dynamically adjusting python script I might write to do this for all sensors might not bork out safely for a single sensor should only one sensor fail (in other words, my python script might could bork out on all of them). This initial approach of a single python script for each individual function was “safe and reliable” despite the overhead CPU cost on my first Gen RaspPi.
But I do eventually plan on replacing all of this. It’s not portable, it’s not user-friendly, it’s not self-adapting and dynamic, and it’s messy it’s like trying to kill a fly with a baseball bat. lol. I also made a mistake, which is instead of using a MQTT topic like ‘home assistant/sensor/fishroom/blah/blah/blah’ I SHOULD just be using something like ‘homeassistant/1wire/##-#################’. where the ###'s are the UUID of the 1-wire device.
Why? Because that allows maximum flexibility with temp probe mobility with minimal changes to HA and underlying configurational dependancies. If say you end up with a few Pi-Zero’s around the house with other temp probes, and you want to re-allocate temp probes later between multiple remote Pi-Zero’s - after physical relocation of the probes - any relabelling of the Temp probe interface name would only need to be done in HA’s configuration.xml (or sensor.xml if you broke that out).
In my current construction, I didn’t make it so easy on myself. And I’ve already had a few instances now, where I wanted to re-position temp probes, both in my house and in my aquarium, onto different remote sensor Pi’s… and I realised then, the error of my ways when I had to go around and change all my local Pi’ scripts to adapt. I should “just work” regardless of where I plug it in, without me needing to reconfigure anything (except for a name label for it’s new role… ie. - from Clownfish tank to Reef tank or Bathtub.
So my plan is to go back and re-write the python script with multiple goals now:
1.) make it 100% pure python (for way lower cpu overhead and portability)
2.) always watch automatically for new 1-wire devices
3.) enumerate and automatically poll all 1-wire devices & publish to MQTT server
4.) centralized management of individual temp probes calibration settings
5.) centralised management of publish frequency
I imagine someone might have something like this already and shared it, but I’ve not seen it yet. But it’ll probably take me a little bit of time to hack through it and get it right but should be a good python learning exercise.