How to avoid fluctuating state changes?

Hi All,

I’ve automation setup in node red to trigger notification when the state of my binary sensor changes.
State -> OFF->ON - Triggers notification that alarm has been set
State ->ON->OFF - Triggers notification for disarm

Issue that i have is that the binary sensor fluctuates which triggers false notifications.

I,e. State changes from OFF->ON->OFF with 5 secs

Is there any possibility to avoid the fluctuating part as it happens within a short timeframe?

Thanks in advance

Trigger and block node.

You add a trigger and block node, this will let through one message until it gets reset.

The output of the trigger and block node splits to your normal sequence but also to a delay of how long you want.
After the delay you create a message (depends on how you set up the node) with what is needed to remove the block from the trigger and block node and put that to the input side of the trigger and block.

I’m typing from my phone and can’t give you a better description right now.

Here is an example:

(Trigger is the name of the node)

[{"id":"dba24fe2.ee247","type":"inject","z":"7129ee98.085df","name":"Manual reset trigger & block node","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":4390,"y":1360,"wires":[["6ad75f0d.aee37"]]},{"id":"6ad75f0d.aee37","type":"function","z":"7129ee98.085df","name":"Payload = reset","func":"msg.payload = \"reset\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":4660,"y":1440,"wires":[["9b444e54.c6c7e"]]},{"id":"9b444e54.c6c7e","type":"trigger","z":"7129ee98.085df","name":"","op1":"","op2":"0","op1type":"date","op2type":"str","duration":"0","extend":false,"units":"ms","reset":"reset","bytopic":"all","topic":"topic","outputs":1,"x":4560,"y":1580,"wires":[["3fe18f61.0531c","599cdeff.9f544"]]},{"id":"ac62c703.5731f8","type":"trigger-state","z":"7129ee98.085df","name":"Your trigger","server":"4bbca37b.1700ec","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"","entityidfiltertype":"exact","debugenabled":false,"constraints":[],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":4310,"y":1580,"wires":[["9b444e54.c6c7e"],[]]},{"id":"3fe18f61.0531c","type":"delay","z":"7129ee98.085df","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":4440,"y":1440,"wires":[["6ad75f0d.aee37"]]},{"id":"599cdeff.9f544","type":"api-call-service","z":"7129ee98.085df","name":"Rest of your sequence","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":4850,"y":1580,"wires":[[]]},{"id":"4bbca37b.1700ec","type":"server","z":"","name":"Home Assistant","addon":true}]


Thanks @Hellis81. I’ll give it a try

Hi @Hellis81. I tried based on your suggestion, but couldn’t make it work. Not sure if i had followed your instruction correctly. In my scenario, i want to trigger notification if the state changes either from OFF->ON or from ON-> OFF, but ignore if its changing from
OFF->ON->OFF in less than 5 secs (due to fluctuations). But the above method may only block the message till the set time period. Is there any way to implement below logic?
—block the first message and wait for set time period
----if the second message arrives within set time (5s), then ignore both messages
----if the second message doesn’t come with in the set time, then take the current state of the node (which can be either ON->OFF or OFF->ON scenario) and trigger notification.

Below is my current flow

So you have a state change node capturing all entities state changes and that is the trigger to your notification.
That looks bad…
I mean unless you only have one entity then that should trigger pretty much all the time.

I purpose the follwing.


In my previous picture I just had it on timestamp, because I don’t use that message, but I suppose you need to pass on the message you got.
But I would advice on a trigger state node instead since that will only capture changes on one specific entity.

The difference here is that we use the same nodes to do all the processing and then at the last step we split the data on “on” and “off”.

[{"id":"d06a390a.b924b8","type":"inject","z":"2227cef1.c55b22","name":"Manual reset trigger & block node","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":290,"y":520,"wires":[["80348ea6.8cb16"]]},{"id":"80348ea6.8cb16","type":"function","z":"2227cef1.c55b22","name":"Payload = reset","func":"msg.payload = \"reset\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":560,"y":600,"wires":[["d1005302.ec98f"]]},{"id":"d1005302.ec98f","type":"trigger","z":"2227cef1.c55b22","name":"","op1":"","op2":"0","op1type":"pay","op2type":"str","duration":"0","extend":false,"units":"ms","reset":"reset","bytopic":"all","topic":"topic","outputs":1,"x":460,"y":740,"wires":[["f8044561.ebdf08","1357fc50.c1c5c4"]]},{"id":"99f5c391.e8777","type":"trigger-state","z":"2227cef1.c55b22","name":"","server":"4bbca37b.1700ec","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"some_entity","entityidfiltertype":"exact","debugenabled":false,"constraints":[],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":210,"y":740,"wires":[["d1005302.ec98f"],["d1005302.ec98f"]]},{"id":"f8044561.ebdf08","type":"delay","z":"2227cef1.c55b22","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":340,"y":600,"wires":[["80348ea6.8cb16"]]},{"id":"617bb401.b288ac","type":"api-call-service","z":"2227cef1.c55b22","name":"Send notification, Armed","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":730,"y":680,"wires":[[]]},{"id":"1357fc50.c1c5c4","type":"switch","z":"2227cef1.c55b22","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":650,"y":740,"wires":[["617bb401.b288ac"],["2d4461d0.11cace"]]},{"id":"2d4461d0.11cace","type":"api-call-service","z":"2227cef1.c55b22","name":"Send notification, Disarmed","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":720,"y":800,"wires":[[]]},{"id":"4bbca37b.1700ec","type":"server","z":"","name":"Home Assistant","addon":true}]
1 Like

Thanks again. My bad as the State node is capturing only one entity and not all entities as the pic shows. Anyways, i have used trigger state node now as per your advise.
Here’s the flow


First trigger node is to capture when there is a state change from ON->OFF (as shown in the screenshot)
and the second node is for OFF->ON

Here’s my sensor state changes view. You can see that the small lines comes in regularly due to fluctuations in the sensor which i’m trying to avoid.

Screen Shot 2020-08-24 at 12.41.54 am

As per the flow above, I could see both trigger nodes triggered within 5 secs as the state changes from ON->OFF and then to ON in 2 secs. With this logic, second message is blocked as its within the delay time, but sends the first message after 5 secs which is triggering false notification. Wherein it should ignore both messages if the second request is received within 5 secs. Anyway to achieve this?
Thanks again for taking your time on this.

So how should the message look when it’s real?

If both the first and the second message if within 5 is “fake”, then what is a real message? Is that when there is one message with nothing coming after it?

I believe this can work.

Here we have a join node that joins all the messages coming in during a time of five seconds.
When the five seconds have passed it passes on an array of all the messages, and the function node counts the number of messages and if it’s one then it sends on the message.
If it’s more than one then it sends a “stop” which then is filtered to a dead end on the switch node.

[{"id":"30a4902.be6097","type":"join","z":"2227cef1.c55b22","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"5","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":450,"y":780,"wires":[["6f9fff9b.7d51a"]]},{"id":"6f9fff9b.7d51a","type":"function","z":"2227cef1.c55b22","name":"count messages","func":"if(msg.payload.length == 1){\n    msg.payload = msg.payload[0];\n}else{\n    msg.payload = \"stop\";\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":620,"y":780,"wires":[["1cd5d047.ef2ca"]]},{"id":"988ede5d.8df08","type":"inject","z":"2227cef1.c55b22","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":280,"y":780,"wires":[["30a4902.be6097"]]},{"id":"977acc63.ae149","type":"api-call-service","z":"2227cef1.c55b22","name":"Send notification, Armed","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1050,"y":760,"wires":[[]]},{"id":"1cd5d047.ef2ca","type":"switch","z":"2227cef1.c55b22","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"stop","vt":"str"},{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":790,"y":780,"wires":[[],["977acc63.ae149"],["a9f19845.1391c8"]]},{"id":"a9f19845.1391c8","type":"api-call-service","z":"2227cef1.c55b22","name":"Send notification, Disarmed","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1070,"y":804.2000122070312,"wires":[[]]},{"id":"4bbca37b.1700ec","type":"server","z":"","name":"Home Assistant","addon":true}]
2 Likes

Awesome mate. Perfect!!! This solution is working… :smile:

Thanks a ton for your help…

I’m not sure on how to mark your response as solution. Do you know on where will i find this option?

I don’t think that exists in this forum (node red).

Perhaps a topic for “What the heck”? :slight_smile:

Realy great!
I found a couple of solutions for this issue but this is easy to understand and working like a charme.

Br,
Tristan