How to determine if RTC is working

I have a DS1307 RTC configured:

time:
  - platform: ds1307
    update_interval: never
    id: on_board_time
  - platform: homeassistant
    id: homeassistant_time
    on_time_sync:
      then:
        ds1307.write_time:

This is, according to the docs, supposed to update by default every 15 minutes and on boot:

 on_boot:
    then:
      - ds1307.read_time:

I use this as below:

    on_turn_on:                   # When motor runs, publish time to HA
      then:
        - text_sensor.template.publish:
            id: last_feed
            state: !lambda |-
                char str[17];
                time_t currTime = id(on_board_time).now().timestamp;
                strftime(str, sizeof(str), "%Y-%m-%d %H:%M", localtime(&currTime));
                return  { str };

and this seems to work fine, publishing to the text_sensor when asked and this then shows up in HA front end.

However, this is working even when the DS1307 isn’t physically connected, so it is using the homeassistant_time time.

I need to determine if the DS1307 is actually working (and when it isn’t) but it doesn’t seem obvious how when time_t currTime = id(on_board_time).now().timestamp; updates regardless of whether the DS1307 exists.

In the logs I do see an error, which I expect when it’s not connected but the currTime still gets updated.

[16:26:25][C][ds1307:023]:   Address: 0x68
[16:26:25][E][ds1307:025]: Communication with DS1307 failed!

In short, what do I need to change to ensure that the time is coming from the RTC?

The DS1307 is, indeed working.

I commented out…

  - platform: homeassistant
    id: homeassistant_time
    on_time_sync:
      then:
        ds1307.write_time:

… which means the DS1307 is not synched with HA time and time is still showing up correctly.

What I’m still puzzled about is why this…

    on_turn_on:                   # When motor runs, publish time to HA
      then:
        - text_sensor.template.publish:
            id: last_feed
            state: !lambda |-
                char str[17];
                time_t currTime = id(on_board_time).now().timestamp;
                strftime(str, sizeof(str), "%Y-%m-%d %H:%M", localtime(&currTime));
                return  { str };

…was still updating when the DS1307 wasn’t physically present.

on_board_time is clearly defined as cupdating from the DS1307.

time:
  - platform: ds1307
    update_interval: never
    id: on_board_time

What is going on here?

1 Like