Daylight Saving Time (DST)

if you have an ESP8266/MQTT based sensor which reads a time server in order to timestamp its readings, it’s a pain having to update the sketch twice a year to adjust the offset. Anyway, I have just implemented an idea which may be of use to others:

in the sketch define 2 separate time clients like so:

#define NTP_OFFSET   0                         // winter - in seconds
#define NTP_INTERVAL 60 * 1000                 // In miliseconds
#define NTP_ADDRESS  ""

#define NTP_OFFSET_1   3600                    // DST - 1*60*60 summer - in seconds
#define NTP_INTERVAL_1 60 * 1000               // In miliseconds
#define NTP_ADDRESS_1  ""

WiFiUDP ntpUDP1;
NTPClient timeClient1(ntpUDP1, NTP_ADDRESS_1, NTP_OFFSET_1, NTP_INTERVAL_1);

set up an MQTT switch on your HA front end to send a message to the sensor to turn on/off DST.

In the sketch store a DST status indicator in EEPROM and activate the relevant time client depending on the DST status, for example:

   dstStatus =;                               // is DST on or off

    if (dstStatus == 1)
       timeClient1.begin();                                  // start the DST defined time client       
       timeClient.begin();                                   // start the Winter defined time client       

Also set up a subscription in the sketch to change DST, for example:

   Adafruit_MQTT_Subscribe *subscription;                    // subscribe to the defined mqtt feed
   while ((subscription = mqtthwt.readSubscription(5000)))
    if (subscription == &hwdst)                              // check for topic when subscrition received
       String msgPayload = " ";               
       msgPayload = (char *)hwdst.lastread;                  // extract the payload  
       if (msgPayload == "on")
           EEPROM.write(0, 1);                               // store the DST time indicator
           sendState("on");                                  // report switch state to Hass
           dstStatus = true;
           timeClient.end();                                 // stop the winter time client
           timeClient1.begin();                              // use DST time client
      if (msgPayload == "off")
           EEPROM.write(0, 0);                               // winter time
           dstStatus = false;
           timeClient.begin();                               // use winter time client
                                                             // another option - ESP.restart();

The sendState() routine sends the switch state message back to HA

The switch config on HA could look like:

  - platform: mqtt
    name: "Daylight Savings Time"
    command_topic: "msqto/priory/hwt/command"
    state_topic: "msqto/priory/hwt/state"
    payload_on: "on"
    payload_off: "off"
    state_on: "on"
    state_off: "off"
    optimistic: false

I just completed this today and it works nicely. I hope it’s useful to someone.

1 Like