From b99b13df5168c23f14cf2f398dbe66596e5e1127 Mon Sep 17 00:00:00 2001 From: Tony Stork Date: Sat, 13 Jan 2024 19:56:21 -0500 Subject: [PATCH] Add motion-detected and shower mode functions for upstairs bathroom --- .../upstairs-bathroom/motion-detected.js | 42 ++++++++++++ .../upstairs-bathroom/shower-mode.js | 67 +++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 second-floor-lighting/upstairs-bathroom/motion-detected.js create mode 100644 second-floor-lighting/upstairs-bathroom/shower-mode.js diff --git a/second-floor-lighting/upstairs-bathroom/motion-detected.js b/second-floor-lighting/upstairs-bathroom/motion-detected.js new file mode 100644 index 0000000..a7599df --- /dev/null +++ b/second-floor-lighting/upstairs-bathroom/motion-detected.js @@ -0,0 +1,42 @@ +const states = global.get('homeassistant.homeAssistant.states') +const lights = states['light.upstairs_bathroom_lights'].state +const duration = states['input_number.upstairs_bathroom_motion_off_delay'].state +const lux = states['sensor.upstairs_bathroom_illuminance'].state +const threshold = states['input_number.upstairs_bathroom_lux_threshold'].state +const showerMode = states['input_boolean.shower_mode'].state +const payload = msg.payload +const newDuration = duration * 60 + +let timerCancel = { + "payload": "stop" +} + +if (showerMode === 'off') { + if (payload === 'on') { + node.send([null,null,timerCancel]) + if (lux <= threshold || lights === 'on') { + node.status({fill:'green',shape:'dot',text:'Lights On'}) + node.send([msg,null,null]) + } else { + node.status({fill:'red',shape:'ring',text:'Too bright'}) + } + } else if (payload === 'off') { + if (lights === 'on') { + msg.duration = newDuration + node.status({fill:"green",shape:"dot",text:parseInt(duration) + ' minutes'}) + node.send([null,msg,null]) + } else { + node.status({fill:"red",shape:"ring",text:"Lights already off"}) + } + } +} + +node.log("----- Upstairs Bathroom Motion Parameters Start -----") +node.log("Upstairs Bathroom payload: " + payload) +node.log("Upstairs Bathroom lights: " + lights) +node.log("Upstairs Bathroom duration: " + duration) +node.log("Upstairs Bathroom newDuration: " + newDuration) +node.log("Upstairs Bathroom lux: " + lux) +node.log("Upstairs Bathroom threshold: " + threshold) +node.log("Upstairs Bathroom showerMode: " + showerMode) +node.log("----- Upstairs Bathroom Motion Parameters End -----") \ No newline at end of file diff --git a/second-floor-lighting/upstairs-bathroom/shower-mode.js b/second-floor-lighting/upstairs-bathroom/shower-mode.js new file mode 100644 index 0000000..6a8fe9b --- /dev/null +++ b/second-floor-lighting/upstairs-bathroom/shower-mode.js @@ -0,0 +1,67 @@ +const states = global.get('homeassistant.homeAssistant.states') +const occupied = states['binary_sensor.upstairs_bathroom_occupied'].state +const earlyNightMode = states['binary_sensor.early_night_mode'].state +const nightMode = states['input_boolean.night_mode'].state +const kallenOvernight = states['input_boolean.kallen_overnight'].state +const motion = states['binary_sensor.upstairs_bathroom_motion'].state +const payload = msg.payload + +let timerCancel = { + "payload": "stop" +} + +let scene = {} +if (payload === 'on') { + scene = 'Bright' +} else { + if (motion === 'on') { + scene = 'Adaptive' + } else { + if (kallenOvernight === 'off') { + if (nightMode === 'on') { + scene = 'Single Nightlight' + } else { + scene = 'Nightlight' + } + } else { + scene = 'Adaptive' + } + } +} + +if (payload === 'on') { + msg.scene = scene + node.status({fill:'green',shape:'dot',text:'Shower Mode On'}) + node.send([timerCancel,msg,null]) +} else if (payload === 'off') { + node.send([timerCancel,null,null]) + if (motion === 'on') { + msg.scene = scene + node.status({fill:'green',shape:'dot',text:'Lights Adaptive'}) + node.send([null,msg,null]) + } else if (earlyNightMode === 'on') { + if (kallenOvernight === 'on') { + node.status({fill:'green',shape:'dot',text:'Lights Off'}) + node.send([null,null,msg]) + } else { + msg.scene = scene + node.status({fill:'green',shape:'dot',text:'Scene Set'}) + node.send([null,msg,null]) + } + } else { + node.status({fill:'green',shape:'dot',text:'Lights Off'}) + node.send([null,null,msg]) + } +} + +node.log("----- Upstairs Bathroom Shower Mode Parameters Start -----") +node.log("Upstairs Bathroom payload: " + payload) +node.log("Upstairs Bathroom scene: " + scene) +if (payload === 'off') { + node.log("Upstairs Bathroom occupied: " + occupied) + node.log("Upstairs Bathroom earlyNightMode: " + earlyNightMode) + node.log("Upstairs Bathroom nightMode: " + nightMode) + node.log("Upstairs Bathroom kallenOvernight: " + kallenOvernight) + node.log("Upstairs Bathroom motion: " + motion) +} +node.log("----- Upstairs Bathroom Shower Mode Parameters End -----") \ No newline at end of file