How to send local hour as a number (when it changes) from raspberry to esp8266

Hello, i am a beginner and i have some unsolved problems, so any help would be much appreciated.
I want to send (through mqtt) from raspberry the local hour as a number to a remote esp8266 every time it changes.
i have already established a communication and i can receive and send commands to the esp8266, but how can i send an integer?
I have installed NTP binding to get the hour.
Also, i have created the item:

Number TimeFromRaspberry "Time: [%d]" <time> {mqtt=">[broker:Command/Home/SeF/TimeFromRaspberry:Command..."}

but i don’t know what to write after the “Command”!

and of course a rule is needed but after many unsuccessful tries i realize i can’t write it!

Thank you in advance,


Number TimeFromRaspberry "Time: [%d]" <time> {mqtt=">[broker:Command/Home/SeF/TimeFromRaspberry:command:*:default"}

and in a rule

rule "Minute Timer"
when Time cron 15 0 * * * ?" then	{	
var Hours = now.getHourOfDay as Number
}  end

You may have to experiment with the number “15” in the cron statement if the time on your Raspberry is out and you find the wrong hour being sent. the “15” means it runs at 15 seconds past the hour change.
This is getting the hour from the system, which is what I do and nothing to do with the ntp binding

1 Like

Thank you very much for the reply. After some minor syntax mistakes it worked!!!

Now on the esp8266 side: how to store this number in a variable?
i will try out some things and i will update the post if come up with the solution.

1 Like

What are you trying to do with your ESP8266?

I want to turn on a LED between 5 p.m and 10 p.m.
So i need a simple comparison between numbers.
But in the code esp8266 convert every messages that receives into a string.

void callback(char* topic, byte* payload, unsigned int length) {
  // Convert the incoming byte array to a string
  payload[length] = '\0'; // Null terminator used to terminate the char array
  String message = (char*)payload;
  // Messages for turning LED on/off
  if (message == "LEDON") {
    LedOperationFunction(false); // Manual LED operation is activated
  } else if (message == "LEDOFF") {
      LedOperationFunction(false); // Manual LED operation is activated
// Message that contains time from raspberry
  if (message = "???????") {
    RaspberryHour = message;

So i think i need to convert back the string to integer somehow

Why not just access the ESP’s GPIO pin direct from OpenHAB and use the time rule to turn the LED ON and OFF? It’s what I do using ESPEasy on my ESP’s. You then can also add in a System started trigger if you need one so it activates after a reboot if it’s the correct time.

A typical item for ESPEasy looks like

Switch MyLED		(gSOff)			{mqtt=">[broker:deviceName/gpio/5:command:OFF:1],>[broker:deviceName/gpio/5:command:ON:0]}

where broker is the name you gave your broker connection in etc/openhab2/services/mqtt.cfg
and deviceName is the topic you told your device to subscribe to.

The rules file then becomes

rule "Hour Timer"
when Time cron 15 0 * * * ?" then	{	
var Hours = now.getHourOfDay as Number
if (Hours > 16 && Hours < 22) {MyLED.sendCommand(ON)} else {MyLED.sendCommand(OFF)}
}  end

You can then expand it to more GPIO Pins in future without reprogramming the ESP

1 Like

I think openhab should send the led-switch on and off command at the given times to your esp.

You already send the hour to your esp, so you also can send a simple on/off command instead.

Your idee would be good, when there is no connection between esp and openhab and esp has to get the time (hour of day) at it´s own, but when you already must send the time to the esp, you can do it all on the raspi side.

This is much easier, only one “brain” inside your installation and esp is only used as a simple switch…

Yes this is a solution that i thought about it and i already have accomplish it in another esp.

But for this esp i want to be very realiable. So i have to create some mechanisms that would prevent false function.

  1. i have implemented in the esp code an ntp server connection so the esp itself get the hour through internet.
  2. i also send to the esp the hour from raspberry
    so if any of these mulfunction the other one continous.
    I also write the value of the variable time in the eeprom, so if both time servers are not working, the esp itself can calcute time through millis().

I you were use OpenHAB to do the heavy lifting, you could change things about a bit for reliability. By using the setup below the item in OpenHAB would get the LED’s status from the ESP, and only update after the ESP has received and acted upon the LED Command. This is the setup I use. I set up a switch device in ESP Easy that gets its status from the GPIO Pin State.
To do this the item becomes:

Switch MyLED (gSOff)			{mqtt=">[broker:deviceName/gpio/5:command:OFF:1],>[broker:deviceName/gpio/5:command:ON:0],<[broker:deviceName/ledState:state:ON:0],<[broker:deviceName/ledState:state:OFF:1]",autoupdate="false"}

Your rule can then be repeated to resend the command if it has not operated. For example

rule "Hour Timer"
when Time cron 30 0 * * * ?" then	{
var Hours = now.getHourOfDay as Number
if (Hours > 16 && Hours < 22) {
if (MyLED.state == OFF) {
logInfo("MyLED", "MyLED didn't turn on the first time so sending again")
MyLED.sendCommand(ON) }
} else {
if (MyLED.state == ON) {
logInfo("MyLED", "MyLED didn't turn OFF the first time so sending again")
MyLED.sendCommand(OFF) }
}  end

The log statement allows you to look in openhab.log to see if it is missing commands. Adding


to the end of the sitemap item entry will allow you to see if the command has worked, as the buttons will only turn on when a state has been received. I also have my ESP’s send back their uptimes, and I reboot them every 28 days, as I’ve found commands start to get lost as their uptime increases. if the uptime reverts to 0 I trigger a rule that resets their items.

1 Like

Thank you very much Kevin. When i find some time i will try your suggestion. It seems to be better.

Kevin can you please share an esp code of yours, so i can take an idea of how you perform the communication between openhab and the esp?

I reflashed the ESP’s with ESPEasy, take a look at

From then on you access the device initially via an access point it creates, then when you’ve told it your WiFi details you configure it via a webpage. You can build the software using either Arduino IDE or Platformio, or you can simply flash with one of the slightly older prebuilt flash files. For just toggling an LED, the prebuilt image is fine. ESPEasy communicates via MQTT over WiFi.
The distribution comes with a flash programmer, so the esp is connected to a usbasp or similar usb to serial adapter if it doesn’t have one onboard, you then start it whilst shorting gpio0 to 0v, assuming it doesn’t have an onboard button already fitted that does it. Then start the flash program, tell it the serial port and click “flash”
It’s easier than it sounds, I’ve done a lot of these, both with and without onboard usb to serial.

1 Like