Time to cook: 120min
Difficulty: +++++

The Nest thermostat is one of the most frequently cited devices when we talk about ‘Smart Connected’ products. It is a smart thermostat developed by Nest Labs and designed by Tony Fadell & Matt Rogers (ex-Apple). A first version was released end 2011. It caused a revolution in the HVAC landscape. In 2014 Google acquired the Nest Thermostat for about $3.4 billion. End 2018 more than 11 million Nest thermostats have been sold.
This recipe add’s a software version of the Nest Thermostat. The software version looks as cool as the physical one. I found the look Alike Nest thermostat as a project from Dal Hundal.
This project is ideal to replace an existing thermostat in your home, and turn it into a connected thermostat and beyond. What we will need is a temperature sensor to measure the ambient temperature and a relay capable of actuating our HVAC installation. We can use the device built in recipe ‘build a connected thermostat’ All the rest is software.
Ingredients
Node-red-dashboard
Look-a-like nest thermostat Node-RED flow
Prerequisites
Cook recipe ‘Building a connected thermostat’
Cook recipe ‘Installing Node RED’
Tools
The Building plan
Import the Node-red-Nest-thermostat flow
Login to your Node-RED environment
If not done yet, install the dashboard nodes
Download the Look-a-like nest thermostat Node-RED flow from Github
Extract the flow.json file
Import the file on a new flow in your Node-RED environment. You will see the following:

This is a sample set up of the Look-a-Like Nest thermostat. The inject nodes on the left side allow you to test out the functionality. We will later replace them with real-time data coming from our sensors or other logic.
What data can you push to the widget?
ambient_temperature your temperature readings numeric payloads.
target_temperature your thermostat setpoint numeric payloads.
hvac_state string (off, heating, cooling) payload.
has_leave boolean (true, false) payloads.
away boolean (true, false) payloads.
On GitHub you can find some more (advanced) options you can set but the above list are the most important.
Next, we are going to adapt the Sample flow for our use case.
First we are going to replace the ambient_temperature inject node by real-time data coming from our temperature sensor.
Aggregating data from the temperature sensor
In this recipe we are going to use the temperature sensor connected to the Arduino MKR1010 which is covered in one of the previous recipes. The device is connected to AllThingsTalk maker, where we created a digital representative. We can use MQTT to retrieve the temperature data.
We need to add an MQTT input node to our flow and configure it to listen to the temperature feed topic (see AllThingsTalk Maker in chapter 2 of the connected house cookbook).
Drag & drop an mqtt input node on the flow
configure the mqtt server
in Connection tab, enter in the server field: api.allthingstalk.io
In the Security tab, enter your device token in the username field
In the Password field, enter a random password (eg fake)
click Done
note: We only need a token to connect to the AllthingsTalk mtt broker, but the Mqtt node does not like that we don’t specify a password, that is why we must enter a fake one.
In Properties in the Topic field, enter the temperature feed topic: device/<deviceId>/asset/temperature/feed
Click Done
Replace <deviceId> with your device ID.
The payload received on the feed topic looks like this:
{"at":"2020-04-13T14:49:46.095755Z","value":26.0}
We are going to add a json parser node and function node to retrieve the temperature value
The nodes we added look as follows:


Set Up the Actuation
Next we are going to set up the actuation. For this we need to send an MQTT message on the command topic of the relay of our Arduino MKR1010 device
Drag & drop an mqtt output node on the flow
Select the mqtt server, you have configured on the mqtt input node
In Properties in the Topic field, enter the relay command topic:
device/<deviceId>/asset/relay/cmd
Click Done
Replace <deviceId> with your device ID.
Adding the Control logic
We have now to add the logic in the sample Look-a-like node-RED flow to control the HVAC system. The Data function node in the sample Look-a-like node-RED flow sends out an update each time the Ambient temperature, target temperature, hvac_state and Away state changes.
The content of such an update message is given below:

This is the ideal candidate to trigger our controller based on some criteria.
The simplest logic we could think of is to turn on the heating when target temperature is > ambient temperature.
We will create a new function node with the name ‘controller’ which gets the necessary information and includes the logic to trigger the actuator. As the actuator is from the type Switch_binary in openHAB, it needs to receive the payload ON or OFF.
The content of the function node is given below:
var actuate_state = global.get("nest1_ambient_actuate") || "OFF"; if (global.get("nest1_ambient_temperature") < global.get("nest1_target_temperature")) { msg = { payload:"ON" }; } else { msg = { payload:"OFF" }; } if (actuate_state != msg.payload) { global.set("nest1_ambient_actuate",msg.payload); return msg; }
We introduce a new global variable which is used to verify the previous actuation state. This prevents that we repeat sending the same states.
Connect the controller function node with the output of the data function node
Connect the controller function node with the input of the openhab2_out node
Adding some more logic:
Now we need to configure a few more things to make the system work as we want.
Feedback the setpoint from the UI
When we set the setpoint on the UI, this information needs to feed back to the target_temperature function.
Create a function node with name ‘Update Target temp’ and the following content
if (msg.topic == "target_temperature") { msg.payload = msg.payload.target_temperature; return msg; }
Connect the input of the Update Target temp’ node with the output of the ‘New setpoint from UI’ function node
Connect the output of the Update Target temp’ node with the input of the ‘target_temperature’ function node
Hvac state
Next we are going to implement the actuation feedback from the controller and set the hvac_state accordingly.
When the controller state = “OFF” the hvac_state should be “off” and when the controller state = “ON”, the hvac_state should become “heating”. To achieve that we are going to add an openHAB2-in node which feeds us the state of our SSR303 Zwave controller and we are going to add a change node to translate the “ON” and “OFF” state from our controller into “off” and “heating” for the hvac_state.
Drag and drop the openHAB2 in node on the flow
Select the Controller you have previously configured
Select the item, representing the relay from your controller and click on Done
The node will report the relay state from the controller.
Now Drag & drop a switch node and add 2 rules as shown in the image below

Wire the openhab2-in node with the input of the switch node and wire the Switch node towards the input of the hvac_state function node.
Default states
We are going to add 2 buttons on the dashboard to set the control state between ‘Home’ and ‘Away’. This will set the control state (AWAY or not) and set the default target temperature
HOME = Default ‘High’ temperature (eg 21°C)
AWAY = Default ‘Low’ temperature (eg 18°C)
Drag & Drop a button Node on the flow and label it ‘HOME’
Enter the Topic: away
Set the Payload: false
Click Done

wire the HOME button Node with the input of the away function
Repeat the same steps for the AWAY button
Drag & Drop a button Node on the flow and label it ‘AWAY’
Enter the Topic: away
Set the Payload: true
Click Done
wire the AWAY button Node with the input of the away function
To set the default target temperature, we are going to add a function node which sets the default target temperature for the HOME and AWAY state.
Drag & Drop a function Node on the flow and label it ‘TargetHigh’
Enter the Topic: target_temperature
Set the Payload: 21
Click Done
msg.topic = 'target_temperature'; msg.payload = 21 return msg;

wire the TargetHigh function Node with the output of the Home button and the input of target_temperature function
Repeat the same steps for the AWAY button
Drag & Drop a function Node on the flow and label it ‘TargetLow’
Enter the Topic: target_temperature
Set the Payload: 18
Click Done
wire the TargetHigh function Node with the output of the Away button and the input of target_temperature function
Leaf state
As a last step, we are going to add the logic for the leaf state. The leaf state shows when the HVAC is set in an ecological mode. You can choose the barriers when you think this state is true for yourself. I will make the leaf appear when the target temperature is lower than 20°C
Drag & drop a function node on te Flow
Give the node a name.Eg: Set Leaf State
Add the following code:
if (msg.payload.target_temperature < 20){ msg.payload = true; } else { msg.payload = false; } msg.topic = "hvac_state"; return msg;
Click Done

Wire the input of the function node ‘Set Leaf State’ with the output of the function node ‘target_temperature’
Wire the output of the function node ‘Set Leaf State’ with the input of the function node ‘has_leaf’
Deploy the solution
Now we have configured everything, we can deploy the solution and test all functionality we have added.

Download the complete Flow here:
Comments