394 lines
9.8 KiB
JavaScript
394 lines
9.8 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 weather = states['weather.iron_nerd_weather_station'].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 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 || payload === 'weather') {
|
|
time = "night"
|
|
eventCall = "on"
|
|
} else if (payload == 1) {
|
|
time = "day"
|
|
eventCall = "off"
|
|
}
|
|
|
|
if (msg.topic === 'timer-finished' || msg.topic === 'manual-trigger' || msg.topic === 'weather') {
|
|
topic = msg.topic
|
|
}
|
|
|
|
if (topic === 'timer-finished' || topic === 'manual-trigger') {
|
|
delay = 'off'
|
|
} else if (topic === 'weather') {
|
|
delay = 'off'
|
|
} else if (weather === 'sunny' || weather === 'clear-night') {
|
|
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 = ["light.hallway_overhead"]
|
|
|
|
if (holidayHold === 'off') {
|
|
lightsOff.push("light.front_porch_light")
|
|
}
|
|
|
|
if (upBathOcc === 'off') {
|
|
lightsOff.push("light.upstairs_bathroom_lights")
|
|
}
|
|
|
|
// Stairwell light strip settings
|
|
let stairKelvin = 2000
|
|
let stairBrt = {}
|
|
let stairEntity = ["light.stairwell_led_strip"]
|
|
if (time === 'day') {
|
|
stairBrt = 255
|
|
} else if (time === 'night') {
|
|
stairBrt = 150
|
|
}
|
|
|
|
// 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 === 'weather') {
|
|
notifyMsg = "Lights on, delay cancelled due to weather change"
|
|
sunsetStatus = "Weather Change"
|
|
} else if (topic === 'timer-finished') {
|
|
notifyMsg = "Lights on because timer was finished manually"
|
|
sunsetStatus = "Manual Trigger"
|
|
} else if (delay === 'on') {
|
|
notifyMsg = "Lights on after delay due to clear weather"
|
|
sunsetStatus = "Delayed"
|
|
} else if (delay === 'off') {
|
|
notifyMsg = "Lights on early due to cloudy weather"
|
|
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 sendStairwellScript = {
|
|
"payload": {
|
|
"domain": "script",
|
|
"service": "stairwell_led_strip",
|
|
"data": {
|
|
"color_temp_kelvin": stairKelvin,
|
|
"brightness": stairBrt
|
|
}
|
|
}
|
|
}
|
|
|
|
let sendStairwellOff = {
|
|
"payload": {
|
|
"domain": "light",
|
|
"service": "turn_off",
|
|
"target": {
|
|
"entity_id": stairEntity
|
|
},
|
|
"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("stairKelvin: " + stairKelvin)
|
|
node.log("stairBrt: " + stairBrt)
|
|
node.log("holidayMode: " + holidayMode)
|
|
node.log("eventCall: " + eventCall)
|
|
if (time === 'night') {
|
|
node.log("weather: " + weather)
|
|
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],sendStairwellScript,sendExtraLights])
|
|
setTimeout(() => {
|
|
node.send([null,[sendStairwellOff,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 === 'weather') {
|
|
node.send([null,sendTimerCancel,null,null])
|
|
}
|
|
node.send([null,null,[sendFirstFloorScene,sendSecondFloorScene],null])
|
|
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") |