Turn off Wifi at Night

The wifi radio consumes a lot of battery power. Most people do not need it on while they are sleeping. This will turn off the wifi radio at night and turn it back on in the morning.

Currently this script is set up to turn off the wifi radio at 11:00 PM and turn it back on at 6:00 AM. You can set these times to whatever your normal sleep schedule is.

To use this on{X} Recipe, simply copy the code below and modify if needed.

/////////////////////////////////////////////////////////////////////
// Set the hours that you consider to be night. Use military time. //
/////////////////////////////////////////////////////////////////////
var startNight = 23;
var endNight = 6;

/////////////////////////////////////////////////////////////////////
// Begin night mode logic										  //
/////////////////////////////////////////////////////////////////////
function setDataRadios(value) {
	var enable = value == 'enable' ? true : false;
	try {
		console.log('Setting data to "' + value + '".');
		device.network.wifiEnabled  = enable;
		device.network.mobileDataEnabled = enable;
	}
	catch(e) {
		console.log(e.message);
	}
}

device.scheduler.setTimer({
	name: 'wifiNightMode', 
	time: 0,
	interval: 'hour', 
	exact: true },
	function () { 
		var currDate = new Date();
		var currHour = currDate.getHours();
		
		switch(currHour)
		{
			case startNight:
				device.localStorage.setItem('wasWifiOn', device.network.wifiEnabled);
				setWiFiRadio('disable');
				console.log('Turning off wifi');
				break;
			case endNight:
				if(device.localStorage.getItem('wasWifiOn') === 'true') {
					setWiFiRadio('enable');
					console.log('Turning on wifi');
				} else {
					console.log('Wifi was originally off: no action');
				}
				break;
			default:
				//console.log('No data action');
				break;
		}
	}
);

Recipe Wall

1,447

Views