Hello,I’m from China and I will try my best to describe what I can describe, So if there is any grammatical mistakes and objections, You can ask questions ,I’d like to share with you the low cost of support through homekit mode and stable control of air conditioning components. I found a lot of information, but i did not find a suitable solution. Many information limited to the designated brand air conditioning, Many information does not support the mode and temperature control, so I intend to find their own solutions. The simpler method needs me to buy “broadlink rm”,But I just want to control the air conditioner with infrared, do not want to spend money to buy that it.
I found a component called “MQTT HAVC” at home-assistant.io,After a brief study of node.js and MQTT,I wrote this script.
I believe all of you here are have been installed and debugged homeassistant, homebridge and LIRC. If not, there are many tutorials on the web.
Script structure:
Subscribe to the “study/ac/mode/set” and “study/ac/temperature/set” topics in “mqtt” to receive mode and temperature settings respectively.
If the received mqtt message heat / cool / auto / idle (mode selection) then put the corresponding state is written to “mode0” and irsend they correspond to the air-conditioning code I set.
For the temperature setting, for example: if the received mqtt message is “17.0”, then the information of “mode0” will be firstly queried. If “mode0 = heat”, irsend of “heat17.0” will be executed, and if “mode0 = cool “, then the implementation of” cool17.0”'s irsend, and so on, when we use the corresponding temperature and state of the irsend parameters written to the corresponding position to replace my" KEY “and” BTN “
I feel pretty easy to write my script is quite bloated, with a lot of “If elseif and if”, so you should understand.
If anyone can streamline my script through my thoughts, I say thank you very much. If your air-conditioning control range is not 17 ~ 30 degrees Celsius, then you need to increase or decrease the script inside the temperature to determine the value of the statement.
Do not forget to change the user name, password and hostname in the script.
After everything is ready,Then
npm install mqtt
node aircon.js
You can keep aircon.js open in the background via "screen -dmS aircon node aircon.js”.
Add components in the homeassistant.
- platform: mqtt
name: Study
modes:
- off
- cool
- heat
mode_command_topic: "study/ac/mode/set"
temperature_command_topic: "study/ac/temperature/set"
aircon.js
var username = 'mqtt';
var password = 'mqtt';
var hostname = '127.0.0.1';
var mode = 'study/ac/mode/set';
var temp = 'study/ac/temperature/set';
var url = 'mqtt://' + username + ':' + password + '@' + hostname;
var spawn = require('child_process').spawn;
var mode0;
console.log(url);
var client = mqtt.connect(url);
client.subscribe(mode);
client.subscribe(temp);
client.on('message', (topic, message) => {
const payload = message.toString()
if (payload === 'heat') {
console.log(`Now Mode '${payload}'`)
mode0 = "heat";
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_I'])
}
else if (payload === 'cool') {
console.log(`Now Mode '${payload}'`)
mode0 = "cool"
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_9'])
}
else if (payload === 'auto') {
console.log(`Now Mode '${payload}'`)
mode0 = "auto"
spawn('irsend', ['SEND_ONCE', 'ac', 'BTN_7'])
}
else if (payload === 'idle') {
console.log(`Now Mode '${payload}'`)
mode0 = "off"
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_CLOSE'])
}
else if (payload === '17.0') {
if (mode0 === 'heat') {
console.log(`17.0heat`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_E'])
}
else if (mode0 === 'cool') {
console.log(`17.0cool`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_0'])
}
else if (mode0 === 'auto') {
console.log(`17.0auto`)
spawn('irsend', ['SEND_ONCE', 'ac', 'BTN_0'])
}
}
else if (payload === '18.0') {
if (mode0 === 'heat') {
console.log(`18.0heat`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_F'])
}
else if (mode0 === 'cool') {
console.log(`18.0cool`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_1'])
}
else if (mode0 === 'auto') {
console.log(`18.0auto`)
spawn('irsend', ['SEND_ONCE', 'ac', 'BTN_1'])
}
}
else if (payload === '19.0') {
if (mode0 === 'heat') {
console.log(`19.0heat`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_G'])
}
else if (mode0 === 'cool') {
console.log(`19.0cool`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_2'])
}
else if (mode0 === 'auto') {
console.log(`19.0auto`)
spawn('irsend', ['SEND_ONCE', 'ac', 'BTN_2'])
}
}
else if (payload === '20.0') {
if (mode0 === 'heat') {
console.log(`20.0heat`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_H'])
}
else if (mode0 === 'cool') {
console.log(`20.0cool`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_3'])
}
else if (mode0 === 'auto') {
console.log(`20.0auto`)
spawn('irsend', ['SEND_ONCE', 'ac', 'BTN_3'])
}
}
else if (payload === '21.0') {
if (mode0 === 'heat') {
console.log(`21.0heat`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_I'])
}
else if (mode0 === 'cool') {
console.log(`21.0cool`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_4'])
}
else if (mode0 === 'auto') {
console.log(`21.0auto`)
spawn('irsend', ['SEND_ONCE', 'ac', 'BTN_4'])
}
}
else if (payload === '22.0') {
if (mode0 === 'heat') {
console.log(`22.0heat`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_J'])
}
else if (mode0 === 'cool') {
console.log(`22.0cool`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_5'])
}
else if (mode0 === 'auto') {
console.log(`22.0auto`)
spawn('irsend', ['SEND_ONCE', 'ac', 'BTN_5'])
}
}
else if (payload === '23.0') {
if (mode0 === 'heat') {
console.log(`23.0heat`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_K'])
}
else if (mode0 === 'cool') {
console.log(`23.0cool`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_6'])
}
else if (mode0 === 'auto') {
console.log(`23.0auto`)
spawn('irsend', ['SEND_ONCE', 'ac', 'BTN_6'])
}
}
else if (payload === '24.0') {
if (mode0 === 'heat') {
console.log(`24.0heat`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_L'])
}
else if (mode0 === 'cool') {
console.log(`24.0cool`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_7'])
}
else if (mode0 === 'auto') {
console.log(`24.0auto`)
spawn('irsend', ['SEND_ONCE', 'ac', 'BTN_7'])
}
}
else if (payload === '25.0') {
if (mode0 === 'heat') {
console.log(`25.0heat`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_M'])
}
else if (mode0 === 'cool') {
console.log(`25.0cool`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_8'])
}
else if (mode0 === 'auto') {
console.log(`25.0auto`)
spawn('irsend', ['SEND_ONCE', 'ac', 'BTN_8'])
}
}
else if (payload === '26.0') {
if (mode0 === 'heat') {
console.log(`26.0heat`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_N'])
}
else if (mode0 === 'cool') {
console.log(`26.0cool`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_9'])
}
else if (mode0 === 'auto') {
console.log(`26.0auto`)
spawn('irsend', ['SEND_ONCE', 'ac', 'BTN_9'])
}
}
else if (payload === '27.0') {
if (mode0 === 'heat') {
console.log(`27.0heat`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_O'])
}
else if (mode0 === 'cool') {
console.log(`27.0cool`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_A'])
}
else if (mode0 === 'auto') {
console.log(`27.0auto`)
spawn('irsend', ['SEND_ONCE', 'ac', 'BTN_A'])
}
}
else if (payload === '28.0') {
if (mode0 === 'heat') {
console.log(`28.0heat`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_P'])
}
else if (mode0 === 'cool') {
console.log(`28.0cool`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_B'])
}
else if (mode0 === 'auto') {
console.log(`28.0auto`)
spawn('irsend', ['SEND_ONCE', 'ac', 'BTN_B'])
}
}
else if (payload === '29.0') {
if (mode0 === 'heat') {
console.log(`29.0heat`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_Q'])
}
else if (mode0 === 'cool') {
console.log(`29.0cool`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_C'])
}
else if (mode0 === 'auto') {
console.log(`29.0auto`)
spawn('irsend', ['SEND_ONCE', 'ac', 'BTN_C'])
}
}
else if (payload === '30.0') {
if (mode0 === 'heat') {
console.log(`30.0heat`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_R'])
}
else if (mode0 === 'cool') {
console.log(`30.0cool`)
spawn('irsend', ['SEND_ONCE', 'ac', 'KEY_D'])
}
else if (mode0 === 'auto') {
console.log(`30.0auto`)
spawn('irsend', ['SEND_ONCE', 'ac', 'BTN_X'])
}
}
});