Daylight Saving Time (DST)

Hi,
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  "europe.pool.ntp.org"

#define NTP_OFFSET_1   3600                    // DST - 1*60*60 summer - in seconds
#define NTP_INTERVAL_1 60 * 1000               // In miliseconds
#define NTP_ADDRESS_1  "europe.pool.ntp.org"

WiFiUDP ntpUDP;
WiFiUDP ntpUDP1;
NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);
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:

   EEPROM.begin(8);
   dstStatus = EEPROM.read(0);                               // is DST on or off

    if (dstStatus == 1)
     {
       timeClient1.begin();                                  // start the DST defined time client       
     }
     else
     {
       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
           sendState("off");
           dstStatus = false;
           timeClient1.end();
           timeClient.begin();                               // use winter time client
                                                             // another option - ESP.restart();
         }       
      EEPROM.commit();       
     }
   }

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