This example shows how to take an action based on a time or schedule. It uses the excellent WP Control plugin.
This plugin allows you to easily set the time window or repeating schedule that you want to use. Here is my logic that I would like to implement. I’ve highlighted my criteria in green and my action in red
If it is 6pm I want to start watering my plants for 3 minutes.
Firstly, I set up a new cron schedule using WP Control. This looks like the picture below. Choose a daily schedule and don’t forget when you first want it to run! I can now start writing my logic in the box.

In order to turn on my pump I send a mysensors_in/10/6/1/0/2 the playload contains the number of minutes that the pump should be on.
So lets have a first go at implementing our logic. It is possible to send data to your Mqtt broker really easily using the MqttCogs_Plugin class. This is REALLY easy. The sendMqtt function will even automatically buffer messages for MySensors nodes that implement smartSleep.
I’ve expanded the call parameters into variables but this is not necessary.
$clientid = 'cron_w'; //can be any short text
$outtopic = 'mysensors_in/10/6/1/0/2'; //topic to write to
$outpayload = 3; //payload, on for 3 minutes
$qos = 1; //qos
$retain = 0; //retain
$error = new stdClass();
$mqttcogs = new MqttCogs_Plugin();
$mqttcogs->sendMqtt($clientid, $outtopic, $outpayload, $qos, $retain, $error);
Here’s a second go. This one picks up the return from the function and then emails the result. I haven’t expanded the variables this time to keep things concise.
$mqttcogs = new MqttCogs_Plugin();
$error = new stdClass();
//note that this function returns
// true (message ok or buffered) or
// false for error
// I just use the $error object though...
$mqttcogs->sendMqtt('cron_w', 'mysensors_in/10/6/1/0/2', 3, 1,0, $error);
switch($error->status) {
case 'ok':
wp_mail( 'test@test.com', 'Watering on', 'Message sent' );
break;
case 'buffered':
wp_mail( 'test@test.com', 'Watering on', 'Message buffered' );
break;
case 'error':
wp_mail( 'test@test.com', 'Watering FAIL', $error->errors[0].reason);
break;
}