Controlling a SonOff S20 switch

Controlling a sonoff S20 device

I've just finished writing an instruction sheet, for my IoT students, on how to control a SonOff S20 mains switch.

Note: This exercise uses a WeMos D1 Mini, Node-RED and MQTT (mosquitto) and of course a SonOff S20.

I hope you enjoy reading this as much as I enjoyed writing it.

Regards from David

PS: The document, in the above link, has been updated to implement the comments from @zenofmud.

3 Likes

Nice and a coulple comments

The word ‘flow’ is over used since everything on every tab is your ‘flows’ while a series of nodes connecter together is a flow, and all the nodes on a tab make up a flow. I'd say:

The ‘flow-variable’ is usable by all nodes in a flow tab.

This is a nitpick but I think it helps reduce confusion.

In the ‘Pass repeated signal ONCE’ node you have

if ( (msg.payload == 1) && (sonoff_s29_status == "off") )
{
	msg.payload = 1;
	sonoff_s29_status = "on";
	flow.set("sonoff_s20_status",sonoff_s29_status);
	return msg;
}

Since msg.payload is already 1 this could be written

if ( (msg.payload == 1) && (sonoff_s29_status == "off") )
{
	flow.set("sonoff_s20_status","on");
	return msg;
}

same for the second ‘if’.

Another case that is not handled - what if the device it 'on' when the flow starts. The 'Initial conditions’ function will set “sonoff_s20_status” to “off” even though it is on. If you try to turn it off, the code will think it is off so it won’t turn it off and the light stays on. Might want to check the status of the light when the flow starts OR use this as a test case for the kids to figure out how to handle it.

Thanks for your inputs Paul. I'll include your improvements later today.
I meant to include a wire from the 'initial_conditions' to turn the SonOff off at the start - for some reason this got omitted. I'll put that right later today.