Files
HA-NerdFlows-Functions/second-floor/emma-bedroom/door-alert.js

69 lines
2.3 KiB
JavaScript

const states = global.get('homeassistant.homeAssistant.states')
const masterBedroomSleeping = states['input_boolean.master_bedroom_sleeping'].state
const emmaSleep = states['input_boolean.emma_sleeping'].state
const tonyAwake = states['input_boolean.tony_awake'].state
const xiaDesktopUser = states['sensor.xia_desktop_current_username'].state
const xiaDesktopIdle = states['binary_sensor.tina_desktop_idle'].state
const tonyLocation = states['person.tony_stork'].state
const tinaLocation = states['person.christina_stork'].state
const overnight = states['binary_sensor.overnight'].state
const payload = msg.payload
const topic = msg.topic
// Set some default values
let who = 'Master Bedroom'
let tinaAvailable = false
let tonyAvailable = false
let enable = false
// Determine who is available to respond
if (tonyAwake === 'on' && tonyLocation === 'home') {
tonyAvailable = true
}
if (xiaDesktopUser === 'irish' && xiaDesktopIdle === 'off' && tinaLocation === 'home') {
tinaAvailable = true
}
// Determine where to send the alert to
if (tinaAvailable === true || tonyAvailable === true) {
who = 'Common Areas'
}
// Determine if the alert should be sent
if (overnight === 'on' && masterBedroomSleeping === 'on') {
enable = true
} else if (overnight === 'off') {
enable = true
}
// Define the alert message
let alertTTS = {
"topic": who,
"payload": "Emma has opened her door, please check on her. I repeat, Emma has opened her door, please check on her. This is urgent, Emma is awake. PDS toddler warning, a large and extremely dangerous toddler is on the ground in your area.",
"type": "critical"
}
// Send the alert if conditions are met
if (topic === 'door' && payload === 'on') {
if (enable === true) {
node.status({fill:'green',shape:'dot',text:`Sending alert to ${who}`})
node.send([alertTTS,msg,null])
} else {
node.status({fill:'red',shape:'ring',text:'Door Opened, but no alert sent'})
}
} else if (topic === 'timer') {
node.status({fill:'green',shape:'dot',text:'Timer Finished'})
node.send([alertTTS,msg,null])
} else if (topic === 'button') {
node.status({fill:'green',shape:'dot',text:'Cancel Button Pressed'})
node.send([null,null,msg])
} else if (topic === 'door' && payload === 'off') {
node.status({fill:'green',shape:'dot',text:'Door Closed'})
}