Code kopieren & zu File Editor
1# System Monitoring Integration
2# Überwacht Raspberry Pi, Server und UPS-Status über Command Line und SSH
3
4# Command Line Sensoren für lokales System
5sensor:
6 # CPU Temperatur (Raspberry Pi)
7 - platform: command_line
8 name: "RPi CPU Temperature"
9 command: 'cat /sys/class/thermal/thermal_zone0/temp'
10 unit_of_measurement: "°C"
11 value_template: "{{ value | multiply(0.001) | round(1) }}"
12 scan_interval: 60
13 icon: "mdi:thermometer"
14
15 # CPU Auslastung
16 - platform: command_line
17 name: "RPi CPU Usage"
18 command: "top -bn1 | grep 'Cpu(s)' | awk '{print $2}' | cut -d'%' -f1"
19 unit_of_measurement: "%"
20 value_template: "{{ value | float | round(1) }}"
21 scan_interval: 30
22 icon: "mdi:cpu-64-bit"
23
24 # RAM Verwendung
25 - platform: command_line
26 name: "RPi Memory Usage"
27 command: "free | grep Mem | awk '{printf \"%.1f\", $3/$2 * 100.0}'"
28 unit_of_measurement: "%"
29 scan_interval: 60
30 icon: "mdi:memory"
31
32 # Festplatten-Nutzung
33 - platform: command_line
34 name: "RPi Disk Usage"
35 command: "df -h / | awk 'NR==2{print $5}' | cut -d'%' -f1"
36 unit_of_measurement: "%"
37 scan_interval: 300
38 icon: "mdi:harddisk"
39
40 # Freier Speicher in GB
41 - platform: command_line
42 name: "RPi Disk Free"
43 command: "df -BG / | awk 'NR==2{print $4}' | cut -d'G' -f1"
44 unit_of_measurement: "GB"
45 scan_interval: 300
46 icon: "mdi:harddisk"
47
48 # Uptime in Tagen
49 - platform: command_line
50 name: "RPi Uptime"
51 command: "awk '{print int($1/86400)}' /proc/uptime"
52 unit_of_measurement: "Tage"
53 scan_interval: 3600
54 icon: "mdi:clock-outline"
55
56 # Load Average
57 - platform: command_line
58 name: "RPi Load Average"
59 command: "uptime | awk -F'load average:' '{print $2}' | awk '{print $1}' | cut -d',' -f1"
60 scan_interval: 60
61 icon: "mdi:gauge"
62
63 # Netzwerk Traffic (Download MB/s)
64 - platform: command_line
65 name: "RPi Network Download"
66 command: "cat /sys/class/net/eth0/statistics/rx_bytes"
67 scan_interval: 10
68 value_template: >
69 {% if states('sensor.rpi_network_download') != 'unknown' %}
70 {% set prev = states('sensor.rpi_network_download') | float %}
71 {% set curr = value | float %}
72 {% if curr > prev %}
73 {{ ((curr - prev) / (1024*1024) / 10) | round(2) }}
74 {% else %}
75 0
76 {% endif %}
77 {% else %}
78 0
79 {% endif %}
80 unit_of_measurement: "MB/s"
81 icon: "mdi:download"
82
83# UPS Monitoring (für APC UPS über apcupsd)
84 - platform: command_line
85 name: "UPS Status"
86 command: 'apcaccess status | grep "STATUS" | cut -d":" -f2 | xargs'
87 scan_interval: 60
88 icon: "mdi:battery-charging"
89
90 - platform: command_line
91 name: "UPS Battery Level"
92 command: 'apcaccess status | grep "BCHARGE" | cut -d":" -f2 | cut -d" " -f2'
93 unit_of_measurement: "%"
94 scan_interval: 60
95 device_class: battery
96 icon: "mdi:battery"
97
98 - platform: command_line
99 name: "UPS Load"
100 command: 'apcaccess status | grep "LOADPCT" | cut -d":" -f2 | cut -d" " -f2'
101 unit_of_measurement: "%"
102 scan_interval: 60
103 icon: "mdi:flash"
104
105 - platform: command_line
106 name: "UPS Time Left"
107 command: 'apcaccess status | grep "TIMELEFT" | cut -d":" -f2 | cut -d" " -f2'
108 unit_of_measurement: "min"
109 scan_interval: 60
110 icon: "mdi:timer"
111
112# Internet Speed Test (einmal täglich)
113 - platform: command_line
114 name: "Internet Speed Download"
115 command: 'speedtest-cli --simple | grep "Download" | cut -d" " -f2'
116 unit_of_measurement: "Mbit/s"
117 scan_interval: 86400 # Einmal am Tag
118 icon: "mdi:speedometer"
119
120 - platform: command_line
121 name: "Internet Speed Upload"
122 command: 'speedtest-cli --simple | grep "Upload" | cut -d" " -f2'
123 unit_of_measurement: "Mbit/s"
124 scan_interval: 86400
125 icon: "mdi:upload"
126
127# SSH-basierte Remote Server Überwachung
128 - platform: command_line
129 name: "Server CPU Temperature"
130 command: 'ssh user@192.168.1.100 "cat /sys/class/thermal/thermal_zone0/temp"'
131 unit_of_measurement: "°C"
132 value_template: "{{ value | multiply(0.001) | round(1) }}"
133 scan_interval: 300
134 icon: "mdi:thermometer"
135
136 - platform: command_line
137 name: "Server Disk Usage"
138 command: 'ssh user@192.168.1.100 "df -h / | awk '\''NR==2{print \$5}'\'' | cut -d'\''%'\'' -f1"'
139 unit_of_measurement: "%"
140 scan_interval: 600
141 icon: "mdi:harddisk"
142
143# Docker Container Monitoring
144 - platform: command_line
145 name: "Docker Containers Running"
146 command: 'docker ps -q | wc -l'
147 scan_interval: 120
148 icon: "mdi:docker"
149
150 - platform: command_line
151 name: "Docker Containers Total"
152 command: 'docker ps -a -q | wc -l'
153 scan_interval: 300
154 icon: "mdi:docker"
155
156# Home Assistant eigene Statistiken
157 - platform: command_line
158 name: "HA Database Size"
159 command: 'du -sh /config/home-assistant_v2.db | cut -f1'
160 scan_interval: 3600
161 icon: "mdi:database"
162
163 - platform: command_line
164 name: "HA Log Size"
165 command: 'du -sh /config/home-assistant.log | cut -f1'
166 scan_interval: 1800
167 icon: "mdi:file-document"
168
169# Template Sensoren für erweiterte Berechnungen
170 - platform: template
171 sensors:
172 rpi_cpu_temperature_status:
173 friendly_name: "RPi CPU Status"
174 value_template: >
175 {% set temp = states('sensor.rpi_cpu_temperature') | float %}
176 {% if temp < 50 %}Normal
177 {% elif temp < 65 %}Warm
178 {% elif temp < 80 %}Heiß
179 {% else %}Kritisch
180 {% endif %}
181 icon_template: >
182 {% set temp = states('sensor.rpi_cpu_temperature') | float %}
183 {% if temp < 50 %}mdi:thermometer-low
184 {% elif temp < 65 %}mdi:thermometer
185 {% elif temp < 80 %}mdi:thermometer-high
186 {% else %}mdi:thermometer-alert
187 {% endif %}
188
189 system_health_score:
190 friendly_name: "System Health Score"
191 unit_of_measurement: "%"
192 value_template: >
193 {% set cpu_temp = states('sensor.rpi_cpu_temperature') | float %}
194 {% set cpu_usage = states('sensor.rpi_cpu_usage') | float %}
195 {% set mem_usage = states('sensor.rpi_memory_usage') | float %}
196 {% set disk_usage = states('sensor.rpi_disk_usage') | float %}
197
198 {% set temp_score = 100 - ((cpu_temp - 30) * 2) if cpu_temp > 30 else 100 %}
199 {% set cpu_score = 100 - cpu_usage %}
200 {% set mem_score = 100 - mem_usage %}
201 {% set disk_score = 100 - disk_usage %}
202
203 {{ ((temp_score + cpu_score + mem_score + disk_score) / 4) | round(0) }}
204 icon_template: >
205 {% set score = states('sensor.system_health_score') | float %}
206 {% if score > 80 %}mdi:heart
207 {% elif score > 60 %}mdi:heart-half
208 {% else %}mdi:heart-broken
209 {% endif %}
210
211# Binäre Sensoren für Alarme
212binary_sensor:
213 - platform: template
214 sensors:
215 rpi_high_temperature:
216 friendly_name: "RPi Überhitzung"
217 value_template: "{{ states('sensor.rpi_cpu_temperature') | float > 70 }}"
218 device_class: heat
219 delay_on: "00:02:00"
220
221 rpi_high_cpu_usage:
222 friendly_name: "RPi Hohe CPU Last"
223 value_template: "{{ states('sensor.rpi_cpu_usage') | float > 80 }}"
224 delay_on: "00:05:00"
225
226 rpi_low_disk_space:
227 friendly_name: "RPi Wenig Speicher"
228 value_template: "{{ states('sensor.rpi_disk_usage') | float > 85 }}"
229 device_class: problem
230 delay_on: "00:30:00"
231
232 ups_battery_low:
233 friendly_name: "UPS Batterie schwach"
234 value_template: "{{ states('sensor.ups_battery_level') | float < 20 }}"
235 device_class: battery
236 delay_on: "00:02:00"
237
238 ups_on_battery:
239 friendly_name: "UPS im Batteriebetrieb"
240 value_template: "{{ states('sensor.ups_status') == 'ONBATT' }}"
241 device_class: power
242 delay_on: "00:00:30"
243
244 internet_slow:
245 friendly_name: "Langsames Internet"
246 value_template: "{{ states('sensor.internet_speed_download') | float < 25 }}"
247 delay_on: "00:10:00"
248
249# Automatisierungen für System-Alerts
250automation:
251 - alias: "System Überhitzung Warnung"
252 trigger:
253 platform: state
254 entity_id: binary_sensor.rpi_high_temperature
255 to: "on"
256 action:
257 - service: notify.mobile_app
258 data:
259 title: "🌡️ System Überhitzung"
260 message: "RPi CPU: {{ states('sensor.rpi_cpu_temperature') }}°C"
261 data:
262 tag: "system_temp"
263 color: "red"
264
265 - alias: "UPS Stromausfall"
266 trigger:
267 platform: state
268 entity_id: binary_sensor.ups_on_battery
269 to: "on"
270 action:
271 - service: notify.mobile_app
272 data:
273 title: "⚡ Stromausfall erkannt"
274 message: "UPS im Batteriebetrieb - {{ states('sensor.ups_time_left') }} min verbleibend"
275 data:
276 tag: "power_outage"
277 color: "red"
278 sound: "alarm.wav"
279
280 - alias: "Wenig Speicherplatz"
281 trigger:
282 platform: state
283 entity_id: binary_sensor.rpi_low_disk_space
284 to: "on"
285 action:
286 - service: notify.mobile_app
287 data:
288 title: "💾 Speicher knapp"
289 message: "Nur noch {{ states('sensor.rpi_disk_free') }}GB frei ({{ states('sensor.rpi_disk_usage') }}% belegt)"
290
291 - alias: "System Health Check"
292 trigger:
293 platform: time
294 at: "09:00:00"
295 action:
296 - service: notify.mobile_app
297 data:
298 title: "📊 Täglicher System Report"
299 message: >
300 Health Score: {{ states('sensor.system_health_score') }}%
301 CPU: {{ states('sensor.rpi_cpu_temperature') }}°C
302 Uptime: {{ states('sensor.rpi_uptime') }} Tage
303 UPS: {{ states('sensor.ups_battery_level') }}%
304
305# Script für manuellen Speed Test
306script:
307 run_speed_test:
308 alias: "Internet Speed Test starten"
309 sequence:
310 - service: notify.persistent_notification
311 data:
312 title: "Speed Test"
313 message: "Internet Speed Test wird durchgeführt..."
314 - service: homeassistant.update_entity
315 target:
316 entity_id:
317 - sensor.internet_speed_download
318 - sensor.internet_speed_upload
319 - delay: 60
320 - service: notify.mobile_app
321 data:
322 title: "🌐 Speed Test Ergebnis"
323 message: >
324 Download: {{ states('sensor.internet_speed_download') }} Mbit/s
325 Upload: {{ states('sensor.internet_speed_upload') }} Mbit/s
326
327# Input Helper für Konfiguration
328input_number:
329 system_alert_cpu_threshold:
330 name: "CPU Alarm Schwelle"
331 min: 60
332 max: 95
333 step: 5
334 initial: 80
335 unit_of_measurement: "%"
336 icon: "mdi:cpu-64-bit"
337
338 system_alert_temp_threshold:
339 name: "Temperatur Alarm Schwelle"
340 min: 60
341 max: 85
342 step: 1
343 initial: 70
344 unit_of_measurement: "°C"
345 icon: "mdi:thermometer"
346
347input_boolean:
348 system_monitoring_enabled:
349 name: "System Monitoring aktiviert"
350 initial: true
351 icon: "mdi:monitor-dashboard"
352
353 system_notifications_enabled:
354 name: "System Benachrichtigungen"
355 initial: true
356 icon: "mdi:bell"