Turning off a rogue Sonoff Light switch

I run Node-RED on my Home Assistant.

I have a Sonoff 4 CH Pro relay installed in my bedroom. It controls 4 switches, one of which is the bedroom light.

It has recently gone, well, "rogue". It switches on and off completely randomly.

It means that the light turns on multiple times during the night and that really disturbs our sleep.

I had designed a simple flow to turn off the light every time it is switched on, between 00:00 till 07:00 hours. However, the problem is, whenever we genuinely need to switch on the light during that time at night, we cannot.

I was thinking if a flow can be designed so that if the light is turned on say, twice, within a short period of time, the flow would be bypassed.

Any ideas on how to execute it? Or may be any other ideas to smartly solve my problem?

I am a noob, I confess.

Is the sonoff flashed?
What does you flow look like?
Is there a timer? or somthing set causing the random on's.

If the device is faulty why waste energy stop using that channel and replace with a sonoff mini or similar. They are so cheap.

Do you using MQTT messages on your flow?
Perhaps you have retained messages?

My current flow looks like this

No. My Sonoff runs stock firmware and I have not flashed Tasmota.

What node-red contrib node are you using?

Two Home Assistant nodes. events:state & call service

One time range node, which is listed under function nodes, native to Node-RED

There are many home assistant nodes, can you give the specific one, also a output of debug after first node, and What timer node is that in the middle

I had mentioned the names of the nodes in my earlier message. events:state & call service & time range

Anyway, my issue is not any errors with Node RED. My issue is how to design a flow to overcome the problem I am facing about random switch on and off, which is obviously due to faulty hardware of the Sonoff switch. Because it is a gang of four switches, I am unwilling to replace the entire gang because of one faulty relay.

I was actually thinking about this today, and idea came to mind... If I was going to do this, I would use persistent global or flow variables or data structure to keep the over all state of the switches.

Then I would do what is called a peak and valley count analysis. Basically this is a performance or event monitoring solution, often used by enterprise monitoring systems. You count the number of peaks and valleys for a given time frame and if there is X peaks and Y valleys in Z seconds... take some some action.

This applies to your on and off scenario. if you have an on-the-off, or an off-then-on too soon, use that as a trigger to check the 'official' state of the switch as stored in your flow or global variables. You only change the state of the flow or global variable for the given switch, when a valid change of state has been done, i.e. say from your phone or NR dashboard, or whatever.

If your flow senses a change other wise, it is an unauthorized change, and triggers the peak and valley analysis. This may be more complex than you want, but over time you could 'tune' the peak/valley counts, or on-then-off, off-then-on unauthorized 'events' so the flow gets smarter at correctly evaluating events.

You can read the state of the switch right? If so... Here is what I think would be the simple model to start with.

AO = Authorized ON
AF = Authorized OFF

UO = Unauthorized ON
UF = Unauthorized OFF

So if flow variable is set AF, and at least one UO occurs, do AF... This forces the given switch OFF, in effect keeping it off as much as possible (less random UF and UO).

So if flow variable is set AO, and at least one UF occurs, do AO... This forces the given switch ON, in effect keeping it on as much as possible (less random UO and UF).

I am assuming that if the switch is AF, and you get UF, you don't care.
I am assuming that if the switch is AO, and you get UO, you don't care

Are you sure your bridge is not generating repeating states, say UF, UF, UF then UO, or something like that? If so, then the peak and valley concept could be applicable, if you want go to that degree of effort. But if you use authorized state to override the unauthorized state as it happens, that would seem based on the above to be sufficient, no?.

If everything above I outlined is true? In reference to how you get ON and OFF events? Here is simple flow that generates random 0 and 1 states, at random seconds (0 < X < 60), and the authorized state is always honored.

[{"id":"5ef7f537.ae175c","type":"inject","z":"4e87fca5.9aef34","name":"Authorized ON","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":120,"y":180,"wires":[["ea133f36.7b00e"]]},{"id":"7b9d89e2.c57978","type":"inject","z":"4e87fca5.9aef34","name":"Authorized  OFF","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":120,"y":220,"wires":[["ea133f36.7b00e"]]},{"id":"ea133f36.7b00e","type":"change","z":"4e87fca5.9aef34","name":"Authorized","rules":[{"t":"set","p":"authorized","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":200,"wires":[[]]},{"id":"617f974.d81ae68","type":"comment","z":"4e87fca5.9aef34","name":"Authorized ON or OFF","info":"","x":140,"y":120,"wires":[]},{"id":"425f0179.83e21","type":"random","z":"4e87fca5.9aef34","name":"Simulate On Or Off State","low":"0","high":"1","inte":"true","property":"payload","x":970,"y":380,"wires":[["2fd34e7a.3ff802"]]},{"id":"a7a7e04b.1fb9c","type":"inject","z":"4e87fca5.9aef34","name":"Automatic (1s)","props":[{"p":"payload"}],"repeat":"1","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":400,"wires":[["5ab612e9.bdcdac"]]},{"id":"d729dead.1d31","type":"debug","z":"4e87fca5.9aef34","name":"Message","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1360,"y":380,"wires":[]},{"id":"5ab612e9.bdcdac","type":"random","z":"4e87fca5.9aef34","name":"Simulate (0s < X < 60s)","low":"0","high":"60","inte":"true","property":"seconds","x":350,"y":400,"wires":[["1c40c58e.669b3a"]]},{"id":"bdcb5c6a.53be2","type":"while-loop","z":"4e87fca5.9aef34","name":"While","condi":"msg.second > 0","x":750,"y":400,"wires":[["425f0179.83e21"],["d5797d11.e73c6"]]},{"id":"a3e71c92.a1ccc","type":"function","z":"4e87fca5.9aef34","name":"Less Second","func":"msg.second -= 1;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":910,"y":540,"wires":[["bdcb5c6a.53be2"]]},{"id":"d5797d11.e73c6","type":"delay","z":"4e87fca5.9aef34","name":"Delay (1s)","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":730,"y":540,"wires":[["a3e71c92.a1ccc"]]},{"id":"1c40c58e.669b3a","type":"change","z":"4e87fca5.9aef34","name":"Seconds","rules":[{"t":"set","p":"second","pt":"msg","to":"seconds","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":400,"wires":[["bdcb5c6a.53be2"]]},{"id":"2fd34e7a.3ff802","type":"function","z":"4e87fca5.9aef34","name":"Authorized?","func":"var theEvent = msg.payload,\n    theAuthorized = flow.get('authorized');\n\nmsg.payload = theAuthorized && msg.payload;\n\nnode.warn(`Event ${theEvent} Authorized ${theAuthorized}`);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1190,"y":380,"wires":[["d729dead.1d31"]]}]

The above flow needs one unique node package https://flows.nodered.org/node/node-red-contrib-random-event-generator. This just to create the random sequencing. Of course in your final control flow, this node would not be needed.

Just send the authorized state to the bridge as the bridge sends back or as you poll the bridge. If the bridge can sent events, that is easier. If your bridge can't send events to NR, then you have to do polling to get the bridge switch state, a bit more complex but the authorized state overriding the current switch state is the same.

I asked for info to help you, if you do not want to give the contrib names and a debug i can not help.

there is no nodes with these names

As to error in node-red, who mentioned errors? i asked for a debug so i knew what messages where coming from the first node.

This would be as simple as monitoring a timestamp and storing it in context e.g

[{"id":"476bf9a1.4bcee","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"timestamp","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":480,"wires":[["5afed0b3.0b63d"]]},{"id":"5afed0b3.0b63d","type":"switch","z":"8d22ae29.7df6d","name":"","property":"payload.time","propertyType":"msg","rules":[{"t":"jsonata_exp","v":"((timestamp - 5000) < $flowContext('timestamp'))","vt":"jsonata"},{"t":"btwn","v":"00.00","vt":"num","v2":"15.00","v2t":"num"},{"t":"else"}],"checkall":"false","repair":false,"outputs":3,"x":460,"y":480,"wires":[[],["415c5f66.7468"],[]]},{"id":"415c5f66.7468","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"timestamp","pt":"flow","to":"timestamp","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":480,"wires":[["ca18810d.3ed83"]]}]

Sorry, if I have pissed you off. It was not my intention.

I had clearly mentioned:

Two Home Assistant nodes. events:state & call service

One time range node, which is listed under function nodes, native to Node-RED

Here is a screencap from my computer

You did not pay attention to what I have already written. Twice.

Anyway, I have confessed in my very first post that I am a noob, and you must be an expert. Therein lies our communication gap.

Thanks. Let me see if I can do it on my own.

I did pay attention but i do not have that contrib installed so i wanted the contrib node name, so i could read up on it.

as asked multiple times now what is the contrib node and can i have a debug of output from first node.

There is no time range node, and the range node does not have multiple outputs, so i am still confused as to what the middle node is, i suspect it is a switch node.

As said if you don't provide the answers and data required to make an informed suggestion i can not help more. I gave you an example of timestamp based check, but with out knowing your set up or data , it may work for you may not.

Good luck in your efforts.

I believe that one of the problems is that Home Assistant comes with some pre-installed nodes that we don't know about. That includes the events and call service nodes I think. I don't think that non-HA users can install those. The result is that often we can't help with HA specific question.

However, @kandissa, I don't think we know what the time range node is, is that part of HA or is it a contrib node?

Yeah, I really wish I had one of these bridges, so I could confirm how and what is send to NR.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.