const states = global.get('homeassistant.homeAssistant.states') const ac = global.get('masterBedroom.aircon.installed', "diskCon") const temp = global.get('outdoorTemp.tempStr') const threshold = states['input_number.master_bedroom_aircon_run_threshold'].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 currentSetTemp = states['climate.master_bedroom_aircon'].attributes.temperature const coolingTime = states['input_boolean.master_bedroom_cooling_on'].state const sleeping = states['input_boolean.master_bedroom_sleeping'].state const hotDay = states['input_boolean.hot_day'].state const topic = msg.topic const masterBedroomAircon = ["climate.master_bedroom_aircon"] let setTemp = {} let setEco = {} let duration = {} let end = {} let setHvac = {} if (topic === 'toggle') { if (hotDay === 'on' || sleeping === 'on') { setTemp = bedTemp setEco = 'turn_off' } else { setTemp = nightTemp setEco = 'turn_on' } } else if (topic === 'timer') { if (currentSetTemp == bedTemp) { setTemp = nightTemp if (hotDay === 'on') { setEco = 'turn_off' } else { setEco = 'turn_on' } } else { setTemp = dayTemp setEco = 'turn_on' } } if (setTemp == bedTemp) { duration = 3600 } else if (setTemp == nightTemp && hotDay === 'off') { duration = 3600 } else { duration = 1800 } if (setTemp == dayTemp) { end = true if (hotDay === 'off') { setHvac = 'off' } else { setHvac = 'cool' } } else if (setTemp != bedTemp && coolingTime === 'on') { end = true setHvac = 'cool' } else { end = false setHvac = 'cool' } let timerStart = { "duration": duration } let sendHvac = { "payload": { "action": "climate.set_hvac_mode", "target": { "entity_id": masterBedroomAircon }, "data": { "hvac_mode": setHvac } } } let sendTemp = { "payload": { "action": "climate.set_temperature", "target": { "entity_id": masterBedroomAircon }, "data": { "temperature": setTemp } } } let sendEco = { "payload": { "action": `switch.${setEco}`, "target": { "entity_id": ["switch.master_bedroom_aircon_eco_mode"] }, "data": {} } } let sendAcFan = { "payload": { "action": "climate.set_fan_mode", "target": { "entity_id": masterBedroomAircon }, "data": { "fan_mode": "auto" } } } if (ac === 'on' && temp >= threshold) { if (sleeping === 'on' || coolingTime === 'on' || end === true) { node.status({fill:"green",shape:"dot",text:"Finished"}) return [[sendHvac, sendTemp, sendAcFan, sendEco], null] } else { node.status({fill:"blue",shape:"dot",text:"Next Cycle"}) return [[sendHvac, sendTemp, sendAcFan, sendEco], timerStart] } } else if (ac === 'off') { node.status({fill:"red",shape:"ring",text:"A/C Not Installed"}) return null } else { node.status({fill:"red",shape:"ring",text:"Too Cold"}) return null }