Correcting Vera time with lua code based on daylight saving

Hello everyone,

My vera system is not affected by the daylight saving. Currently, it’s 1 hour behind (I have +3:30 offset with GMT, so the correct time will be GMT + 4:30). I want to parse the output of os.date() to extract the day, month, and year. Then, in a lua script I would set the system time so that it matches the correct time. However, I did not managed to find a lua function for setting the Vera time by code.

Does such a function exist? And if not, what is the best way to work around this problem?

Thank you!

You shouldn’t have to parse os.date() output, since [tt]os.date “*t”[/tt] gives you a structure:

{
  day = 30,
  hour = 12,
  isdst = true,
  min = 54,
  month = 4,
  sec = 15,
  wday = 7,
  yday = 121,
  year = 2016
}

…in the local time zone. [tt]os.date “!*t”[/tt] will give you the same in Universal time.

{
  day = 30,
  hour = 11,
  isdst = false,
  min = 56,
  month = 4,
  sec = 21,
  wday = 7,
  yday = 121,
  year = 2016
}

(Note the difference in the isdst parameter.)

However, you shouldn’t have to do any of this. I’d suggest that you have your location wrongly set, since Vera should cope with DST.

You must be in Iran. I did some reading about daylight saving in Iran (Iran Standard Time - Wikipedia). Looks like there is no standard rule for transitions to and from daylight saving time, so presumably Vera doesn’t even try.

To answer your question: you can’t set the system clock from Luup. But trust me, you don’t want to do that. Changing the clock means lying to Vera about what time it is, worldwide, so (1) it’ll be undone the moment Vera tries to sync its clock with a time server, and (2) any Vera scene based on sunrise and sunset would be off by an hour. What you need to do is change your UTC offset to UTC+4:30. Fortunately there is such a time zone, used by Afghanistan. If that is selectable as a time zone on Vera then you should select it. Leave your latitude/longitude as whatever city in Iran you live in, so that you sill get correct sunrise and sunset times.

It may seem counterintuitive to select an entirely different country but as long as it has the same UTC offset it works.

As usual, @futzle has the definitive answer!

@akbooer: Thanks a lot for these clues on lua time function. I only know very few things about lua, so basically I gather small chunks of information by reading these forum’s posts :slight_smile:

@futzle: Thank you very much and for the exact reply for my question. Yes, I am in Iran. Actually, I just checked the daylight saving time/date for a range of years (like 2009-2017) and I noticed that on every leap year it will be 21st of March (correspondingly September), and in other year 22nd of March. So, I would just need to know if CurrentYear - 1972 is divisible by 4.

However, this is only a hypothesis ;D I need to do a bit of research. But we do know when to change the time in Iran without any doubt! :slight_smile:

Choosing Afghanistan solves the problem in the first half of the year, but you need to manually correct it in the second. Can I make it automatic through luup?

Regards,
Mona

Possibly, but you will need a root shell on your Vera. I think the file you need to edit is /www/cmh/timezone.json, because that file seems to correspond to the list of time zones that are shown in the UI.

The format in the “value” field is reasonably well documented. Do a web search for the TZ environment variable in the GNU C library and you’ll find a definition.

I don’t think that the rule you’ve reverse-engineered can be represented perfectly in the TZ format, so you may have to set a reminder to edit the file once a year to reflect the changing start and end dates. At least you won’t have to wait until exactly the day of the change to edit it.