Snippets

Peter Scargill Thermostatic code see "node-red-thermostatic-function" on the blog tech.scargill.net

Created by Peter Scargill

File snippet.txt Added

  • Ignore whitespace
  • Hide word diff
+var stat=context.global.thermostat;
+var moment=context.global.moment;
+var mmt = moment();
+var mmtMidnight = mmt.clone().startOf('day');
+var today = mmt.clone().diff(mmtMidnight, 'minutes');
+var now = new Date();
+var thehour=now.getHours();
+var weekday=now.getDay();
+
+var heatOn="{out0:1}";
+var heatOff="{out0:0}";
+
+msg.topic="heatingrelay/toesp";
+
+if (stat.daynow!==thehour)
+    {
+     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
+
+var timeleft=0;
+var proceed=0;
+
+
+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; 
+    if (stat.hold===0) { 
+                          
+
+                        var thetemp;
+                        if (weekday==1)
+                        {
+                            stat.display_status="WEEK ";
+                            if (today>=stat.p5)
+                                {
+                                 thetemp=stat.t5;
+                                 stat.current_state=1;
+                                 timeleft=(1440-today)+stat.p1;
+                                }
+                            else if (today>=stat.p4)
+                            {
+                                thetemp=stat.t4;
+                                stat.current_state=2;
+                                timeleft=stat.p5-today;
+                            }
+                            else if (today>=stat.p3)
+                            {
+                                thetemp=stat.t3;
+                                stat.current_state=3;
+                                timeleft=stat.p4-today;
+                            }
+                            else if (today>=stat.p2)
+                            {
+                                thetemp=stat.t2;
+                                stat.current_state=4;
+                                timeleft=stat.p3-today;
+                            }
+                            else if (today>=stat.p1)
+                            {
+                                thetemp=stat.t1;
+                                stat.current_state=5;
+                                timeleft=stat.p2-today;
+                            }
+                            else
+                            {
+                                thetemp=stat.t5;
+                                stat.current_state=6;
+                                timeleft=stat.p1-today;
+                            }
+                        }
+                        else
+                        {
+                            stat.display_status="W/E ";
+                            if (today>=stat.pw5)
+                                {
+                                 thetemp=stat.tw5;
+                                 stat.current_state=1;
+                                 timeleft=(1440-today)+stat.pw1;
+                                }
+                            else if (today>=stat.pw4)
+                            {
+                                thetemp=stat.tw4;
+                                stat.current_state=2;
+                                timeleft=stat.pw5-today;
+                            }
+                            else if (today>=stat.pw3)
+                            {
+                                thetemp=stat.tw3;
+                                stat.current_state=3;
+                                timeleft=stat.pw4-today;
+                            }
+                            else if (today>=stat.pw2)
+                            {
+                                thetemp=stat.tw2;
+                                stat.current_state=4;
+                                timeleft=stat.pw3-today;
+                            }
+                            else if (today>=stat.pw1)
+                            {
+                                thetemp=stat.tw1;
+                                stat.current_state=5;
+                                timeleft=stat.pw2-today;
+                            }
+                            else
+                            {
+                                thetemp=stat.tw5;
+                                stat.current_state=6;
+                                timeleft=stat.pw1-today;
+                            }
+                        }
+                        stat.desired=thetemp;
+
+                        } 
+    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) 
+    {
+        stat.heating=1; 
+        msg.payload=heatOn; stat.display_status+=" HEAT ";
+        node.send(msg);
+        msg.topic="holly1/toesp";    
+        msg.payload="{rgb:0,200,110,0,4}";
+        node.send(msg);
+        }
+ else if  (stat.display_humidity>stat.max_humidity)
+    {
+        stat.heating=1; 
+        msg.payload=heatOn; stat.display_status+=" DRY ";
+        node.send(msg);
+        msg.topic="holly1/toesp";    
+        msg.payload="{rgb:0,0,180,0,4}";
+        node.send(msg);
+    }
+else 
+    {
+        stat.heating=0; 
+        msg.payload=heatOff;  stat.display_status+=" ";
+        node.send(msg);
+        msg.topic="holly1/toesp";    
+        msg.payload="{rgb:0,0,0,180,4}";
+        node.send(msg);
+    }
+
+stat.timeleft=timeleft;
+
+if (stat.hold>0)
+{
+    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"});
+}
+else
+{
+    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"});
+}
+
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.