Here’s my solution to use chatgpt/openai to analyze my events.log from yesterday and summarize it to be spoken in the morning to alert me of any strangeness.
items:
String AI_LogResults "AI Log Results [%s]" (HomeState)
rule:
var String openAI_Request4 = 'Review the attached OpenHAB event log and summarize, in plain English and three sentences, what operational activity occurred around failures, automations, or user actions, excluding raw status noise and hp printer.'
var String openAI_Script4 = '/etc/openhab/scripts/analyze_log.sh'
var String openAI_File4 = '/var/log/openhab/Archived/events.log.7.log'
if (gInternet.state == ON) {
logInfo("NIGHTLYSTUFF","DONE - Summary of Analysis of Home Automation Events Log.")
var String results55 = executeCommandLine(Duration.ofSeconds(200), bash, openAI_Script4, openAI_File4, openAI_Request4)
if (results55 !== null && results55 != NULL && results55 != '') {
results55 = results55.trim()
AI_LogResults.postUpdate(results55)
logInfo("OPENAI","-----------------------------------------------------------------------------")
logInfo("OPENAI",results55)
logInfo("OPENAI","-----------------------------------------------------------------------------")
}
}
script:
#!/usr/bin/env bash
OPENAI_API_KEY="<your key>"
MODEL="gpt-4.1-mini"
MAX_TOKENS=500
CHUNK_LINES=500
SLEEP_BETWEEN=1
LOG_FILE="$1"
REQUEST_TEXT="$2"
if [[ -z "$LOG_FILE" || -z "$REQUEST_TEXT" ]]; then
echo "Usage: $0 /path/to/logfile.log \"Your request text\""
exit 1
fi
if [[ ! -f "$LOG_FILE" ]]; then
echo "Error: File not found: $LOG_FILE"
exit 1
fi
TMP_DIR=$(mktemp -d)
split -l "$CHUNK_LINES" "$LOG_FILE" "$TMP_DIR/chunk_"
SUMMARIES=()
for CHUNK in "$TMP_DIR"/chunk_*; do
TMP_JSON=$(mktemp)
TMP_RESP=$(mktemp)
jq -n --arg model "$MODEL" \
--arg prompt "$REQUEST_TEXT" \
--rawfile log "$CHUNK" \
--argjson max_tokens "$MAX_TOKENS" \
'{
model: $model,
messages: [
{role: "user", content: ("Analyze the following log chunk and " + $prompt + "\n\n" + $log)}
],
max_tokens: $max_tokens
}' > "$TMP_JSON"
# Use --no-buffer and output to a file to avoid hanging
curl --no-buffer -s https://api.openai.com/v1/chat/completions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d @"$TMP_JSON" > "$TMP_RESP"
SUMMARY=$(jq -r '.choices[0].message.content' < "$TMP_RESP")
SUMMARIES+=("$SUMMARY")
rm -f "$TMP_JSON" "$TMP_RESP"
sleep "$SLEEP_BETWEEN"
done
COMBINED=$(printf "%s\n\n" "${SUMMARIES[@]}")
FINAL_JSON=$(mktemp)
FINAL_RESP=$(mktemp)
jq -n --arg model "$MODEL" \
--arg prompt "$REQUEST_TEXT" \
--arg combined "$COMBINED" \
--argjson max_tokens "$MAX_TOKENS" \
'{
model: $model,
messages: [
{role: "user", content: ("Combine these chunk summaries and " + $prompt + "\n\n" + $combined)}
],
max_tokens: $max_tokens
}' > "$FINAL_JSON"
curl --no-buffer -s https://api.openai.com/v1/chat/completions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d @"$FINAL_JSON" > "$FINAL_RESP"
jq -r '.choices[0].message.content' < "$FINAL_RESP"
rm -rf "$TMP_DIR" "$FINAL_JSON" "$FINAL_RESP"
Output:
The OpenHAB system underwent a large-scale restart or reload during which numerous smart home devices—including lighting, thermostats, media players, sensors, and security components—were reinitialized and transitioned mostly from offline or unknown to online states, with typical temporary communication errors resolving without critical failure. Multiple automation scripts ran to adjust system modes, turning off lights and switches, disabling alarms, enabling motion sensors, pausing Roomba vacuums, and issuing voice alerts such as weather warnings and cryptocurrency price drops, indicating coordinated automated routines rather than direct user control. Throughout the period, intermittent connectivity issues affected certain Z-Wave nodes and the HP LaserJet printer but were transient and resolved automatically, while ongoing environmental monitoring and sensor updates triggered numerous rule executions, reflecting stable system operation without major disruptions.
Best, Jay