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