Files
HA-NerdFlows-Functions/second-floor-lighting/upstairs-bathroom/motion-detected.js

42 lines
1.7 KiB
JavaScript

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 = parseInt(states['sensor.upstairs_bathroom_illuminance'].state)
const threshold = parseInt(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 -----")