+var stat=context.global.thermostat;
+var moment=context.global.moment;
+var mmtMidnight = mmt.clone().startOf('day');
+var today = mmt.clone().diff(mmtMidnight, 'minutes');
+var thehour=now.getHours();
+var weekday=now.getDay();
+msg.topic="heatingrelay/toesp";
+if (stat.daynow!==thehour)
+ if (stat.hold) stat.hold--;
+ stat.update=10; // update in a couple of secs -once an hour
+if ((weekday===0) || (weekday==6)) weekday=0; else weekday=1; // 0 if weekend, 1 if weekday
+if (stat.current_state!==stat.last_state) // if change of timezone or initially - set the desired temperature to stored setting ignoring user changes
+ stat.last_state=stat.current_state;
+ stat.temperature_offset=0;
+ stat.desired=stat.frost;
+ stat.display_status="WEEK ";
+ timeleft=(1440-today)+stat.p1;
+ else if (today>=stat.p4)
+ timeleft=stat.p5-today;
+ else if (today>=stat.p3)
+ timeleft=stat.p4-today;
+ else if (today>=stat.p2)
+ timeleft=stat.p3-today;
+ else if (today>=stat.p1)
+ timeleft=stat.p2-today;
+ timeleft=stat.p1-today;
+ stat.display_status="W/E ";
+ timeleft=(1440-today)+stat.pw1;
+ else if (today>=stat.pw4)
+ timeleft=stat.pw5-today;
+ else if (today>=stat.pw3)
+ timeleft=stat.pw4-today;
+ else if (today>=stat.pw2)
+ timeleft=stat.pw3-today;
+ else if (today>=stat.pw1)
+ timeleft=stat.pw2-today;
+ timeleft=stat.pw1-today;
+ else { stat.display_status="Frost"; thetemp=stat.frost; }
+if (stat.temperature_offset!==0) stat.display_status = "Override";
+if ((stat.desired+stat.temperature_offset)>stat.display_temperature)
+ msg.payload=heatOn; stat.display_status+=" HEAT ";
+ msg.topic="holly1/toesp";
+ msg.payload="{rgb:0,200,110,0,4}";
+ else if (stat.display_humidity>stat.max_humidity)
+ msg.payload=heatOn; stat.display_status+=" DRY ";
+ msg.topic="holly1/toesp";
+ msg.payload="{rgb:0,0,180,0,4}";
+ msg.payload=heatOff; stat.display_status+=" ";
+ msg.topic="holly1/toesp";
+ msg.payload="{rgb:0,0,0,180,4}";
+ if (stat.heating==1) clr="orange"; else clr="black";
+ node.status({fill:clr,shape:"dot",text:stat.display_status+parseInt(stat.hold) + " hour(s) at " +thetemp+"c"});
+ if (stat.heating==1) clr="red"; else clr="blue";
+ node.status({fill:clr,shape:"dot",text:stat.display_status+parseInt(timeleft/60)+"hrs " +(timeleft%60)+"mins at " +thetemp+"c"});