Files
HA-NerdFlows-Functions/climate/master-bedroom/processing.js
2023-09-28 12:28:27 -04:00

406 lines
13 KiB
JavaScript

node.log("Master Bedroom Climate: Processing Started")
// pull in the necessary information
const states = global.get('homeassistant.homeAssistant.states')
const allowed = states['input_boolean.master_bedroom_climate_protocol'].state
const ac = global.get('mb_aircon_installed', "diskCon")
const temp = global.get("tempStr")
const payload = msg.payload
const vacation = states['input_boolean.vacation_mode'].state
const highTemp = states['sensor.today_corrected_high_temp'].state
const dayTemp = states['input_number.master_bedroom_daytime_temp'].state
const nightTemp = states['input_number.master_bedroom_night_temp'].state
const bedTemp = states['input_number.master_bedroom_bedtime_temp'].state
const showerMode = states['input_boolean.shower_mode'].state
const nightVolume = states['input_number.master_bedroom_echo_dot_night_volume'].state
const fanMode = states['input_select.scheduled_climate_mode_master_bedroom_fan'].state
const acMode = states['input_select.scheduled_climate_mode_master_bedroom_aircon'].state
const sleeping = states['input_boolean.master_bedroom_sleeping'].state
const hotDay = states['input_boolean.hot_day'].state
const heatWarning = states["binary_sensor.heat_warning"].state
const showerCooldown = states["timer.shower_mode_cooldown"].state
const earlyNight = states["binary_sensor.early_night_mode"].state
const danger = states['binary_sensor.heat_warning'].attributes.danger
const meltdown = states['input_boolean.meltdown_protocol'].state
const coolingActive = states['input_boolean.master_bedroom_cooling_on'].state
const echoDotDND = 'switch.basement_echo_dot_do_not_disturb_switch'
node.log("Master Bedroom Climate: Constants Set")
// init variables
let setTemp = []
let setEco = []
let setHvac = []
let setFan = []
let setCool = []
let setSleep = []
let setDisplay = []
let time = []
let type = msg.type
let topic = msg.topic
let echoDotService = []
node.log("Master Bedroom Climate: Variables Defined")
// Sleep Switch Handling
if (type === 'sleep' && payload === 'off') {
setDisplay = 'turn_on'
echoDotService = 'turn_off'
if (coolingActive === 'on') {
time = 'night'
} else {
time = 'day'
}
} else if (type === 'sleep' && payload === 'on') {
setDisplay = 'turn_off'
echoDotService = 'turn_on'
time = 'bedtime'
} else {
time = msg.time
}
if (topic === 'mrbedroom-wakeup') {
setSleep = 'turn_off'
}
// Day Time
if (time === 'day') {
if (type === 'auto') {
setCool = 'turn_off'
}
if (earlyNight === 'off') {
setFan = "turn_off"
if (ac === 'on') {
if (danger === 'Extreme') {
setTemp = nightTemp
setEco = "eco"
setHvac = "cool"
} else if (hotDay === 'on' || heatWarning === 'on') {
setTemp = dayTemp
setEco = "eco"
setHvac = "cool"
} else {
setTemp = nightTemp
setEco = "eco"
setHvac = "off"
}
}
} else if (earlyNight === 'on') {
if (ac === 'on') {
if (danger === 'Extreme') {
setTemp === bedTemp
} else {
setTemp = nightTemp
}
if (fanMode === 'Fan') {
setFan = 'turn_on'
} else {
setFan = 'turn_off'
}
if (acMode === 'AC') {
setHvac = 'cool'
} else if (acMode === 'fan') {
setHvac = 'fan_only'
} else {
setHvac = 'off'
}
if (hotDay === 'on') {
setEco = 'off'
} else {
setEco = 'on'
}
}
}
// Night Time
} else if (time === 'night') {
if (type === 'auto') {
setCool = 'turn_on'
}
if (danger === 'Extreme') {
setTemp = bedTemp
} else {
setTemp = nightTemp
}
if (hotDay === 'on' || sleeping === 'on') {
setEco = 'none'
} else {
setEco = 'eco'
}
if (acMode === 'AC') {
setHvac = 'cool'
} else if (acMode === 'Fan') {
setHvac = 'fan_only'
} else {
setHvac = 'off'
}
if (type === 'sleep' && payload === 'off') {
setFan = 'turn_off'
} else if (fanMode === 'Fan') {
setFan = 'turn_on'
}
// Bed Time
} else if (time === 'bedtime') {
if (ac === 'on') {
setTemp = bedTemp
setEco = 'none'
if (acMode === 'AC') {
setHvac = 'cool'
} else if (acMode === 'Fan') {
setHvac = "fan_only"
}
}
if (fanMode === 'Fan') {
setFan = 'turn_on'
}
}
node.log("Master Bedroom Climate: Decision Logic Complete")
// Define message payloads
let sendFan = {
"payload": {
"domain": "fan",
"service": setFan,
"target": {
"entity_id": ["fan.master_bedroom_fan"]
},
"data": {}
}
}
let sendCool = {
"payload": {
"domain": "input_boolean",
"service": setCool,
"target": {
"entity_id": ["input_boolean.master_bedroom_cooling_on"]
},
"data": {}
}
}
let sendSleep = {
"payload": {
"domain": "input_boolean",
"service": setSleep,
"target": {
"entity_id": ["input_boolean.master_bedroom_sleeping"]
},
"data": {}
}
}
let sendDisplay = {
"payload": {
"domain": "switch",
"service": setDisplay,
"target": {
"entity_id": ["switch.master_bedroom_aircon_display"]
},
"data": {}
}
}
let notify = {
"topic": topic,
"nighttemp": nightTemp,
"acmode": acMode,
"fanmode": fanMode
}
let sendHvac = {
"payload": {
"domain": "climate",
"service": "set_hvac_mode",
"target": {
"entity_id": ["climate.master_bedroom_aircon"]
},
"data": {
"hvac_mode": setHvac
}
}
}
let sendTemp = {
"payload": {
"domain": "climate",
"service": "set_temperature",
"target": {
"entity_id": ["climate.master_bedroom_aircon"]
},
"data": {
"temperature": setTemp
}
}
}
let sendEco = {
"payload": {
"domain": "climate",
"service": "set_preset_mode",
"target": {
"entity_id": ["climate.master_bedroom_aircon"]
},
"data": {
"preset_mode": setEco
}
}
}
let sendAcFan = {
"payload": {
"domain": "climate",
"service": "set_fan_mode",
"target": {
"entity_id": ["climate.master_bedroom_aircon"]
},
"data": {
"fan_mode": "Auto"
}
}
}
let sendEchoDotDND = {
"payload": {
"domain": "switch",
"service": echoDotService,
"target": {
"entity_id": ["switch.basement_echo_dot_do_not_disturb_switch"]
},
"data": {}
}
}
node.log("Master Bedroom Climate: Message Payloads Defined")
// Log the parameters that were chosen, for debugging purposes
node.log("----- Master Bedroom Climate: Set Parameters -----")
node.log("setTemp: " + setTemp)
node.log("setEco: " + setEco)
node.log("setHvac: " + setHvac)
node.log("setFan: " + setFan)
node.log("setCool: " + setCool)
node.log("setSleep: " + setSleep)
node.log("setDisplay " + setDisplay)
node.log("time: " + time)
node.log("type: " + type)
node.log("topic: " + topic)
node.log("----- Master Bedroom Climate: End Parameters -----")
// If this was an automated trigger, set the cooling context for the bedroom accordingly.
if (type === 'auto' && time != 'bedtime') {
node.send([null, null, sendCool, null])
node.log("Master Bedroom Climate: Cooling Context Set")
}
// Automated responses
if (type === 'auto' && allowed === 'on' && meltdown === 'off' && vacation === 'off') {
node.log("Master Bedroom Climate: Auto")
if (sleeping === 'on' && topic != 'mrbedroom-wakeup') {
node.status({ fill: "red", shape: "ring", text: "Blocked (sleep mode)" })
node.log("Master Bedroom Climate: Blocked (sleep mode)")
} else {
if (topic === 'mrbedroom-cooling' && ac === 'on') {
node.status({ fill: "green", shape: "dot", text: "Cooling Schedule" })
node.send([[sendHvac, sendTemp, sendEco, sendAcFan], null, null])
node.log("Master Bedroom Climate: Auto/Cooling")
} else if (topic === 'mrbedroom-bedtime') {
node.status({ fill: "green", shape: "dot", text: "Bedtime" })
node.log("Master Bedroom Climate: Auto/Bedtime")
if (ac === 'on') {
node.send([[sendHvac, sendTemp, sendEco, sendAcFan], null, null, null])
node.log("Master Bedroom Climate: Auto/Bedtime/AC")
}
if (fanMode === 'fan') {
node.send([null, sendFan, null, null])
node.log("Master Bedroom Climate: Auto/Bedtime/Fan")
}
} else if (topic === 'mrbedroom-fan' && fanMode === 'Fan') {
node.status({ fill: "green", shape: "dot", text: "Fan Schedule" })
node.send([null, sendFan, null, null])
node.log("Master Bedroom Climate: Auto/Fan")
} else if (topic === 'mrbedroom-wakeup') {
node.status({ fill: "green", shape: "dot", text: "Wakeup Schedule" })
node.log("Master Bedroom Climate: Auto/Wakeup")
if (sleeping === 'off') {
node.send([null, sendFan, null, null])
node.log("Master Bedroom Climate: Auto/Wakeup/Sleep Off")
if (ac === 'on') {
node.send([[sendHvac, sendTemp, sendEco, sendAcFan], null, null, null])
node.log("Master Bedroom Climate: Auto/Wakeup/AC On")
}
} else if (sleeping === 'on') {
node.send([null, null, sendSleep, null])
node.log("Master Bedroom Climate: Auto/Wakeup/Sleep On")
}
}
}
// Manual Responses
} else if (type === 'manual') {
node.log("Master Bedroom Climate: Manual")
if (time === 'night') {
node.status({ fill: "blue", shape: "dot", text: "Manual Night" })
node.send([null, sendFan, null, null])
node.log("Master Bedroom Climate: Manual/Night")
if (ac === 'on') {
node.send([[sendHvac, sendTemp, sendEco, sendAcFan], null, null, null])
node.log("Master Bedroom Climate: Manual/Night/AC")
}
} else if (time === 'day') {
node.status({ fill: "blue", shape: "dot", text: "Manual Day" })
node.send([null, sendFan, null, null])
node.log("Master Bedroom Climate: Manual/Day")
if (ac === 'on') {
node.send([[sendHvac, sendTemp, sendEco, sendAcFan], null, null, null])
node.log("Master Bedroom Climate: Manual/Day/AC")
}
} else if (time === 'bedtime') {
node.status({ fill: "blue", shape: "dot", text: "Manual Bedtime" })
node.send([null, sendFan, null, null])
node.log("Master Bedroom Climate: Manual/Bedtime")
if (ac === 'on') {
node.send([[sendHvac, sendTemp, sendEco, sendAcFan], null, null, null])
node.log("Master Bedroom Climate: Manual/Bedtime/AC")
}
}
} else if (type === 'sleep') {
node.log("Master Bedroom Climate: Sleep")
if (time === 'night') {
node.status({ fill: "blue", shape: "dot", text: "Wakeup (Hot Day)" })
node.send([null, sendFan, sendEchoDotDND, null])
node.log("Master Bedroom Climate: Sleep/Night")
if (ac === 'on') {
node.send([[sendDisplay, sendHvac, sendTemp, sendEco, sendAcFan], null, null, null])
node.log("Master Bedroom Climate: Sleep/Night/AC")
}
} else if (time === 'day') {
node.status({ fill: "blue", shape: "dot", text: "Wakeup" })
node.send([null, sendFan, sendEchoDotDND, null])
node.log("Master Bedroom Climate: Sleep/Day")
if (ac === 'on') {
node.send([[sendDisplay, sendHvac, sendTemp, sendEco, sendAcFan], null, null, null])
node.log("Master Bedroom Climate: Sleep/Day/AC")
}
} else if (time === 'bedtime') {
node.status({ fill: "blue", shape: "dot", text: "Sleep" })
node.send([null, sendFan, sendEchoDotDND, null])
node.log("Master Bedroom Climate: Sleep/Bedtime")
if (ac === 'on') {
node.send([[sendDisplay, sendHvac, sendTemp, sendEco, sendAcFan], null, null, null])
node.log("Master Bedroom Climate: Sleep/Bedtime/AC")
}
}
} else if (meltdown === 'on') {
node.status({ fill: "red", shape: "ring", text: "Blocked (Meltdown Protocol)" })
node.log("Master Bedroom Climate: Blocked (Meltdown Protocol)")
} else if (vacation === 'on') {
node.status({ fill: "red", shape: "ring", text: "Blocked (Vacation Mode)" })
node.log("Master Bedroom Climate: Blocked (Vacation Mode)")
} else {
node.status({ fill: "red", shape: "ring", text: "Blocked (Automation Disabled)" })
node.log("Master Bedroom Climate: Blocked (Automation Disabled)")
}
node.log("Master Bedroom Climate: Processing Complete")