Xiaomi Aqara Temp & Humidity sensor in NodeRED

It's cold. Perfect time to mess about with my heating system

Some time ago, I made a NEST alike smart controller for my thermostat. This device used NodeRED as the platform and up until now, it has been reading the temperature from a single DHT11 sensor. I have learned that I can integrate the Xiaomi’s Gateway into NodeRED without hardware modification, so adding the Aqara Temp & Humidity sensor in NodeRED is the next step. The best thing is, these are very small and use low power! At less than $10 you could have one in each room!

Xiaomi Aqara Temp & Humidity sensor in NodeRED

To use the Aqara Temp & Humidity sensor in NodeRED you will need the Xiaomi Gateway. I have covered this already in my previous tutorial. This one builds on that knowledge, so please make sure you are familiar with the concept. 

The sensor is capable of temperature and humidity reporting in 15 min intervals. In addition to this, there is a small button which will check the connectivity (sadly in Chinese) and will send the values on request. That information can be intercepted by the gateway and posted via a debug node:
Temperature

{
	"cmd": "report",
	"model": "sensor_ht",
	"sid": "158dxxxxxx",
	"short_id": 20028,
	"data": {
		"temperature": "2152"
	}
}

Humidity

{
	"cmd": "report",
	"model": "sensor_ht",
	"sid": "158dxxxxxxx",
	"short_id": 20028,
	"data": {
		"humidity": "4801"
	}
}

The two payloads contain the information as 4 digit integer. To obtain the true value, you have to divide the value by 1000. (in examples T: 21.52°C H:48.01%) If you are interested in F rather than C – you will need to convert according to the formula:

T(°F) = T(°C) * 1.8 + 32

There is a node responsible for collecting the information from the sensor and making it available as a payload in a more readable format, however, the node is lacking conversion from C to F

Just Values

{
	"temperature": 21.54,
	"humidity": 49.17,
	"pressure": null,
	"voltage": 2.975,
	"voltage_level": "high",
	"time": 1546734980991,
	"device": "Temp1"
}

The values also contain information about the battery level which is why I’m not going to do this manually with a function node.

Full Data

{
	"cmd": "report",
	"model": "sensor_ht",
	"sid": "158d00xxxxx",
	"short_id": 20028,
	"data": {
		"temperature": "2152"
	}
}
{
	"cmd": "report",
	"model": "sensor_ht",
	"sid": "158d000xxx",
	"short_id": 20028,
	"data": {
		"humidity": "4801"
	}
}

In this mode, the sensor data is returned as it would be directly from the gateway itself.

Charts, Temperature, Humidity and conversions

To display the data in a meaningful way, I’m going to use NodeRED dashboard. It’s easy to deploy I will have no problems getting the reading this way. The chart node requires an integer payload (number) and a separate topic for each value type. I will post 3 types of values: Temperature in °C in °F and Humidity in %.

To achieve this I have to first limit the rate of incoming messages (each time I get one with temperature and one with humidity) with a limit node (limit: 1 message in 3 sec). Then I have to recalculate the temperature in F, round the values (I really don’t care about decimal points) and split the message into three payloads. I can achieve this with a function node:

var h = Math.floor(msg.payload.humidity);
var c = Math.floor(msg.payload.temperature);
var f = Math.floor(c*1.8 + 32); 
var msg1 = { payload: c };
var msg2 = { payload: f };
var msg3 = {payload: h};
   
return [msg1, msg2, msg3];

To each payload, I have to add a unique topic and I can pass the values to the chart node.

Integration with NEST alike thermostat

2 sensors sharing the average temperature

Having 2 or more temperature sensors at home is beneficial. In my case, one is located upstairs (it’s warmer there) and the other is located near the entry door (the coldest area). I can now track the ambient temperature from both locations and prioritise one reading over another one.

Support NotEnoughTech
2 sensors – thermostat will prioritise reading from one over another thanks to the switch

I don’t have heating zones implemented yet as this will require a plumbing job, which I cannot afford right now, I can use the data from the temperature sensor to stop the heating when the desired location reaches the set temperature. In the evenings, I often work in my office (upstairs) so there is no reason to heat up the house more even if the temperature downstairs reports the value below the target.

I have introduced 2 switches which with a simple help of the control flow will prioritise the reading from one temperature sensor over the other.
To make sure both values are submitted in the same object – I have copied the values with change node into the msg.payload and modified the function node to:

flow.set('TempAmbient', msg.payload);
return msg;

Conclusion

Using the Aqara Temp & Humidity sensor in NodeRED is very simple and effective. This and the fact that the sensor uses low power Zigbee protocol, makes it a great portable device to monitor temperature changes across the house. The time will tell how long the sensor lasts on a single battery, but something tells me, months will pass before I have to pop the cover open to change the battery. I will be getting more sensors soon. At some point I want to map one sensor to each room and start collecting the data. In the future Aqara Temp & Humidity sensors will be controlling the radiators directly.

Support NotEnoughTech
A lot of time and effort goes into keeping NotEnoughTech alive! If my work helped you out, consider buying me a coffee or check out exclusive rewards available to Patreon supporters.
SHARE