Time based enable/disable of Sonoff relay on/off commands via MQTT

I have an oscillating fan that’s controlled by a Sonoff S2X smart plug. I’d like to set up rules on the S2X so that it only responds to MQTT relay on/off commands between certain times, e.g., Fan will turn on/off via a switch on the sitemap, but only between 6pm-9am.
I could write code that runs on the OpenHAB server to do this, but that seems a lot harder to port to another smart plug.

One kludgy way to do it would be to set a rule that sets/resets a GPIO assignment at 6pm/9am.

(http://192.168.1.199/md?g99=7&g1=0&g2=0&g3=224&h3=1&g4=0&g14=0&save=)

http://192.168.1.199/md?g99=7&g1=0&g2=1&g3=224&h3=1&g4=0&g14=0&save=

(Enable/Disable relay)

But that’s ugly. There must be a more elegant method.
Anybody want to share?

Because everything is normalized in OH to a small set of Items and events, I would think it works be easier to pretty to another plug as OH rules don’t care if it’s a Sonoff, Zigbee, MQTT, Shelly, etc. It see a switch and know a switch can receive ON or OFF command.

Typically in an OH configuration, The natural place to put something like this is OH because OH is in the middle. If you hard code the times on the Sonoff, if you ever want to change those times you have to change it on the Sonoff. Of you use DateTime items in OH and Rule, you can set the start time using anything in OH: Astro, iCalendar, time pickers in Sitemaps or MainUI, etc.

You have a lot more flexibility.

The rule is simple as well. Assuming a managed rule with JS Scripting, add a script condition:

time.toZDT().isBetweenTimes(items.StartTime, items.EndTimes);

Or if you don’t use Items you can use a Time condition to define the start and end time. Or as a script condition:

time.toZDT().isBetweenTimes("6:00 pm", "9:00 am');

You can even mix and match, for example stop at sunset and start at 9 am and stuff like that.

The rule triggers on changes to the MQTT Item and the action forwards the change as a command to the Sonoff. The condition will prevent the action from executing of us outside of the allowed times.

As for how to do it on the Sonoff itself, if you don’t get a good answer here you may need to go to their support for help. We probably have a few users of this device but may not have someone with that shirt if expertise.

I forgot to mention that the Sonoff has been flashed with Tasmota, as I do with all my ESP-based gadgets.

The fan in question is referenced on the Sitemap and some Rules, examples are below::

Sitemap:

	Switch    item=Main_Bedroom_Fan_Alex_Main_Bedroom_Fan_Alex_Power_Control 		icon="fan" 			label="Alex's Fan Power [%s]"	visibility=[Main_Bedroom_Fan_Alex_Main_Bedroom_Fan_Alex_Online_Status==ON]

For ease of use some home devices (eg lights, fans, air purifiers) are controlled using 315/433MHz battery powered wallswitches and keyfobs, in addition to being on the Sitemap and controllable through Google voice commands.

Rules:

	//toggle alex's main bedroom Fan
	if	(packet_NUM==2022600)	{
			if (Main_Bedroom_Fan_Alex_Main_Bedroom_Fan_Alex_Power_Control.state==ON) {
				Main_Bedroom_Fan_Alex_Main_Bedroom_Fan_Alex_Power_Control.sendCommand(OFF)
			if (LogLevel==1) {say("4 3 3 code received from wall switch. button 2, turn fan off")}
			}	else	{
			Main_Bedroom_Fan_Alex_Main_Bedroom_Fan_Alex_Power_Control.sendCommand(ON)
			if (LogLevel==1) {say("4 3 3 code received from wall switch. button 2, turn fan on")}
			}	
	}	

Most of my OH code and structure has remained static since early OH2 with the only changes made done in order to make the old code work in OH3 and then OH4; I’ve not kept up with the latest tech because I don’t work in programming or remotely close and, thanks in large part to OpenHAB’s awesome maintainers, Everything Just Works (™).

I guess it’s time for an intense LearnMeSomeCodeSkills session.

Your set up is similar to mine. I use RF remotes as well.
I am on OH4.2 and use ECMAScript rules.
You could do something similar to this test script example:

//use below if the item changes
//var rfinput = items.getItem(event.itemName).state;

//use below to test
var rfinput = "IN"

switch(rfinput) {
case "IN":
    if (time.toZDT().isBetweenTimes("6:00 pm", "9:00 am")){
//do your stuff you want here??
console.log('IN was selected');
    }
    console.log("got to here")
break;    
    case "OUT":
console.log('OUT was selected');
break;
    
case "VACATION":
console.log('VACATION was selected');
break;

//if nothing found do below    
default:  
    console.log("Nothing found in case statements" + rfinput)
}


Manny with similar skill levels find Blockly to be an easier rules environment to work in. It’s really hard to make syntax errors eliminating a while bunch of potential roadblocks. Manny find the visual nature of the blocks to be easier to understand.

As of OH 4.2 there is almost nothing that Blockly cannot do. The same cannot be said for Rules DSL.

My recommendation is to not do any new development of rules using Rules DSL and instead use any of they other options. If you have to relearn it anyway, may as well use a better environment.

The equivalent of JS Scripting code in Blockly is as follows.

I used the startTime and endTime variables to make the screen shot more compact.

I don’t have your variables and Items and Blockly makes you select existing Item so you’d have to replace “MyItem” with your Item names. But the Blockly equivalent to your rule snippet would look like this:

If you didn’t want to use a rule condition but add the test inline in the script action, it would look something like this:

This doesn’t mean I recommend rewriting your existing rules. I’m just trying to demonstrate what Blockly code looks like in a way you can compare to existing code.

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