Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output File #157

Merged
merged 4 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/config.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,18 @@ cpu_thermal_zone = 'cpu'
# read external sensors for temperature, humidity, pressure etc.
ext_sensors = False
#ext_sensors = [["Housing", "ds18b20", "0014531448ff", -300], ["ext2", "sht21", 0, [-300, 0]]]

# output file
output_filename = False
#output_filename = "/dev/shm/mjpeg/user_annotate.txt"
# a for append or w for write (overwrites content)
output_mode = "w"
# define what should be in the output
def get_content_outputfile():
# In this example the values from the ext_sensors are used
# the values for the temperature values are rounded to 1 decimal
sht21_temp = round(float(ext_sensors[1][3][0]), 1)
sht21_hum = ext_sensors[1][3][1]
ds18b20_temp = round(float(ext_sensors[0][3]), 1)
return f"T: {sht21_temp} 'C; H: {sht21_hum} %% ; T-Rpi: {ds18b20_temp} 'C"

18 changes: 18 additions & 0 deletions src/rpi-cpu2mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,24 @@ def gather_and_send_info():
if args.display:
print_measured_values(monitored_values)

# write some output to a file
if config.output_filename:
# the only options are "a" for append or "w" for (over)write
# check if one of this options is defined
if config.output_mode not in ["a", "w"]:
print("Error, output_type not known. Default w is set.")
config.output_type = "w"
try:
# open the text file
output_file = open(config.output_filename, config.output_mode)
# read what should be written into the textfile
# we need to define this is a function, otherwise the values are not updated and default values are taken
output_content = config.get_content_outputfile()
output_file.write(output_content)
output_file.close()
except Exception as e:
print("Error writing to output file:", e)

if args.hass_api:
publish_to_hass_api(monitored_values)
else:
Expand Down