Files
HA-NerdFlows-Functions/time-based/processing.js

364 lines
9.5 KiB
JavaScript

//! FLOW VARIABLES DEFINED IN START TAB
// Set Constants
const states = global.get('homeassistant.homeAssistant.states')
const holidayMode = states['input_boolean.holiday_mode'].state
const holidayHold = states['input_boolean.holiday_mode_hold'].state
const currentHoliday = states['input_select.holiday_animation'].state
const holiday = currentHoliday.toLowerCase()
const sunsetLights = states['input_boolean.sunset_lights_on'].state
const vacation = states['input_boolean.vacation_mode'].state
const upBathOcc = states['binary_sensor.upstairs_bathroom_occupied'].state
const deskLights = states['light.tina_desk_lights'].state
const luxLivingRoom = states['sensor.living_room_front_illuminance'].state
const luxThresholdLivingRoom = states['input_number.living_room_lux_threshold'].state
const luxStairwell = states['sensor.stairwell_bottom_illuminance'].state
const luxThresholdStairwell = states['input_number.stairwell_lux_threshold'].state
const adaptiveSleep = flow.get("adaptiveSleep", "diskCon")
const selScenesMain = flow.get("selScenesMain", "diskCon")
const selScenesTinaDesk = flow.get("selScenesTinaDesk", "diskCon")
const booleanOff = flow.get("booleanOff", "diskCon")
const payload = msg.payload
node.log("Time-based Automations: Constants Set")
// Set a few important variables
let time = {}
let topic = {}
let delay = {}
let eventCall = {}
if (payload == 0) {
time = "night"
eventCall = "on"
} else if (payload == 1) {
time = "day"
eventCall = "off"
}
if (msg.topic === 'timer-finished' || msg.topic === 'manual-trigger' || msg.topic === 'lux') {
topic = msg.topic
}
if (topic === 'timer-finished' || topic === 'manual-trigger' || topic === 'lux') {
delay = 'off'
} else if (luxLivingRoom > luxThresholdLivingRoom && luxStairwell > luxThresholdStairwell) {
delay = 'on'
} else {
delay = 'off'
}
node.log("Time-based Automations: Main variables defined")
// ---------- Configuration ----------
// Decide which nighttime lighting to turn off at sunrise
let lightsOff = []
if (holidayHold === 'off') {
lightsOff.push("light.front_porch_light")
}
if (upBathOcc === 'off') {
lightsOff.push("light.upstairs_bathroom_lights")
}
// Sunset lights timer settings
let timerEntity = ["timer.sunset_lighting_timer"]
let timerDuration = "00:30:00"
// Adaptive lighting switches to set
let switchAdaptive = []
let setAdaptive = {}
if (time === 'day') {
if (holidayHold === 'on'){
switchAdaptive = flow.get("adaptiveDayHoliday", "diskCon")
} else {
switchAdaptive = flow.get("adaptiveDay", "diskCon")
}
setAdaptive = 'on'
} else if (time === 'night') {
switchAdaptive = flow.get("adaptiveNight", "diskCon")
if (holidayMode === 'on') {
setAdaptive = 'off'
} else {
setAdaptive = 'on'
}
}
// Notification settings
let notifyTitle = {}
let notifyMsg = {}
let sunsetStatus = {}
if (sunsetLights === 'off') {
notifyTitle = "Sunset Lights SKIPPED"
notifyMsg = "Lights not on due to nobody home, or toggle shutoff"
sunsetStatus = "Skipped"
} else {
notifyTitle = "Sunset Lights On"
if (topic === 'timer-finished') {
notifyMsg = "Lights on because timer was finished manually"
sunsetStatus = "Manual Trigger"
} else if (topic === 'lux') {
notifyMsg = "Lights on because light level dropped below threshold"
sunsetStatus = "Light Level Trigger"
} else if (delay === 'on') {
notifyMsg = "Lights on after delay due to sufficient light"
sunsetStatus = "Delayed"
} else if (delay === 'off') {
notifyMsg = "Lights on early due to low light"
sunsetStatus = "Early"
}
}
// Holiday Settings
let setHoliday = {}
let switchHoliday = ["switch.animated_scene_" + holiday]
if (holidayMode === 'on') {
if (time === 'day') {
setHoliday = 'off'
} else if (time === 'night') {
setHoliday = 'on'
}
}
node.log("Time-based Automations: Decision Logic Complete")
// ---------- Service Calls ----------
let sendLights = {
"payload": {
"domain": "light",
"service": "turn_off",
"target": {
"entity_id": lightsOff
},
"data": {}
}
}
let sendSleepOff = {
"payload": {
"domain": "switch",
"service": "turn_off",
"target": {
"entity_id": adaptiveSleep
},
"data": {}
}
}
let sendBooleanOff = {
"payload": {
"domain": "input_boolean",
"service": "turn_off",
"target": {
"entity_id": booleanOff
},
"data": {}
}
}
let sendAdaptive = {
"payload": {
"domain": "switch",
"service": "turn_" + setAdaptive,
"target": {
"entity_id": switchAdaptive
},
"data": {}
}
}
let sendSceneResetMain = {
"payload": {
"domain": "input_text",
"service": "set_value",
"target": {
"entity_id": selScenesMain
},
"data": {
"value": "Adaptive"
}
}
}
let sendSceneResetDesk = {
"payload": {
"domain": "input_text",
"service": "set_value",
"target": {
"entity_id": selScenesTinaDesk
},
"data": {
"value": "Day Mode"
}
}
}
let sendHoliday = {
"payload": {
"domain": "switch",
"service": "turn_" + setHoliday,
"target": {
"entity_id": switchHoliday
},
"data": {}
}
}
let sendTimer = {
"payload": {
"domain": "timer",
"service": "start",
"target": {
"entity_id": timerEntity
},
"data": {
"duration": timerDuration
}
}
}
let sendTimerCancel = {
"payload": {
"domain": "timer",
"service": "cancel",
"target": {
"entity_id": timerEntity
},
"data": {}
}
}
let sendNotifyPhone = {
"payload": {
"domain": "script",
"service": "text_notify",
"data": {
"who": "all",
"title": notifyTitle,
"message": notifyMsg,
"type": "normal",
"tag": "sunset-lights"
}
}
}
let sendNotifyTV = {
"payload": {
"domain": "script",
"service": "tv_notify",
"data": {
"who": "all",
"title": notifyTitle,
"message": notifyMsg,
"data": {
"fontsize": "large",
"duration": 3,
"transparency": "25%"
}
}
}
}
let sendFirstFloorScene = {
"payload": {
"domain": "script",
"service": "evening_on_first_floor",
"data": {
"sunset_lights": 1
}
}
}
let sendSecondFloorScene = {
"payload": {
"domain": "script",
"service": "evening_on_second_floor",
"data": {
"sunset_lights": 1
}
}
}
let sendDeskScene = {
"payload": {
"domain": "input_select",
"service": "select_option",
"target": {
"entity_id": ["input_select.tina_desk_scenes"]
},
"data": {
"option": "Reset"
}
}
}
let sendExtraLights = {
"payload": {
"event": "extraLights",
"data": {
"toggle": eventCall
}
}
}
node.log("Time-based Automations: Message Payloads Defined")
node.log("----- Time-based Automations: Set Parameters")
node.log("time: " + time)
node.log("topic: " + topic)
node.log("lightsOff: " + lightsOff)
node.log("holidayMode: " + holidayMode)
node.log("eventCall: " + eventCall)
if (time === 'night') {
node.log("luxLivingRoom: " + luxLivingRoom)
node.log("luxThresholdLivingRoom: " + luxThresholdLivingRoom)
node.log("luxStairwell: " + luxStairwell)
node.log("luxThresholdStairwell: " + luxThresholdStairwell)
node.log("delay: " + delay)
node.log("sunsetStatus: " + sunsetStatus)
}
if (holidayMode === 'on') {
node.log("switchHoliday: " + switchHoliday)
node.log("holiday: " + holiday)
node.log("holidayHold: " + holidayHold)
node.log("switchHoliday: " + switchHoliday)
}
node.log("----- Time-based Automations: End Parameters -----")
if (vacation === 'off') {
if (time === "day") {
node.status({fill:"green",shape:"dot",text:"Sunrise Flow"})
node.send([null,[sendLights,sendBooleanOff],null,sendExtraLights])
setTimeout(() => {
node.send([null,[sendSleepOff,sendAdaptive,sendSceneResetMain,sendSceneResetDesk],null,null])
if (holidayMode === 'on' && holidayHold === 'off') {
node.send([null,[sendHoliday],null,null])
}
}, 5000)
} else if (time === "night") {
node.status({fill:"green",shape:"dot",text:"Sunset Flow"})
node.send([null,sendAdaptive,null,null])
if (holidayMode === 'on') {
setTimeout(() => {
node.send([null,sendHoliday,null,null])
}, 1000)
}
if (delay === 'on') {
node.send([null,sendTimer,null,null])
} else {
if (topic === 'lux') {
node.send([null,sendTimerCancel,null,null])
}
node.send([null,null,[sendFirstFloorScene,sendSecondFloorScene],sendExtraLights])
if (deskLights === 'on') {
node.send([null,sendDeskScene,null,null])
}
if (topic != 'manual-trigger') {
node.send([[sendNotifyPhone,sendNotifyTV],null,null,null])
}
}
}
}
node.log("Time-based Automations: Processing Complete")