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 = 'Alexa Everywhere' // Default to playing on all Echo Dots unless told otherwise let tinaAvailable = false let tonyAvailable = false let enable = false let reason = {} // Determine who is available to respond if (tonyAwake === 'on' && tonyLocation === 'home') { tonyAvailable = true } if (xiaDesktopUser === 'irish' && xiaDesktopIdle === 'off' && tinaLocation === 'home') { tinaAvailable = true } // If both of us are still asleep, or I am asleep and she is at work, play the alert only in the master bedroom if (tinaAvailable === false && tonyAvailable === false) { who = 'Master Bedroom' } // Determine if the alert should be sent if (emmaSleep === 'on') { if (overnight === 'on' && masterBedroomSleeping === 'on') { enable = true reason = 'Overnight is on, and master bedroom is sleeping' } else if (overnight === 'off') { enable = true reason = 'Overnight is off' } } // 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" } // Add some logs for debugging node.log(`--------------- Emma Door Alert --------------`) node.log(`Emma Door Alert - topic: ${topic}`) node.log(`Emma Door Alert - payload: ${payload}`) node.log(`Emma Door Alert - masterBedroomSleeping: ${masterBedroomSleeping}`) node.log(`Emma Door Alert - emmaSleep: ${emmaSleep}`) node.log(`Emma Door Alert - tonyAwake: ${tonyAwake}`) node.log(`Emma Door Alert - xiaDesktopUser: ${xiaDesktopUser}`) node.log(`Emma Door Alert - xiaDesktopIdle: ${xiaDesktopIdle}`) node.log(`Emma Door Alert - tonyLocation: ${tonyLocation}`) node.log(`Emma Door Alert - tinaLocation: ${tinaLocation}`) node.log(`Emma Door Alert - overnight: ${overnight}`) node.log(`Emma Door Alert - tonyAvailable: ${tonyAvailable}`) node.log(`Emma Door Alert - tinaAvailable: ${tinaAvailable}`) node.log(`Emma Door Alert - who: ${who}`) node.log(`Emma Door Alert - enable: ${enable}`) node.log(`Emma Door Alert - reason: ${reason}`) node.log(`----------------------------------------------`) // 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 === 'cancel') { 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'}) }