battery_percent; // If battery is above threshold, reset the notification flag if ($batteryPercent > $batteryThreshold && $device->battery_notification_sent) { $device->battery_notification_sent = false; $device->save(); continue; } // Skip if battery is not low or notification was already sent if ($batteryPercent > $batteryThreshold) { continue; } if ($device->battery_notification_sent) { continue; } /** @var User|null $user */ $user = $device->user; if (! $user) { continue; // Skip if no user is associated with the device } // Send notification and mark as sent $user->notify(new BatteryLow($device)); $device->battery_notification_sent = true; $device->save(); } } }