Files
HA-NerdFlows-Functions/first-floor-lighting/downstairs-bathroom/downstairs-bathroom.js
Tony Stork e23c78c971 Improve motion lighting in some rooms
Motion lighting should no longer reset lights to adaptive when scene or brightness has been changed manually
2025-02-23 01:07:31 -05:00

28 lines
1.1 KiB
JavaScript

const states = global.get('homeassistant.homeAssistant.states')
const lights = states['light.downstairs_bathroom_lights'].state
const duration = states['input_number.downstairs_bathroom_lights_off_delay'].state
const lux = parseInt(states['sensor.downstairs_bathroom_illuminance'].state)
const threshold = parseInt(states['input_number.downstairs_bathroom_lux_threshold'].state)
const payload = msg.payload
const newDuration = duration * 60
if (payload === 'on') {
if (lux <= threshold && lights === 'off') {
node.status({fill:'green',shape:'dot',text:'Turning lights on'})
node.send([msg,null])
} else {
if (lights === 'on') {
node.status({fill:'red',shape:'ring',text:'Lights already on'})
} 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])
} else {
node.status({fill:"red",shape:"ring",text:"Lights already off"})
}
}