Support for RTC and NTP on Pi+HA Core

When there’s an internet outage, most of my HA stuff (lights, automations…) stop working due to the Raspberry Pi not having a real-time clock (RTC), so the Pi doesn’t know the correct time. Supporting RTC would enable using a local NTP server, so outside connections wouldn’t be needed for locally-controlled stuff.

There are cheap hardware RTC addons such as the DS3231 but HA Core doesn’t seem to support it. I think one of the tenets of Home Assistant is the local control, which should work even when offline.

You can vote for your own request you know.
:smiley:

1 Like

Most users run on a Pi.
Pi’s don’t have a real time clock and hassos can not support a real time clock.
I think a wide range of users would benefit from this
:+1:

3 Likes

I should also add that I take a pi away on holiday.
It’s a kodi machine and it has a RTC module fitted (I’m just lazy and only like to set the time once in a different tz)
Anyway, the unit came without a battery (batteries are bad as they need replacing and can sometimes leak damaging may be both the RTC and the pi.
So I fitted a 1.2 farrad (yes more than a whole farrad :dizzy_face:, though since then I’ve found 4 farrad capacitors (and all coin sized equivalents), but you have to ensure the voltage is good for at least 5.3v) and that kept time on the pi for 4 months without power.
Not really relevant to the thread but saves buying replacement batteries, is good for the environment and prevents possible damage.
:+1: :+1: :+1:

The RaspBee II includes an RTC, so many HA users may have an RTC available but not used. It would be great to include that kernel module, or at least have a provision to build and install kernel modules automatically like Debian’s dkms system.

3 Likes

Hello,

I think I am on something. Seems like out of the box, hass.io has almost everything needed. In the coming days I’ll try to clarify if yes or no I have got my RTC working. Right now I still need to find out :

  • if there is some fake hardware clock like there is on raspbian
  • how to run a script at boot, before docker starts. I think that systemd could help me. (that’s to set system time from rtc , at boot, if there is no internet)

this is on hass.io, on a rpi 4 with cheap ds3231 device that seems to work as ds1307

2 Likes

That looks interesting, please continue your investigation :slight_smile:

So I’ve tested this :

  • RTC plugged in
  • wifi configured on homeassistant to connect to a local router (not the WAN) with dhcp on.
  • uplink WAN port of local router disconnected
  • my PC connected to that router

I restarted H.A, and it seems that things are working. One thing that is not working as long as WAN is down is root access over port 22222. Weird.

But date seems to be recovered from RTC without further tweaking, without any additional script at startup (basically, I’ve juste loaded the correct kernel modules and enabled I2C and rpi overlay) : (screen inside ha’s terminal plugin
image

1 Like

The procedure I followed is :

  1. modify config.txt on boot partition (as root on 22222 or offline) and add at the end :
dtparam=i2c_vc=on
dtparam=i2c_arm=on

Then I rebooted and checked that I2C device was there :

# ls /dev/i2c-*
/dev/i2c-0   /dev/i2c-1   /dev/i2c-10  /dev/i2c-11

While RTC connected, I launched those commands :

modprobe i2c-bcm2708
modprobe i2c-dev
modprobe rtc-ds1307
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device    #(because I had an error using i2c-1 adapter)

date #to check that the system date is OK
hwclock -w  #to update RTC module's date
hwclock -r  #to read date on RTC module, should be OK

then, since everything was (surprisingly) OK, I modified config.txt again like so (added the dtoverlay line), and also created the i2c_rtc.conf like so (as root on port 22222) :

image

this is the module that I have :

if you want to try …

2 Likes

Configured for HA OS 5.12 on RPi 4B with no ‘root’ access.

  • RTC module DS1307 - see photo;
  • boot disk attached to PC & changes made in config.txt:
dtparam=i2c_vc=on
dtparam=i2c_arm=on
dtoverlay=i2c-rtc,ds3231

On restart (with complete unplug from power) with NO Internet connection see in HA Host log:

[    3.812698] rtc-ds1307 1-0068: registered as rtc0

And voila - time is correct.

4 Likes

So that’s it? Add a couple of parameters to the boot config file and plug in the clock?

followed same , add-ons started but can’t access HA UI
i can see correct time using “NTP Tool” (windows) as I’ve installed chrony addon

1 Like

I managed to get this to work too.

Just added

dtparam=i2c_vc=on
dtparam=i2c_arm=on
dtoverlay=i2c-rtc,ds3231

connected the clock and rebooted.

1 Like

Just wanted to add that this simple config also worked for me. However, I do have another form of this module, which is not meant to be used with an RPi, and is meant to be used with a rechargeable CR2032 battery.
Talking about this thing:

So I wanted to share how to modify it to safely use it with an RPi:

You need to do two things:

  1. Remove some resistors to not damage the RPi
  2. Kill the charging circuit, if you gonna use non-rechargeable batteries

1. Remove some resistors to not damage the RPi
tl;dr: Remove R2 & R3. Don’t bridge, just remove.

If you look at the schematic I gonna link below, you will see that SCL & SDA are pulled high against whatever you supply as VCC. So if you power it with 5V, these pins will be pulled to 5V and will likely damage your RPi, as it only likes 3V3 on its GPIOs.
I initially found that note on this guide, but he didn’t explain why I need to do it, until I found the schematic and that then made sense: https://tutorials-raspberrypi.com/controlling-the-raspberry-pi-rtc-module-i2c-real-time-clock/
You could think: Why not supply the RTC board with 3V3 then? I tried it, but I didn’t get any life sign from it when on 3V3. So it seems to need 5V.
So you need to unsolder (and not bridge) R2 and R3 to stop the board from pulling them high. It’s actually not even necessary to pull them high.

2. Kill the charging circuit, if you gonna use non-rechargeable batteries
tl;dr: Remove D1, R4, R5, R6, make a bridge between pads of R6.

While I was searching for the “Why do I need to remove these R2 & R3 resistors?”, I stumbled over a very critical thing I would not have realized until connecting the board: It has a charging circuit to charge the CR2032 battery: Tiny RTC Charging Circuit Schematic - General Electronics - Arduino Forum
But who on earth owns rechargeable CR2032 batteries!? I didn’t even know those exist xD
So you need to kill the charging circuit. You can either kill two traces, but I liked it more to remove some components and kill the circuit by that, instead of scratching over a PCB with a knive… uuurrggghhhh… Here’s the schematic of the board:


So remove D1, R4, R5, R6 and make a bridge between the pads of R6.

And now the board works with an RPi and my HA can boot without any internet connection! :slight_smile:

Hope this helps somebody having the same RTC boards I ordered :wink:

1 Like

Hi,

I had a similar experience with the cheap zs-042 modules that can be found on ebay. They keep good time but the charging circuit must be disabled. I made a blog post about it which I have included here.

The cheapest clocks around

The ZS-042 module is a very cheap DS3231 breakout board. Common on eBay and Amazon units can be had for less than a dollar. It is a bonus that the module comes with a 4K eprom as well. In my experience these units work well as a clock but damage the LIRC2032 battery that it is supposed to keep charged. I used one with the charger enabled for about a month and found the LIRC2032 had swollen and would not take a charge. So to use these ZS-042s the charging circuit needs to be disabled and a standard CR2032 battery used instead.

There are a lot of discussions on the net about this module. The best discussions can be found at the Arduino forum, the Cave Perl Project and One Transistor . All three sites discuss the charging circuit issue but the Cave Perl Project offers advice on how to make the board more power efficient. One Transistor gives a detailed explanation as to why the charging circuit is a problem on this board.

Basically — the built in battery charging circuit has a good chance of destroying LIRC2302’s because the voltage must be kept at a specific level to charge them. The charging circuit on the ZS-042 is not up to the task for this.

Certainly a standard CR2302 can not be used on an unmodified ZS-042. A non rechargeable battery should not be charged, at any voltage.

As far as I am concerned — the charging circuit must be disabled on this board. I don’t have confidence that I will be able to keep the supply voltage at just the right voltage to charge the battery correctly. Plus trickle charging a LIR2302 continuously is not recommended (see the links for more info).

Removing the charging circuit also has the benefit of allowing us to run the RTC at 3.3 volts. You have to have 5 volts to charge, but the DS3231 has a very wide voltage range.

To disable the charging circuit remove the 1N4148 diode, 200 ohm resistor or both from the board. About two minutes work with a soldering iron. Easy to follow instructions can be found here:

If removing a SMD from your board seems daunting this Youtube video shows you all about it in less than two minutes –> How to Remove SMD Resistors & Capacitors Using a Regular Soldering Iron

A modified board looks like this:

With a simple mod these dirt cheap and highly available modules can be used in all our projects.

Datasheets:

2 Likes

FYI there is a DS3231 module made for the Pi it looks like this, It simply sits on the expansion header.

I’d forgotten all about hwclock until I read this post.

Hi there. Great Job. It’s been a while since you posted this, but maybe you can help me.
This seems to be what I’m looking for, but I’m not using a Pi, but a PC with its own internal RTC.

I’ve got the hwclock, no problem there. Now, can you explain to me how I configure it to use this instead on the NTP?

It would be great if you can help me. I had planned an offline installation, and I found this problem when I’m about to deploy everything.

Have a look at Chrony. Maybe it has an option to update system clock from RTC. Another option is to build a GPS clock to serve as a NTP server.

how to add the GPS clock?

Any clue on how to make it work? How does it function without a battery?