


// Calculate 3hr moving baro pressure
$moving_barom_press = 0.0;
find_barop_3hr_ave(toHpaBarom($conn, $_POST['baromrelin']), $moving_barom_press);


function find_barop_3hr_ave($curr_barom_press, &$moving_barom)
	{

	// calculate 3 hour moving ave for barometrix pressure
		
		$hrsAgo = 3;;
		$tStep = 15;

		$minsAgo = $hrsAgo * 60;
		$minStart = $minsAgo - $tStep;
		$minEnd = $minsAgo + $tStep;
				
		$sql = " SELECT AVG(baromrelhpa) AS av_val FROM weather_data_metric WHERE 
                 dateacst BETWEEN DATE_ADD(NOW(), INTERVAL -190 MINUTE)
                              AND DATE_ADD(NOW(), INTERVAL -170 MINUTE) ";

//		$sql = " SELECT (baromrelhpa) AS av_val FROM weather_data_metric WHERE 
//                 dateacst BETWEEN DATE_ADD(NOW(), INTERVAL -" . $mE . " MINUTE)
//                              AND DATE_ADD(NOW(), INTERVAL -" . $mS . " MINUTE) ";
		
//		$result = $conn->query($sql);
//		$row = $result->fetch_assoc();
/*		
		if ($result == true) {
			$moving_barom = 1.0;
			}
		else
			{
			$moving_barom = 2.0;
			}
*/	
		/*
		if ($result && $row = mysqli_fetch_assoc($result)) {
			// Check if the result is not null (in case no rows matched the time range
			if ($row['avg_val'] !== null) {
				$moving_barom = $curr_barom_press - $row['avg_val'];
			} else {
				// no average found
				$moving_barom = 0.0;
			}
		
		}
		*/
		$moving_barom = $curr_barom_press/100;
		
	}
*/












Some changes ands requests.
Change "Feels Like" to apparent.
Added a function to get average rainfall current month - to be compared to current month:
function getMthAvg().
avg_visibility is already in kms, no need to /1000.

  `ave_wind_sp` decimal(5,2) DEFAULT NULL,      current wind speed.
  `10min_max_wind` decimal(5,2) DEFAULT NULL,   max gust for the same period as ave_wind_sp.
  `winddir_cardinal` varchar(10) DEFAULT NULL,   dirextion for the above.
  `maxdailygustkmh` decimal(5,2) DEFAULT NULL,   daily maximum, so far.


<?php
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log','/home/paul/logs/gws5037/dashboard_php.log');

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
require_once 'config.php';

$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($conn->connect_error) { die("Connection failed"); }

// Fetch Latest Metric Data
$metric = $conn->query("SELECT * FROM weather_data_metric ORDER BY dateacst DESC LIMIT 1")->fetch_assoc();
// Fetch Latest BOM Observations
$bom_obs = $conn->query("SELECT * FROM bom_obs_readings ORDER BY created_at DESC LIMIT 1")->fetch_assoc();
// Fetch Latest Forecast
$forecast = $conn->query("SELECT * FROM weather_forecasts ORDER BY created_at DESC LIMIT 1")->fetch_assoc();

if (!$metric) { echo "No data found."; exit; }

 // Get average rainfall current month
function getMthAvg()
{
	$cMth = (int)date("m")-1;
	$mthAvg = array(20.1, 20.5, 23.5, 43.7, 67.9, 71.6, 65.7, 61.5, 50.8, 44.3, 30.9, 25.9);
	
	return $mthAvg[$cMth];
}


?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Weather Dashboard</title>
    <style>
        body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: #eef2f3; color: #333; margin: 0; padding: 20px; }
        .container { max-width: 1000px; margin: auto; display: flex; flex-wrap: wrap; gap: 15px; }
        header { width: 100%; display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 10px; border-bottom: 2px solid #ddd; padding-bottom: 10px; }
        h2 { margin: 0; color: #2c3e50; font-size: 1.2rem; text-transform: uppercase; width: 100%; margin-top: 10px; }
        
        .card { background: #fff; padding: 15px; border-radius: 12px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); flex: 1 1 calc(25% - 20px); min-width: 160px; display: flex; flex-direction: column; justify-content: center; align-items: center; }
        .card h3 { margin: 0; color: #7f8c8d; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 1px; }
        .card p { font-size: 1.6rem; font-weight: bold; margin: 8px 0 0; color: #2c3e50; }
        .card .unit { font-size: 0.9rem; color: #95a5a6; margin-left: 2px; }
        .card .sub-text { font-size: 0.8rem; color: #7f8c8d; margin-top: 5px; }
        
        .forecast-card { background: #34495e; color: white; flex: 1 1 45%; }
        .forecast-card h3 { color: #bdc3c7; }
        .forecast-card p { color: #fff; }
        
        .timestamp { color: #7f8c8d; font-size: 0.8rem; }
        .uv-rating { padding: 2px 8px; border-radius: 4px; font-size: 0.8rem; background: #f1c40f; color: #000; font-weight: bold; }
    </style>
    <script>setTimeout(() => location.reload(), 60000);</script>
</head>
<body>

<div class="container">
    <header>
        <div style="font-size: 1.5rem; font-weight: bold;">Weather Station</div>
        <div class="timestamp">Last Update: <?php echo $metric['dateacst']; ?></div>
    </header>

    <h2>Current Conditions</h2>
    <div class="card">
        <h3>Temperature</h3>
        <p><?php echo $metric['tempc']; ?><span class="unit">°C</span></p>
        <div class="sub-text">Feels like <?php echo $metric['app_tempc']; ?>°C</div>
    </div>
    <div class="card">
        <h3>Humidity</h3>
        <p><?php echo $metric['humidity']; ?><span class="unit">%</span></p>
    </div>
    <div class="card">
        <h3>Wind</h3>
        <p><?php echo $metric['ave_wind_sp']; ?><span class="unit">km/h</span></p>
        <div class="sub-text"><?php echo $metric['winddir_cardinal']; ?> | Gusts <?php echo $metric['maxdailygustkmh']; ?></div>
    </div>
    <div class="card">
        <h3>Rain (Today)</h3>
        <p><?php echo $metric['dailyrain_mm']; ?><span class="unit">mm</span></p>
        <div class="sub-text">Month: <?php echo $metric['monthlyrain_mm']; ?>mm</div>
    </div>

    <h2>Atmospheric & UV</h2>
    <div class="card">
        <h3>Pressure</h3>
        <p><?php echo $metric['baromrelhpa']; ?><span class="unit">hPa</span></p>
        <div class="sub-text"><?php echo $metric['barom_move']; ?></div>
    </div>
    <div class="card">
        <h3>Visibility</h3>
        <p><?php echo $bom_obs['avg_visibility']; ?><span class="unit">km</span></p>
    </div>
    <div class="card">
        <h3>UV Index</h3>
        <p><?php echo $bom_obs['uv_index'] ?? $forecast['uv_index']; ?></p>
        <span class="uv-rating"><?php echo $bom_obs['uv_rating'] ?? $forecast['uv_rating']; ?></span>
    </div>

    <h2>Forecast</h2>
    <div class="card forecast-card">
        <h3>Today's Outlook</h3>
        <img src="icons/<?php echo $forecast['forecast_icon']; ?>.png" alt="icon" style="width:50px;">
        <p><?php echo $forecast['air_temp_max']; ?>°C</p>
        <div class="sub-text" style="color:#ecf0f1;"><?php echo $forecast['precis']; ?></div>
        <div class="sub-text" style="color:#ecf0f1;"><?php echo $forecast['probability_of_rain']; ?> rain (<?php echo $forecast['rain_range']; ?>)</div>
    </div>
    <div class="card forecast-card">
        <h3>Tomorrow</h3>
        <img src="icons/<?php echo $forecast['tmw_forecast_icon']; ?>.png" alt="icon" style="width:50px;">
        <p><?php echo $forecast['tmw_air_temp_max']; ?>°C <span style="font-size:1rem; opacity:0.7;">/ <?php echo $forecast['tmw_air_temp_min']; ?>°C</span></p>
        <div class="sub-text" style="color:#ecf0f1;">Issued: <?php echo $forecast['issue_time']; ?></div>
    </div>
</div>

</body>
</html>










