function cron()
{
    global $a, $s, $i, $t;
    $weer = unserialize(cget('weer'));
    $buienradar = $weer['buien'];
    $buiten_temp = $weer['buiten_temp'];
    $wind = $weer['wind'];
    $windrichting = $weer['windrichting'];
    $wolken = $weer['wolken'];
    if ($s['weg'] == 'On') {
        if ($s['heating'] != 'Off' && strtotime($t['heating']) < eenuur) {
            sw($i['heating'], 'Off', 'heating');
            $s['heating'] = 'Off';
        }
    }
    //else {if($s['heating']!='On'){sw($i['heating'],'On','heating');$s['heating']='On';}}
    $Setkamer = 8.0;
    $setpointkamer = cget('setpointkamer');
    if ($setpointkamer != 0 && strtotime($t['kamer_set']) < eenuur) {
        cset('setpointkamer', 0);
        $setpointkamer = 0;
    }
    if ($setpointkamer != 2) {
        if ($buiten_temp < 15 && $s['raamkamer'] == 'Closed' && $s['heating'] == 'On' && (strtotime($t['raamkamer']) < tweeuur || time > achtavond)) {
            $Setkamer = 12.0;
            if (time < zevenochtend || time > achtavond) {
                $Setkamer = 16.0;
            }
        }
        if ($s['kamer_set'] != $Setkamer) {
            ud($i['kamer_set'], 0, $Setkamer, 'Rkamer_set');
            $s['kamer_set'] = $Setkamer;
        }
    }
    $Settobi = 8.0;
    $setpointtobi = cget('setpointtobi');
    if ($setpointtobi != 0 && strtotime($t['tobi_set']) < eenuur) {
        cset('setpointtobi', 0);
        $setpointtobi = 0;
    }
    if ($setpointtobi != 2) {
        if ($buiten_temp < 15 && $s['raamtobi'] == 'Closed' && $s['heating'] == 'On' && (strtotime($t['raamtobi']) < tweeuur || time > achtavond)) {
            $Settobi = 12.0;
            if (date('W') % 2 == 1) {
                if (date('N') == 3) {
                    if (time > achtavond) {
                        $Settobi = 16.0;
                    } elseif (date('N') == 4) {
                        if (time < zevenochtend || time > achtavond) {
                            $Settobi = 16.0;
                        } elseif (date('N') == 5) {
                            if (time < zevenochtend) {
                                $Settobi = 16.0;
                            }
                        }
                    }
                }
            } else {
                if (date('N') == 3) {
                    if (time > achtavond) {
                        $Settobi = 16.0;
                    } elseif (in_array(date('N'), array(4, 5, 6))) {
                        if (time < zevenochtend || time > achtavond) {
                            $Settobi = 16.0;
                        } elseif (date('N') == 7) {
                            if (time < zevenochtend) {
                                $Settobi = 16.0;
                            }
                        }
                    }
                }
            }
        }
        if ($s['tobi_set'] != $Settobi) {
            ud($i['tobi_set'], 0, $Settobi, 'Rtobi_set');
            $s['tobi_set'] = $Settobi;
        }
    }
    $Setalex = 8.0;
    $setpointalex = cget('setpointalex');
    if ($setpointalex != 0 && strtotime($t['alex_set']) < achtuur) {
        cset('setpointalex', 0);
        $setpointalex = 0;
    }
    if ($setpointalex != 2) {
        if ($buiten_temp < 17 && $s['raamalex'] == 'Closed' && $s['heating'] == 'On' && (strtotime($t['raamalex']) < tweeuur || time > achtavond)) {
            $Setalex = 12.0;
            if (time < strtotime('8:00') || time > achtavond) {
                $Setalex = 18.5;
            }
        }
        if ($s['alex_set'] != $Setalex) {
            ud($i['alex_set'], 0, $Setalex, 'Ralex_set');
            $s['alex_set'] = $Setalex;
        }
    }
    $Setliving = 14.0;
    $setpointliving = cget('setpointliving');
    if ($setpointliving != 0 && strtotime($t['living_set']) < drieuur) {
        cset('setpointliving', 0);
        $setpointliving = 0;
    }
    if ($setpointliving != 2) {
        if ($buiten_temp < 20 && $s['heating'] == 'On' && $s['raamliving'] == 'Closed') {
            $Setliving = 17.0;
            if (time >= strtotime('3:00') && time < zevenochtend) {
                $s['slapen'] == 'Off' ? $Setliving = 20.5 : ($Setliving = 17.0);
            } elseif (time >= zevenochtend && time < strtotime('22:30')) {
                $s['slapen'] == 'Off' ? $Setliving = 20.5 : ($Setliving = 17.0);
            }
        }
        if ($s['living_set'] != $Setliving) {
            ud($i['living_set'], 0, $Setliving, 'Rliving_set');
            $s['living_set'] = $Setliving;
        }
    }
    if ($s['deurbadkamer'] == "Open") {
        if ($s['badkamer_set'] != 10.0 && (strtotime($t['deurbadkamer']) < eenmin || $s['lichtbadkamer'] == 'Off')) {
            ud($i['badkamer_set'], 0, 10, 'badkamer_set 10 deur open');
            $s['badkamer_set'] = 10.0;
        }
    } elseif ($s['deurbadkamer'] == "Closed") {
        if ($s['lichtbadkamer'] == 'On' && $s['badkamer_set'] != 22.0) {
            ud($i['badkamer_set'], 0, 22.0, 'badkamer_set 22 deur dicht en licht aan');
            $s['badkamer_set'] = 22.0;
        } elseif ($s['lichtbadkamer'] == 'Off' && $s['badkamer_set'] != 15.0) {
            ud($i['badkamer_set'], 0, 15.0, 'badkamer_set 15 deur dicht en licht uit');
            $s['badkamer_set'] = 15.0;
        }
    }
    $kamers = array('living', 'badkamer', 'tobi', 'alex', 'kamer');
    $kamersgas = array('living', 'alex');
    $bigdif = 100;
    $brander = $s['brander'];
    $Tbrander = strtotime($t['brander']);
    $Ibrander = $i['brander'];
    foreach ($kamers as $kamer) {
        ${'dif' . $kamer} = number_format($s[$kamer . '_temp'] - $s[$kamer . '_set'], 1);
        if (in_array($kamer, $kamersgas)) {
            if (${'dif' . $kamer} < $bigdif) {
                $bigdif = ${'dif' . $kamer};
            }
        }
        ${'Set' . $kamer} = number_format($s[$kamer . '_set'], 1);
    }
    foreach ($kamers as $kamer) {
        if (${'dif' . $kamer} <= number_format($bigdif + 0.2, 1) && ${'dif' . $kamer} < 1) {
            ${'RSet' . $kamer} = setradiator($kamer, ${'dif' . $kamer}, true, $s[$kamer . '_set']);
        } else {
            ${'RSet' . $kamer} = setradiator($kamer, ${'dif' . $kamer}, false, $s[$kamer . '_set']);
        }
    }
    if ($s['kamerZ'] != $RSetkamer) {
        ud($i['kamerZ'], 0, $RSetkamer, 'RkamerZ');
    }
    if ($s['tobiZ'] != $RSettobi) {
        ud($i['tobiZ'], 0, $RSettobi, 'RtobiZ');
    }
    if ($s['alexZ'] != $RSetalex) {
        ud($i['alexZ'], 0, $RSetalex, 'RalexZ');
    }
    //if($s['badkamerZ']!=$RSetbadkamer)ud($i['badkamerZ'],0,$RSetbadkamer,'RbadkamerZ');
    if ($s['livingZ'] != $RSetliving) {
        ud($i['livingZ'], 0, $RSetliving, 'RlivingZ');
    }
    if ($s['livingZZ'] != $RSetliving) {
        ud($i['livingZZ'], 0, $RSetliving, 'RlivingZZ');
    }
    if ($s['livingZE'] != $RSetliving) {
        ud($i['livingZE'], 0, $RSetliving, 'RlivingZE');
    }
    if ($bigdif <= -0.3 && $brander == "Off" && $Tbrander < time - 60) {
        sw($Ibrander, 'On', 'brander dif = ' . $bigdif);
    } elseif ($bigdif <= -0.2 && $brander == "Off" && $Tbrander < time - 120) {
        sw($Ibrander, 'On', 'brander dif = ' . $bigdif);
    } elseif ($bigdif <= -0.1 && $brander == "Off" && $Tbrander < time - 180) {
        sw($Ibrander, 'On', 'brander dif = ' . $bigdif);
    } elseif ($bigdif <= 0 && $brander == "Off" && $Tbrander < time - 240) {
        sw($Ibrander, 'On', 'brander dif = ' . $bigdif);
    } elseif ($bigdif >= 0.1 && $brander == "On" && $Tbrander < time) {
        sw($Ibrander, 'Off', 'brander dif = ' . $bigdif);
    } elseif ($bigdif >= 0 && $brander == "On" && $Tbrander < time - 60) {
        sw($Ibrander, 'Off', 'brander dif = ' . $bigdif);
    } elseif ($bigdif >= -0.1 && $brander == "On" && $Tbrander < time - 120) {
        sw($Ibrander, 'Off', 'brander dif = ' . $bigdif);
    } elseif ($bigdif >= -0.2 && $brander == "On" && $Tbrander < time - 180) {
        sw($Ibrander, 'Off', 'brander dif = ' . $bigdif);
    } elseif ($bigdif >= -0.3 && $brander == "On" && $Tbrander < time - 240) {
        sw($Ibrander, 'Off', 'brander dif = ' . $bigdif);
    }
    $badkvuur = $s['badkamervuur'];
    $Tbadkvuur = strtotime($t['badkamervuur']);
    $Ibadkvuur = $i['badkamervuur'];
    if ($difbadkamer <= -0.2 && $badkvuur == "Off" && $Tbadkvuur < time - 180) {
        sw($Ibadkvuur, 'On', 'badkamervuur dif = ' . $difbadkamer);
    } elseif ($difbadkamer <= -0.1 && $badkvuur == "Off" && $Tbadkvuur < time - 240) {
        sw($Ibadkvuur, 'On', 'badkamervuur dif = ' . $difbadkamer);
    } elseif ($difbadkamer <= 0 && $badkvuur == "Off" && $Tbadkvuur < time - 360) {
        sw($Ibadkvuur, 'On', 'badkamervuur dif = ' . $difbadkamer);
    } elseif ($difbadkamer >= 0.1 && $badkvuur == "On" && $Tbadkvuur < time) {
        sw($Ibadkvuur, 'Off', 'badkamervuur dif = ' . $difbadkamer);
    } elseif ($difbadkamer >= 0 && $badkvuur == "On" && $Tbadkvuur < time - 45) {
        sw($Ibadkvuur, 'Off', 'badkamervuur dif = ' . $difbadkamer);
    } elseif ($difbadkamer >= -0.1 && $badkvuur == "On" && $Tbadkvuur < time - 90) {
        sw($Ibadkvuur, 'Off', 'badkamervuur dif = ' . $difbadkamer);
    } elseif ($difbadkamer >= -0.3 && $badkvuur == "On" && $Tbadkvuur < time - 150) {
        sw($Ibadkvuur, 'Off', 'badkamervuur dif = ' . $difbadkamer);
    }
    /* ==== Zonneluifel ==== */
    $maxbuien = 20;
    $maxwolken = 80;
    $zonopen = 1500;
    $zontoe = 200;
    if ($windrichting > 240 && $windrichting < 300) {
        $maxwind = 6;
    } else {
        $maxwind = 8;
    }
    /*if($s['luifel']!='Open'&&($wind>=$maxwind||$buienradar>=$maxbuien||$wolken>=$maxwolken||$s['zon']<$zontoe)){
    	print stamp.'  --- Luifel: Wind='.$wind.'|Buien='.round($buienradar,0).'|Zon='.$s['zon'].'|Luifel='.$s['luifel'].'|Last='.$t['luifel'].PHP_EOL;
    	if($wind>=$maxwind){
    		$msg='Luifel dicht, wind '.$wind .'m/s @ '.$windrichting.'°';
    		sw($i['luifel'],'Off',$msg);
    		telegram($msg,true,'channel');
    		if(strtotime($t['luifel'])<eenuur)sw($i['luifel'],'Off',$msg.' repeat');
    	}elseif($buienradar>=$maxbuien){
    		$msg='Luifel dicht, '.round($buienradar,0) .'% regen voorspeld';
    		sw($i['luifel'],'Off',$msg);
    		telegram($msg,true,'channel');
    		if(strtotime($t['luifel'])<eenuur)sw($i['luifel'],'Off',$msg.' repeat');
    	}elseif($wolken>=$maxwolken){
    		$msg='Luifel dicht, '.round($wolken,0) .'% wolken';
    		sw($i['luifel'],'Off',$msg);
    		telegram($msg,true,'channel');
    		if(strtotime($t['luifel'])<eenuur)sw($i['luifel'],'Off',$msg.' repeat');
    	}elseif($s['zon']<$zontoe){
    		$msg='Luifel dicht, geen zon meer';
    		sw($i['luifel'],'Off',$msg);
    		telegram($msg,true,'channel');
    		if(strtotime($t['luifel'])<eenuur)sw($i['luifel'],'Off',$msg.' repeat');
    	}
    }elseif($s['luifel']!='Closed'&&time>strtotime('10:25')&&$wind<$maxwind-1&&$buienradar<$maxbuien-1&&$wolken<$maxwolken-5&&$s['living_temp']>20&&$s['zon']>$zonopen&&strtotime($t['luifel'])<tienmin){
    	print stamp.'  --- Luifel: Wind='.$wind.'|Buien='.round($buienradar,0).'|Zon='.$s['zon'].'|Luifel='.$s['luifel'].'|Last='.$t['luifel'].PHP_EOL;
    		$msg='Luifel open '.stamp;
    		sw($i['luifel'],'On',$msg);
    		telegram($msg,true,'channel');
    		$s['luifel']=='Open';
    }
    if(strtotime($t['luifel'])<eenuur){$msg='Repeat luifel '.$s['luifel'];sw($i['luifel'],$s['luifel'],$msg.' repeat');telegram($msg,true,'Guy');}
    */
    if (cget('time-cron') < eenmin) {
        cset('time-cron', time);
        $items = array('eettafel', 'zithoek', 'tobi', 'kamer', 'alex');
        foreach ($items as $item) {
            if ($s[$item] != 'Off') {
                if (strtotime($t[$item]) < dertigsec) {
                    $action = cget('dimmer' . $item);
                    if ($action == 1) {
                        $level = filter_var($s[$item], FILTER_SANITIZE_NUMBER_INT);
                        $level = floor($level * 0.95);
                        if ($level < 2) {
                            $level = 0;
                        }
                        if ($level == 20) {
                            $level = 19;
                        }
                        sl($i[$item], $level, $item);
                        if ($level == 0) {
                            cset('dimmer' . $item, 0);
                        }
                    } elseif ($action == 2) {
                        $level = filter_var($s[$item], FILTER_SANITIZE_NUMBER_INT);
                        $level = $level + 2;
                        if ($level == 20) {
                            $level = 21;
                        }
                        if ($level > 30) {
                            $level = 30;
                        }
                        sl($i[$item], $level, $item);
                        if ($level == 30) {
                            cset('dimmer' . $item, 0);
                        }
                    }
                }
            }
        }
        //Water duiven
        //if(time>strtotime('5:00')&&time<strtotime('6:30')&&strtotime($t['water'])<(time-rand(300,900)))sw($i['water'],'On','duiven wegjagen');
        //if(time>strtotime('6:30')&&time<strtotime('21:30')&&strtotime($t['water'])<(time-rand(900,3600)))sw($i['water'],'On','duiven wegjagen');
        //if(time>strtotime('21:30')&&time<strtotime('22:50')&&strtotime($t['water'])<(time-rand(300,900)))sw($i['water'],'On','duiven wegjagen');
        /*if(time>strtotime('22:00')&&time<strtotime('22:30')){
        		if($s['water']=='Off'&&$buienradar<2)sw($i['water'],'On','planten');
        	}elseif(time>strtotime('22:30')&&$s['water']=='On')sw($i['water'],'Off','planten');
        	*/
        if ($s['pirlivingR'] != 'Off' && strtotime($t['pirlivingR']) < eenmin) {
            sw($i['pirlivingR'], 'Off', 'Reset pirlivingR');
        }
        if ($s['pirgarage'] == 'Off' && $s['poort'] == 'Closed' && strtotime($t['pirgarage']) < driemin && strtotime($t['poort']) < driemin && strtotime($t['garage']) < eenmin && $s['garage'] == 'On' && $s['lichten_auto'] == 'On') {
            sw($i['garage'], 'Off', 'licht garage');
        }
        if (strtotime($t['pirinkom']) < tweemin && strtotime($t['pirhall']) < tweemin && strtotime($t['inkom']) < tweemin && strtotime($t['hall']) < tweemin && $s['lichten_auto'] == 'On') {
            if ($s['inkom'] == 'On') {
                sw($i['inkom'], 'Off', 'licht inkom');
            }
            if ($s['hall'] == 'On') {
                sw($i['hall'], 'Off', 'licht hall');
            }
        }
        if (strtotime($t['pirkeuken']) < tweemin && strtotime($t['wasbak']) < tweemin && $s['pirkeuken'] == 'Off' && $s['wasbak'] == 'On' && $s['werkblad'] == 'Off' && $s['keuken'] == 'Off' && $s['kookplaat'] == 'Off') {
            sw($i['wasbak'], 'Off', 'wasbak pir keuken');
        }
        if ($s['weg'] == 'Off' && $s['slapen'] == 'Off') {
            $items = array('GroheRed', 'poortrf', 'denonpower');
            foreach ($items as $item) {
                if ($s[$item] == 'Off') {
                    sw($i[$item], 'On', $item);
                }
            }
        }
        if (time >= strtotime('7:00') && time <= strtotime('23:45')) {
            $fc = json_decode(curl('https://api.forecast.io/forecast/2e43c1ed64ff79afc81f329285103310/50.1925552,3.8140885?units=si'), true);
        }
        if (isset($fc['currently'])) {
            if ($fc['currently']['temperature'] != 0) {
                $newtemp = round($fc['currently']['temperature'] + 2.1, 1);
            } else {
                $newtemp = round($buiten_temp, 1);
            }
            $newbuien = $fc['currently']['precipProbability'] * 100;
            $newwolken = round($fc['currently']['cloudCover'] * 100, 0);
            $newwind = round($fc['currently']['windSpeed'], 0);
            $newwindrichting = round($fc['currently']['windBearing'], 0);
        } else {
            $openweathermap = json_decode(curl('http://api.openweathermap.org/data/2.5/weather?id=2790729&APPID=ac3425b0bf0a02a80d2525db6215021d&units=metric'), true);
        }
        if (isset($openweathermap['main'])) {
            if ($openweathermap['main']['temp'] != 0) {
                $newtemp = round($openweathermap['main']['temp'] + 1.3, 1);
            } else {
                $newtemp = round($buiten_temp, 1);
            }
            $newwind = round($openweathermap['wind']['speed'], 0);
            $newwindrichting = round($openweathermap['wind']['deg'], 0);
            $newwolken = round(($wolken + $openweathermap['clouds']['all']) / 2, 0);
        }
        $rains = curl('http://gps.buienradar.nl/getrr.php?lat=50.1925552&lon=3.8140885');
        $rains = str_split($rains, 11);
        $totalrain = 0;
        $aantal = 0;
        foreach ($rains as $rain) {
            $aantal = $aantal + 1;
            $totalrain = $totalrain + substr($rain, 0, 3);
            $buien = $totalrain / $aantal;
            if ($aantal == 7) {
                break;
            }
        }
        if (isset($newbuien)) {
            $newbuienradar = round(max($newbuien, $buien), 0);
            if ($newbuienradar > 100) {
                $newbuienradar = 100;
            }
        } else {
            $newbuienradar = round($buien);
        }
        if (isset($newtemp)) {
            $weer['buiten_temp'] = $newtemp;
        }
        if (isset($newbuienradar)) {
            $weer['buien'] = $newbuienradar;
        }
        if (isset($newwind)) {
            $weer['wind'] = $newwind;
        }
        if (isset($newwindrichting)) {
            $weer['windrichting'] = $newwindrichting;
        }
        if (isset($newwolken)) {
            $weer['wolken'] = $newwolken;
        }
        $uweer = serialize($weer);
        cset('weer', $uweer);
        curl('http://127.0.0.1/secure/logwrite.php?device=temp&buiten=' . $buiten_temp . '&living=' . $s['living_temp'] . '&badkamer=' . $s['badkamer_temp'] . '&kamer=' . $s['kamer_temp'] . '&tobi=' . $s['tobi_temp'] . '&alex=' . $s['alex_temp'] . '&zolder=' . $s['zolder_temp']);
        $regenpomp = $s['regenpomp'];
        $Tregenpomp = strtotime($t['regenpomp']);
        if ($buienradar > 0) {
            $pomppauze = 14400 / $buienradar;
        } else {
            $pomppauze = 14400;
        }
        if ($regenpomp == 'On' && $Tregenpomp < eenmin) {
            sw($i['regenpomp'], 'Off', 'regenpomp');
        } elseif ($regenpomp == 'Off' && $Tregenpomp < time - $pomppauze) {
            sw($i['regenpomp'], 'On', 'regenpomp');
        }
        $items = array(3 => 'media', 7 => 'hallzolder', 8 => 'lichtbadkamer', 9 => 'badkamervuur', 12 => 'inkomvoordeur', 13 => 'brander', 15 => 'bureeltobi', 22 => 'werkbladtuin', 23 => 'water', 24 => 'garageterras', 49 => 'keukenzolderg', 56 => 'grohered', 57 => 'fanvestiaire', 58 => 'tuinpomp', 59 => 'zwembad', 61 => 'wasbakkookplaat', 65 => 'media');
        foreach ($items as $item => $name) {
            if (cget('timerefresh-' . $name) < time - rand(1800, 2700)) {
                RefreshZwave($item, 'time', $name);
                break;
            }
        }
        if (date('i', time) % 5 == 0) {
            if ($s['voordeur'] == 'On' && strtotime($t['voordeur']) < tienmin) {
                sw($i['voordeur'], 'Off', 'Voordeur uit');
            }
            if ($s['lichten_auto'] == 'Off') {
                if (strtotime($t['lichten_auto']) < drieuur) {
                    sw($i['lichten_auto'], 'Off', 'lichten_auto aan');
                }
            }
            if ($s['weg'] == 'On' || $s['slapen'] == 'On') {
                $lastoff = cget('timelastoff');
                if ($lastoff < tienmin) {
                    if (strtotime($t['weg']) > eenmin || strtotime($t['slapen']) > eenmin) {
                        $uit = 60;
                    } else {
                        $uit = 600;
                    }
                    if ($s['weg'] == 'On') {
                        alles('Off', $uit);
                    }
                    if ($s['slapen'] == 'On') {
                        alles('Slapen', $uit);
                    }
                    $items = array('living', 'badkamer', 'kamer', 'tobi', 'alex');
                    foreach ($items as $item) {
                        ${'setpoint' . $item} = cget('setpoint' . $item);
                        if (${'setpoint' . $item} != 0 && strtotime($t[$item]) < eenuur) {
                            cset('setpoint' . $item, 0);
                        }
                    }
                    cset('timelastoff', time);
                }
                if (strtotime($t['weg']) < eenmin) {
                    if ($s['poortrf'] == 'On') {
                        sw($i['poortrf'], 'Off', 'Poort uit');
                    }
                }
            }
            if ($s['kodi'] == 'On' && strtotime($t['kodi']) < vijfmin) {
                $devcheck = 'Kodi';
                if (pingDomain('192.168.0.7', 1597) == 1) {
                    $prevcheck = cget('check' . $devcheck);
                    if ($prevcheck > 0) {
                        cset('check' . $devcheck, 0);
                    }
                } else {
                    $check = cget('check' . $devcheck) + 1;
                    if ($check > 0) {
                        cset('check' . $devcheck, $check);
                    }
                    if ($check == 3) {
                        sw($i['kodi'], 'Off', 'kodi');
                    }
                }
            }
            $devcheck = 'PiCam1-Voordeur';
            if (pingDomain('192.168.0.11', 80) == 1) {
                $prevcheck = cget('check' . $devcheck);
                if ($prevcheck >= 3) {
                    telegram($devcheck . ' online', true, 'channel');
                }
                if ($prevcheck > 0) {
                    cset('check' . $devcheck, 0);
                }
            } else {
                $check = cget('check' . $devcheck) + 1;
                if ($check > 0) {
                    cset('check' . $devcheck, $check);
                }
                if ($check == 3) {
                    telegram($devcheck . ' Offline', true, 'channel');
                }
                if ($check % 100 == 0) {
                    telegram($devcheck . ' nog steeds Offline', true, 'channel');
                }
            }
            $devcheck = 'PiCam2-Alex';
            if (pingDomain('192.168.0.12', 80) == 1) {
                $prevcheck = cget('check' . $devcheck);
                if ($prevcheck >= 3) {
                    telegram($devcheck . ' online', true, 'channel');
                }
                if ($prevcheck > 0) {
                    cset('check' . $devcheck, 0);
                }
            } else {
                $check = cget('check' . $devcheck) + 1;
                if ($check > 0) {
                    cset('check' . $devcheck, $check);
                }
                if ($check == 3) {
                    telegram($devcheck . ' Offline', true, 'channel');
                }
                if ($check % 100 == 0) {
                    telegram($devcheck . ' nog steeds Offline', true, 'channel');
                }
            }
            $devcheck = 'PiCam3-Oprit';
            if (pingDomain('192.168.0.13', 80) == 1) {
                $prevcheck = cget('check' . $devcheck);
                if ($prevcheck >= 3) {
                    telegram($devcheck . ' online', true, 'channel');
                }
                if ($prevcheck > 0) {
                    cset('check' . $devcheck, 0);
                }
            } else {
                $check = cget('check' . $devcheck) + 1;
                if ($check > 0) {
                    cset('check' . $devcheck, $check);
                }
                if ($check == 3) {
                    telegram($devcheck . ' Offline', true, 'channel');
                }
                if ($check % 100 == 0) {
                    telegram($devcheck . ' nog steeds Offline', true, 'channel');
                }
            }
            $devcheck = 'PiHole-DNS';
            if (pingDomain('192.168.0.2', 53) == 1) {
                $prevcheck = cget('check' . $devcheck);
                if ($prevcheck >= 3) {
                    telegram($devcheck . ' online', true, 'channel');
                }
                if ($prevcheck > 0) {
                    cset('check' . $devcheck, 0);
                }
            } else {
                $check = cget('check' . $devcheck) + 1;
                if ($check > 0) {
                    cset('check' . $devcheck, $check);
                }
                if ($check == 3) {
                    telegram($devcheck . ' Offline', true, 'channel');
                }
                if ($check % 100 == 0) {
                    telegram($devcheck . ' nog steeds Offline', true, 'channel');
                }
            }
            $devcheck = 'PiHole-WWW';
            if (pingDomain('192.168.0.2', 80) == 1) {
                $prevcheck = cget('check' . $devcheck);
                if ($prevcheck >= 3) {
                    telegram($devcheck . ' online', true, 'channel');
                }
                if ($prevcheck > 0) {
                    cset('check' . $devcheck, 0);
                }
            } else {
                $check = cget('check' . $devcheck) + 1;
                if ($check > 0) {
                    cset('check' . $devcheck, $check);
                }
                if ($check == 3) {
                    telegram($devcheck . ' Offline', true, 'channel');
                }
                if ($check % 100 == 0) {
                    telegram($devcheck . ' nog steeds Offline', true, 'channel');
                }
            }
            if (($s['weg'] == 'On' || $s['slapen'] == 'On') && $s['meldingen'] == 'On' && strtotime($t['weg']) < driemin && strtotime($t['slapen']) < driemin) {
                $devices = array('raamliving', 'pirliving', 'pirlivingR', 'pirkeuken', 'pirgarage', 'pirinkom');
                foreach ($devices as $device) {
                    if (!in_array($s[$device], array('Closed', 'Off'))) {
                        if (cget('timealert' . $device) < eenmin) {
                            sw($i['sirene'], 'On', 'SIRENE');
                            cset('timealert' . $device, time);
                            $msg = $device . ' ' . $s[$device] . ' om ' . $t[$device];
                            telegram($msg, true, 'channel');
                            ios($msg);
                            if (sms === true) {
                                sms($msg);
                            }
                        }
                    }
                }
            }
            if (strtotime($t['tobi_set']) < eendag) {
                ud($i['tobi_set'], 0, $s['tobi_set'], 'Update tobi');
            }
            if (strtotime($t['living_set']) < eendag) {
                ud($i['living_set'], 0, $s['living_set'], 'Update living');
            }
            if (strtotime($t['kamer_set']) < eendag) {
                ud($i['kamer_set'], 0, $s['kamer_set'], 'Update kamer');
            }
            if (strtotime($t['alex_set']) < eendag) {
                ud($i['alex_set'], 0, $s['alex_set'], 'Update alex');
            }
            if (cget('timenetworkheal') < twaalfuur && time > strtotime('15:00')) {
                curl(api . 'json.htm?type=command&param=zwavenetworkheal&idx=4');
                cset('timenetworkheal', time);
            }
            $items = array('brander', 'badkamervuur');
            foreach ($items as $item) {
                if ($s[$item] != 'Off' && strtotime($t[$item]) < eenuur) {
                    sw($i[$item], $s[$item], $item);
                }
            }
            if ($s['meldingen'] == 'Off' && strtotime($t['meldingen']) < drieuur) {
                sw($i['meldingen'], 'On', 'meldingen');
            }
            if (strtotime($t['pirliving']) < vieruur && strtotime($t['pirlivingR']) < vieruur && strtotime($t['pirgarage']) < vieruur && strtotime($t['pirinkom']) < vieruur && strtotime($t['pirhall']) < vieruur && strtotime($t['slapen']) < vieruur && strtotime($t['weg']) < vieruur && $s['weg'] == 'Off' && $s['slapen'] == "Off") {
                sw($i['slapen'], 'On', 'wakker1');
                if ($s['slapen'] == 'Off') {
                    telegram('slapen ingeschakeld na 4 uur geen beweging', false, 'channel');
                } else {
                    telegram('slapen ingeschakeld na 4 uur geen beweging', true, 'channel');
                }
            }
            if (strtotime($t['pirliving']) < twaalfuur && strtotime($t['pirlivingR']) < twaalfuur && strtotime($t['pirgarage']) < twaalfuur && strtotime($t['pirinkom']) < twaalfuur && strtotime($t['pirhall']) < twaalfuur && strtotime($t['slapen']) < twaalfuur && strtotime($t['weg']) < twaalfuur && $s['weg'] == 'Off' && $s['slapen'] == "On") {
                sw($i['slapen'], 'Off', 'wakker2');
                sw($i['weg'], 'On', 'weg');
                if ($s['slapen'] == 'Off') {
                    telegram('weg ingeschakeld na 12 uur geen beweging', false, 'channel');
                } else {
                    telegram('weg ingeschakeld na 12 uur geen beweging', true, 'channel');
                }
            }
            if ($buienradar < 2 && $s['regenpomp'] == 'On') {
                sw($i['regenpomp'], 'Off', 'regenpomp');
            }
            if ($s['zwembadfilter'] == 'On') {
                if (strtotime($t['zwembadfilter']) < vieruur && time > strtotime("18:00") && $s['zwembadwarmte'] == 'Off') {
                    sw($i['zwembadfilter'], 'Off', 'zwembadfilter');
                }
            } else {
                if (strtotime($t['zwembadfilter']) < vieruur && time > strtotime("12:00") && time < strtotime("16:00")) {
                    sw($i['zwembadfilter'], 'On', 'zwembadfilter');
                }
            }
            if ($s['zwembadwarmte'] == 'On') {
                if (strtotime($t['zwembadwarmte']) < eendag) {
                    sw($i['zwembadwarmte'], 'Off', 'warmtepomp zwembad');
                }
                if ($s['zwembadfilter'] == 'Off') {
                    sw($i['zwembadfilter'], 'On', 'zwembadfilter');
                }
            }
            if ($s['meldingen'] == 'On') {
                $items = array('living_temp', 'badkamer_temp', 'kamer_temp', 'tobi_temp', 'alex_temp', 'zolder_temp');
                $avg = 0;
                foreach ($items as $item) {
                    $avg = $avg + $s[$item];
                }
                $avg = $avg / 6;
                foreach ($items as $item) {
                    $temp = $s[$item];
                    if ($temp > $avg + 5 && $temp > 25) {
                        $msg = 'T ' . $item . '=' . $temp . '°C. AVG=' . round($avg, 1) . '°C';
                        if (cget('timealerttemp' . $item) < eenuur) {
                            telegram($msg, false, 'channel');
                            ios($msg);
                            if (sms === true) {
                                sms($msg);
                            }
                            cset('timealerttemp' . $item, time);
                        }
                    }
                    if (strtotime($t[$item]) < vieruur) {
                        if (cget('timealerttempupd' . $item) < twaalfuur) {
                            telegram($item . ' not updated');
                            cset('timealerttempupd' . $item, time);
                        }
                    }
                }
            }
            $devices = array('tobiZ', 'alexZ', 'livingZ', 'livingZZ', 'livingZE', 'kamerZ');
            foreach ($devices as $device) {
                if (strtotime($t[$device]) < vieruur) {
                    if (cget('timealert' . $device) < twaalfuur) {
                        telegram($device . ' geen communicatie');
                        cset('timealert' . $device, time);
                    }
                }
            }
            if ($s['weg'] == 'Off' && $s['slapen'] == 'Off') {
                if ($buiten_temp > $s['kamer_temp'] && $buiten_temp > $s['tobi_temp'] && $buiten_temp > $s['alex_temp'] && $buiten_temp > 22 && ($s['kamer_temp'] > 20 || $s['tobi_temp'] > 20 || $s['alex_temp'] > 20) && ($s['raamkamer'] == 'Open' || $s['raamtobi'] == 'Open' || $s['raamalex'] == 'Open')) {
                    if ((int) cget('timeramen') < twaalfuur) {
                        telegram('Ramen boven dicht doen, te warm buiten. Buiten = ' . $buiten_temp . ',kamer = ' . $s['kamer_temp'] . ', Tobi = ' . $s['tobi_temp'] . ', Alex = ' . $s['alex_temp'], false, 'channel');
                        cset('timeramen', time);
                    } elseif (($buiten_temp <= $s['kamer_temp'] || $buiten_temp <= $s['tobi_temp'] || $buiten_temp <= $s['alex_temp']) && ($s['kamer_temp'] > 20 || $s['tobi_temp'] > 20 || $s['alex_temp'] > 20) && ($s['raamkamer'] == 'Closed' || $s['raamkamer'] == 'Closed' || $s['raamkamer'] == 'Closed')) {
                        if ((int) cget('timeramen') < twaalfuur) {
                            telegram('Ramen boven open doen, te warm binnen. Buiten = ' . $buiten_temp . ',kamer = ' . $s['kamer_temp'] . ', Tobi = ' . $s['tobi_temp'] . ', Alex = ' . $s['alex_temp'], false, 'channel');
                            cset('timeramen', time);
                        }
                    }
                }
            }
            include 'gcal/gcal.php';
        }
    }
}
         RefreshZwave(22, 'cron');
     }
     if ($mc->get('RefreshZwave23') < $tienmin - mt_rand(0, 600)) {
         RefreshZwave(23, 'cron');
     }
     if ($mc->get('RefreshZwave24') < $tienmin - mt_rand(0, 600)) {
         RefreshZwave(24, 'cron');
     }
     if ($mc->get('RefreshZwave25') < $tienmin - mt_rand(0, 600)) {
         RefreshZwave(25, 'cron');
     }
     if ($mc->get('RefreshZwave35') < $tienmin - mt_rand(0, 600)) {
         RefreshZwave(35, 'cron');
     }
     if ($mc->get('RefreshZwave79') < $tienmin - mt_rand(0, 600)) {
         RefreshZwave(79, 'cron');
     }
     //Motion detection on PiCam
     //if(($Sweg=='On'||$Sslapen=='On')&&($STweg>$tweemin||$STslapen>$tweemin||$STmeldingen<$eenuur)) curl('http://picam1:8080/0/detection/start');
     //else if ($Sweg=='Off'&&$Sslapen=='Off'&&($STweg>$tweemin||$STslapen>$tweemin||$STmeldingen<$eenuur)) curl('http://picam1:8080/0/detection/pause');
     if ($mc->get('gcal') < $vijfmin) {
         if (php_sapi_name() == 'cli') {
             include 'gcal/gcal.php';
         }
         $mc->set('gcal', $time);
     }
     unset($rain, $rains, $avg, $Set, $openweathermap, $Type, $SwitchType, $SubType, $name, $client, $results, $optParams, $service, $event, $calendarId);
 }
 //END ALL
 $execution = microtime(true) - $starttime;
 $phptime = $execution - $domotime;