If it was on function

Hey I'm trying a flow for my Hallway with an Motion Sensor and Lumination Sensor.
It works pretty well with one tiny issue. If the light turns off and I join the hallway 1 sec later -nothing happens because my Lumination Sensor needs ~2 sec.

So I need a function or something like this with a "was the light on for the last 2 seconds?"
My skills are limited and I have no cloue how to handle this.

Thanks for any advise and help :slight_smile:

Since nobody else chimed in I'll take a swing at this. First, do you have any flow at all written ? Might give me a hint at what you are thinking. My next thought would be maybe put a trigger or delay node in to handle the 2 second wait. Try putting a flow together the best you can and post it then maybe we can find a solution that fits

1 Like

timestamp data. Compare now, being when you want to know, and the last storage of the timestamp when it was turned off. if your compare is greater than 2 seconds, then it's been on for more than that... Am I getting what you're asking?

You can also use a delay node to fire 2 seconds after your known input event, then check to see if it was on or off.

you can also use a latch bit type of node context variable. turns on or off with an event and will stay on until you switch it off. so you can compare if it was switched on with the event after your 2 second delay.

Perhaps just store that timestamp value of when it was turned off. I hope I get what you're asking.

1 Like

Hi There
You need a function node with something along the lines of;

// Assumes that there is an input from Motion Sensor & Lumination Sensor nodes
// with msg.topic = "Motion Sensor" and msg.topic == "Lumination Sensor"
// msg.payload for both is either "On" or "Off"

let currentTime = new Date().getTime();
// timeLightOff will always be 0 unless the light has been turned off
let timeLightOff = context.get("timeLightOff") || 0;

// Times are in mSec so adjust luminationSensorDelay to suite
const luminationSensorDelay = 2000;

Let currentState = msg.payload;

if (msg.topic == "Lumination Sensor") {
   if (currentState == "On") currentTime = 0;

   context.set("timeLightOff", currentTime);

} else if (msg.topic == "Motion Sensor" && currentTime != 0){
   if (currentTime - timeLightOff > luminationSensorDelay) {
      msg.topic = "Lumination Sensor";
      msg.payload = "On";

   }

}

You haven't shown your flow but this node will need to feed back into the Lumination Sensor node so that the light can be turned on and this node gets triggered again with a Lumination Sensor On msg. This is not a working function but it should give you some ideas. Basically you need to save the time the light went off so that if motion is detected in the Sensor restore time you turn the light back on

1 Like

Thanks for the replys.

Here my actual flow:

[{"id":"aeb907cd.6f0168","type":"switch","z":"f431c46a.84ab58","name":"motion on/off?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":360,"y":360,"wires":[["ecc8799d.9897a8","9e961e19.a9aa3"],["ecc8799d.9897a8"]]},{"id":"9e961e19.a9aa3","type":"api-current-state","z":"f431c46a.84ab58","name":"Dark?","server":"fefd8095.1f637","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"sensor.lichtsensor","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":690,"y":220,"wires":[["25c1c62c.b7cdca"]]},{"id":"25c1c62c.b7cdca","type":"switch","z":"f431c46a.84ab58","name":"","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"8","vt":"num"},{"t":"gt","v":"8","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":930,"y":220,"wires":[["e753feb8.21a25","236e49cf.56e9c6"],[]]},{"id":"e753feb8.21a25","type":"time-range-switch","z":"f431c46a.84ab58","name":"22:30 - Sunrise","lat":"52.207329","lon":"9.555720","startTime":"22:30","endTime":"09:30","startOffset":0,"endOffset":0,"x":1160,"y":200,"wires":[["ceebe219.248c1"],["aec6a70f.4e97d8"]]},{"id":"ceebe219.248c1","type":"api-call-service","z":"f431c46a.84ab58","name":"Dimm-lights 3%","server":"fefd8095.1f637","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.flur_unten_2","data":"{\"brightness\":3, \"transition\":1}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1380,"y":260,"wires":[["9adb6719.fe0d98"]]},{"id":"aec6a70f.4e97d8","type":"api-call-service","z":"f431c46a.84ab58","name":"Light","server":"fefd8095.1f637","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.flur_unten","data":"{\"brightness\":200, \"transition\":1}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1350,"y":340,"wires":[["9adb6719.fe0d98"]]},{"id":"b8d1bfb2.40c37","type":"server-state-changed","z":"f431c46a.84ab58","name":"Motion","server":"fefd8095.1f637","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.presence_manu","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":"","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":110,"y":360,"wires":[["aeb907cd.6f0168"]]},{"id":"9adb6719.fe0d98","type":"debug","z":"f431c46a.84ab58","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1640,"y":340,"wires":[]},{"id":"ecc8799d.9897a8","type":"stoptimer","z":"f431c46a.84ab58","duration":"30","units":"Second","payloadtype":"num","payloadval":"0","name":"","x":590,"y":400,"wires":[["a9b783bf.9f949"],[]]},{"id":"a9b783bf.9f949","type":"api-current-state","z":"f431c46a.84ab58","name":"motion?","server":"fefd8095.1f637","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"binary_sensor.presence_manu","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":800,"y":400,"wires":[[],["9c00bbed.f6bb58"]]},{"id":"c37e530c.3f992","type":"api-call-service","z":"f431c46a.84ab58","name":"Off","server":"fefd8095.1f637","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.flur_unten","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1250,"y":420,"wires":[[]]},{"id":"9c00bbed.f6bb58","type":"api-current-state","z":"f431c46a.84ab58","name":"Lights On?","server":"fefd8095.1f637","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.flur_unten","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1010,"y":400,"wires":[[],["c37e530c.3f992"]]},{"id":"3fb18031.ab363","type":"comment","z":"f431c46a.84ab58","name":"Flur Licht bewegung","info":"","x":150,"y":260,"wires":[]},{"id":"236e49cf.56e9c6","type":"time-range-switch","z":"f431c46a.84ab58","name":"20:30 - Sunrise","lat":"52.207329","lon":"9.555720","startTime":"20:30","endTime":"22:30","startOffset":0,"endOffset":0,"x":1160,"y":120,"wires":[["333159c3.9d2146"],[]]},{"id":"333159c3.9d2146","type":"api-call-service","z":"f431c46a.84ab58","name":"Dimm-lights 10%","server":"fefd8095.1f637","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.flur_unten","data":"{\"brightness\":10, \"transition\":1}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1390,"y":180,"wires":[[]]},{"id":"fefd8095.1f637","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

If the hall is dark and you enter it, it still takes ~2 seconds for the sensor to work...correct?
If you want to get rid of the delay, why not look for a faster sensor/?

I think its quit fast for a low energy Zigbee Sensor.

And this is only a test for my hole house... So I cant get cables to the sensors.

Ok, but shouldn’t the motion sensor control the light coming on? If it sees motion shouldn’t it turn the light on?
What is the lumination sensors job?
What are you doing in your flow?

You can see my flow a few post ago...

If the Motion Sensor is triggerd it goes to my Lumination Sensor - and ONLY if the light is <8 the lights goes On.

My Hallway is quite dark so thats why im using it...

push :slight_smile:

Due to the fact this uses Home Assistant (which I con't have), you might be better off asking on it's forum: https://community.home-assistant.io

1 Like