diff --git a/second-floor-lighting/emma-bedroom/emma-bedroom.js b/second-floor-lighting/emma-bedroom/emma-bedroom.js new file mode 100644 index 0000000..c20eadb --- /dev/null +++ b/second-floor-lighting/emma-bedroom/emma-bedroom.js @@ -0,0 +1,29 @@ +const states = global.get('homeassistant.homeAssistant.states') +const lights = states['light.emma_bedroom_light'].state +const duration = states['input_number.emma_bedroom_lights_off_delay'].state +const lux = states['sensor.emma_bedroom_illuminance'].state +const threshold = states['input_number.emma_bedroom_lux_threshold'].state +const sleeping = states['input_boolean.emma_sleeping'].state +const payload = msg.payload +const newDuration = duration * 60 + +if (sleeping === 'off') { + if (payload === 'on') { + if (lux <= threshold || lights === 'on') { + node.status({fill:'green',shape:'dot',text:'Lights On'}) + node.send([msg,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]) + } else { + node.status({fill:"red",shape:"ring",text:"Lights already off"}) + } + } +} else { + node.status({fill:'red',shape:'ring',text:'Blocked (Sleeping)'}) +} \ No newline at end of file