Multiple time zones

Hi, I am trying to display times across multiple time zone

my code is:

time:
  - platform: sntp
    timezone: 'Europe/London'
    id: lontime
  - platform: sntp
    timezone: 'America/New_York'
    id: nytime

    
    

display:
  - platform: tm1637
    clk_pin: D6
    dio_pin: D5
    update_interval: 500ms
    lambda: |-
      static int i = 0;
      i++;
      if ((i % 2) == 0)
        it.strftime("%H.%M", id(nytime).now());
      else
        it.strftime("%H%M", id(nytime).now();
  - platform: tm1637
    clk_pin: D5
    dio_pin: D6
    update_interval: 500ms
    lambda: |-
      static int i = 0;
      i++;
      if ((i % 2) == 0)
        it.strftime("%H.%M", id(lontime).now());
      else
       it.strftime("%H%M", id(lontime).now());

If i declare the London timezone last all, both displays show london time, if i declare NY timezone last, both displays show NY time.

It looks like the nodemcuv2 can only deal in one time zone at a time? Is there a why i can change the time zone as writing it to the display. I want to try to get away from just using static +5 hours as this will not take into account daylight saving.

Thanks
Pete

Does it all need to be on the esp? Home assistant world clock allows adding a sensor to display a different timezone.

Then just bring that sensor into the esp

1 Like

thanks for your reply.

it does
needs to live on the ESP and use STNP. I am making this clock for a friend that does not have home assistant,

I think you’ll have to use a lambda and use a C++ function to get different TZ’s. e.g. Getting current time of a different timezone using C++ - Stack Overflow

2 Likes

It may help to note that keeping time and displaying time are different things.

That said, the ESP firmware (ESPHome, Arduino, etc) expect to be keeping time to only one timezone. (after all, the machine barely has an internal clock at all, let alone two)

But thankfully, using components like ‘text_sensor’, you can display the time in any zone you wish.
So perhaps keep time in UTC, and display it as many ways as you want, using multiple ‘text_sensor’ components. Examples abound for displaying time/uptime using them.

2 Likes

What Nick said. You are going to be writing plenty of lambda if you are doing display anyhow, so you could use lambda in a sensor to retrieve the +/- of the time zones you want to display, the simply apply the difference from UTC when you print to the display.

Thanks Daryl. I am quite new to esphome. any chance of a code example of how you would add hours to time?

Cheers
Pete

Also been looking at time libraries from platformio as per the documentation. I have found a library called " ropg/ezTime" what looks perfect, but when trying to build i get the following error:

/data/travel-clock/.piolibdeps/travel-clock/ezTime/src/ezTime.cpp:10:12: fatal error: EEPROM.h: No such file or directory

****************************************************************
* Looking for EEPROM.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:EEPROM.h"
* Web  > https://registry.platformio.org/search?q=header:EEPROM.h
*
****************************************************************

   10 |   #include <EEPROM.h>
      |            ^~~~~~~~~~
compilation terminated.
Compiling /data/travel-clock/.pioenvs/travel-clock/src/esphome/components/api/proto.cpp.o
*** [/data/travel-clock/.pioenvs/travel-clock/lib2c6/ezTime/ezTime.cpp.o] Error 1
Compiling /data/travel-clock/.pioenvs/travel-clock/src/esphome/components/api/subscribe_state.cpp.o

Any suggestion would be greatly appreciated.

Not likely without your yaml :slight_smile:

The issue with these timezones is that they don’t have DST dates at the same time.
So not only will you need to calculate backwards to NY time, but also keep track of DST.

I haven’t thought this through but perhaps get the time from an API?

ESPhome how to read json from web? - ESPHome - Home Assistant Community (home-assistant.io)

and

World Time API: Simple JSON/plain-text API to obtain the current time in, and related data about, a timezone.

Why not use the standard arduino library Timezone - Arduino Reference

You need to do a bit of work because of how ESPHome implements it.strftime() - but below works and will print GMT + 0.5h on the top left of my display.

First define time in yaml:

time:
  - platform: sntp
    id: my_time
    timezone: "Europe/London"

Then calculate and print:

// string to hold formatted time 
char displaytz1[6];
// time zone offset in hours to calculate plus or minus UCT
int offset = .5; 
// calculate timestamp as UTC + offset in seconds
std::time_t tz1time = id(my_time).now().timestamp+(60*60*offset);
// convert timestamp back into tm time structure
std::tm *tz1time_astm = localtime(&tz1time);
// convert to string for printing (this is because ESPhome it.strftime for some reason doesn't like tm
strftime(displaytz1, 6, "%H:%M", tz1time_astm);
// print formatted time string
it.printf(5, 10, id(calibri_25), TextAlign::TOP_LEFT, "%s", displaytz1);

That looks pretty comprehensive - and may end up being simpler in the long run.

Generally speaking timezones is not something you want to calculate yourself.
The code you posted will suffer from the issue I posted (I understand your code is just an example).
27th of march all clocks shifted to summer time in UK. US did it two weeks prior. And the same at the autumn where UK October 30, but November 6 in US goes to winter time.

That will be a mess to code that part.

The canonical timezone information for computers is in the C library glibc. It is complex.

Yep. You notice I have avoided the issue of timezones and DST completely - and just answered the question about calculating offsets :slight_smile:

That said, you could hard code your DST rules and offsets and use those rules as input into the calculation, avoiding extra libraries for instance. The library Nick linked looked pretty good though.

Thanks every one for your efforts here. I have a working solution that i am happy with.

I have gone for Daryl’s code that allows me to change the time with an offset. The issue with that is trying to track DST across multiple time zones. So I have used an API with timezone.io which can be configured to just return an offset per a location. the http request happens once a day at 1 am,

Thanks again everyone.

Isn’t it 2 am that the DST happens?
So if you calibrate at 1 am then it will be incorrect by the time you get out of bed.
I would recommend 3 am just to be on the safe side

2 Likes

Good call! Thanks

I know that this is an old post, but I need to create a clock similar to yours for a few time zones.

I thought that this would be simple, but ran into the same problems you had.
Because I live in a state that does not change time I have had a great problem trying to figure out the yaml.

Would you mind sharing your complete yaml for this.
Thanks