Get value from the previous second

Hello guys!
I have a sensor which consist in a PIR + Ambient light. I turn the lights only if the light is below some level. However what’s happening is that when I go through the sensor I make some shadow over it so the lux value goes down quite a bit and triggers the lights.

I was wondering if there’s a way to check, for example, 1 second before the PIR enables to decide if I should turn on the lights or not, instead of using the last value which is already “in the shadow”

I’ve played with the “Get history” node but I’m not able to get what I want. Any ideas to send me in the right direction?

Thanks!

Depending on how often your sensor sends out updates, you can probably use the previous_value sensor reading for the brightness once the PIR is triggered.
Another way, could be to read out the lightlevel and feed a helper sensor. Then you can just play with delays in updating the sensor value.

My sensor sends updates every 1 second. Sorry where is the “preview_value” sensor reading? is it a node or is it inside the get history node?

Once your sensor updates, its in the message that is sent out.
You can check out the complete message by connecting a debug node to the entity and setting it to complete msg instead of just payload.

umm but I’m using “Current state” node for the conditions, that one doesn’t seem to provide the previous state and if I use the “Events state” I can’t inject from the left :thinking:

I was able to read the prev state and do a function to read that path and pass it as payload value, but I’m still not sure how to connect the PIR with the prev_value reading. Here’s what I have so far

[{"id":"01a491104e1c7e13","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"e33e4890c15dd198","type":"server-state-changed","z":"01a491104e1c7e13","name":"Ambient light < 8?","server":"17f27332.cbbb3d","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.oficina_luz_ambiente","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"8","halt_if_type":"num","halt_if_compare":"lt","outputs":2,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":350,"y":240,"wires":[[],["7c1f637df74663de"]]},{"id":"7c1f637df74663de","type":"function","z":"01a491104e1c7e13","name":"get prev value","func":"msg.payload = msg.data.old_state.state\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":240,"wires":[["4c15bea481ef953d"]]},{"id":"53456e4708a80bfa","type":"debug","z":"01a491104e1c7e13","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1010,"y":300,"wires":[]},{"id":"4c15bea481ef953d","type":"api-current-state","z":"01a491104e1c7e13","name":"Ambient light < 8","server":"17f27332.cbbb3d","version":3,"outputs":2,"halt_if":"8","halt_if_type":"num","halt_if_compare":"lt","entity_id":"sensor.oficina_luz_ambiente","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":790,"y":240,"wires":[["53456e4708a80bfa"],[]]},{"id":"876b95df52a962a7","type":"server-state-changed","z":"01a491104e1c7e13","name":"PIR","server":"17f27332.cbbb3d","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.oficina_movimiento","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":310,"y":180,"wires":[[],[]]},{"id":"17f27332.cbbb3d","type":"server","name":"Home Assistant","version":4,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m"}]

Yes, that is true. Current state does not work.
What are you using to trigger your automation? I would assume its the PIR.

Does the Sensor give you both values in one message?
If thats the case, simply add a switch node after the trigger that checks if the old_state was below your desired threshold.

I actually have 2 triggers! One as you said it’s the PIR, the other is that when the ambient light goes below 8 and there’s someone in the room I also turn on the lights

Store the data in node red.
https://nodered.org/docs/user-guide/context

Yes either do it like WallyR recommended and save the values in NodeRed or maybe try it again with the get history node.

Here is an example get_history node that checks one entity up to 10s back:

[{"id":"f51f96a3a1de42fd","type":"api-get-history","z":"1486cf5f.cd1781","name":"","server":"","version":0,"startdate":"","enddate":"","entityid":"light.ankleide","entityidtype":"is","useRelativeTime":true,"relativeTime":"10s","flatten":true,"output_type":"array","output_location_type":"msg","output_location":"payload","x":310,"y":2760,"wires":[["7ae0f30daa575442"]]},{"id":"7ae0f30daa575442","type":"debug","z":"1486cf5f.cd1781","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":520,"y":2740,"wires":[]},{"id":"4ea3bf77f663b916","type":"inject","z":"1486cf5f.cd1781","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":2780,"wires":[["f51f96a3a1de42fd"]]}]

It will output all the state changes from the last 10s as an array.

EDIT Corrected the Format of the Code.

I will take a look! If I do it like @WallyR mentions then I need to figure out how this works and how to get the value back into my condition nodes to use it.

Thanks all!

Tried to import your code but I’m getting this

That’s because the code should have been posted in a code block, instead of a quote block. The quotes have been converted to fancy quotes instead of " and so now it is not valid JSON. You need to do copy the text in to something like notepad, and do a find and a replace on all the fancy quotes (like the one shown in the error) and replace them with normal quotes - like the one shown on your keyboard.

Here is the code again, with all 154 quotes replace with the correct type.

[{"id":"f51f96a3a1de42fd","type":"api-get-history","z":"1486cf5f.cd1781","name":"","server":"","version":0,"startdate":"","enddate":"","entityid":"light.ankleide","entityidtype":"is","useRelativeTime":true,"relativeTime":"10s","flatten":true,"output_type":"array","output_location_type":"msg","output_location":"payload","x":310,"y":2760,"wires":[["7ae0f30daa575442"]]},{"id":"7ae0f30daa575442","type":"debug","z":"1486cf5f.cd1781","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":520,"y":2740,"wires":[]},{"id":"4ea3bf77f663b916","type":"inject","z":"1486cf5f.cd1781","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":2780,"wires":[["f51f96a3a1de42fd"]]}]

Alright I think I’ve made it, but it’s a bit complicated. Probably would be better to code the switch node into the function but I’m not a dev so for now this is how it looks

[{"id":"98d239e28e8bc19a","type":"api-get-history","z":"873b9e55.85b7b","name":"","server":"17f27332.cbbb3d","version":0,"startdate":"","enddate":"","entityid":"sensor.oficina_luz_ambiente","entityidtype":"is","useRelativeTime":true,"relativeTime":"10s","flatten":true,"output_type":"array","output_location_type":"msg","output_location":"payload","x":410,"y":960,"wires":[["8cc0dd63a6b082ee"]]},{"id":"7b4d66bbdce5ac65","type":"debug","z":"873b9e55.85b7b","name":"debug 11","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":760,"y":980,"wires":[]},{"id":"31967bd4d9cb30b5","type":"inject","z":"873b9e55.85b7b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":980,"wires":[["98d239e28e8bc19a"]]},{"id":"86f751386fdaf1cf","type":"switch","z":"873b9e55.85b7b","name":"Is < 8?","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"8","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":610,"y":1120,"wires":[["7b4d66bbdce5ac65"],["9695670f97b04279"]]},{"id":"9695670f97b04279","type":"debug","z":"873b9e55.85b7b","name":"debug 12","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":780,"y":1160,"wires":[]},{"id":"8cc0dd63a6b082ee","type":"function","z":"873b9e55.85b7b","name":"get prev value","func":"msg.payload = msg.payload[0].state\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":1060,"wires":[["86f751386fdaf1cf"]]},{"id":"17f27332.cbbb3d","type":"server","name":"Home Assistant","version":4,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m"}]
  1. It gets the 10 seconds
  2. I read position 0 of the array so it’s prev 9 or 10 seconds with the function node
  3. With the switch node I divide the output so I can do a condition to continue with the flow

Try to count events which exceed your light level. For example if you are sampling every minute, count to 10. If 10 in a row reached the threshold, it means that the requested level has been reached so you can turn lights on

Another way is to collect all events and calculate average value during defined time window.

I would also look for ready to use nodes which are specialized in what you need. Those are often very natured, based on complex algorithms, with polished corner-cases you have no even think they exist. Cannot advice any but NodeRed repo has thousands of nodes

Idk sounds like you need an event state and set the wait period to a few seconds or a wait until node and again set the wait period to a few seconds.