Esphome Set your time

substitutions:
  timezone: "Europe/Kiev"

esp32:
  board: esp32-s3-devkitc-1
  variant: esp32s3
  flash_size: 16MB
  framework:
    type: arduino

esphome:
  name: "time"
  friendly_name: "time"
  platformio_options:
    board_build.flash_mode: dio
  on_boot:
    - priority: 200
      then:
        - ds1307.read_time:
            id: rtc_time

psram:
  mode: octal
  speed: 80MHz

i2c:
  - id: bus_slow
    sda: GPIO01
    scl: GPIO02
    scan: True
    frequency: 100kHz  

number:
  - platform: template
    name: "Year Select"
    icon: "mdi:hours-24"
    id: year_select
    mode: BOX
    min_value: 2025
    max_value: 2040
    step: 1
    initial_value: 2025
    unit_of_measurement: "Year"
    update_interval: 0s
    optimistic: true   

  - platform: template
    name: "Month Select"
    icon: "mdi:hours-24"
    id: month_select
    mode: BOX
    min_value: 1
    max_value: 12
    step: 1
    initial_value: 1
    unit_of_measurement: "Month"
    update_interval: 0s
    optimistic: true   

  - platform: template
    name: "Day Select"
    icon: "mdi:hours-24"
    id: day_select
    mode: BOX
    min_value: 1
    max_value: 31
    step: 1
    initial_value: 1
    unit_of_measurement: "Day"
    update_interval: 0s
    optimistic: true   

  - platform: template
    name: "Hours Select"
    icon: "mdi:hours-24"
    id: hours_select
    mode: BOX
    min_value: 0
    max_value: 23
    step: 1
    initial_value: 0
    unit_of_measurement: "Hours"
    update_interval: 0s
    optimistic: true   


  - platform: template
    name: "Minutes Select"
    icon: "mdi:hours-24"
    id: minutes_select
    mode: BOX
    min_value: 0
    max_value: 59
    step: 1
    initial_value: 0
    unit_of_measurement: "Minutes"
    update_interval: 0s
    optimistic: true   

text_sensor:
  - platform: template
    name: "Current Time"
    id: current_time
    icon: mdi:clock-time-eight-outline
    lambda: |-
      auto time = id(rtc_time).now();
      return time.strftime("%Y-%m-%d / %H:%M");
    update_interval: 5s

button:
  - platform: template
    name: "Set Manual Time"
    id: set_time_button
    icon: mdi:clock-time-six-outline
    on_press:
      - script.execute: set_time

script:
  - id: set_time
    then:
      - lambda: |-
          setenv("TZ", "${timezone}", 1);  
          tzset();  

          struct tm time_info;
          time_info.tm_year = id(year_select).state - 1900;
          time_info.tm_mon = id(month_select).state - 1;
          time_info.tm_mday = id(day_select).state;
          time_info.tm_hour = id(hours_select).state;
          time_info.tm_min = id(minutes_select).state;
          time_info.tm_sec = 0;
          time_info.tm_isdst = -1;

          std::time_t t = mktime(&time_info);
          struct timeval tv = {t, 0};
          settimeofday(&tv, NULL);

          ESP_LOGD("main", "Time set to: %s", ctime(&t));
      - delay: 1s    
      - component.update: current_time    
      - ds1307.write_time: 
          id: rtc_time
      - logger.log: Time synchronized ds1307
      - delay: 1s
      - ds1307.read_time: 
          id: rtc_time
      - logger.log: Time read by ds1307

time:
  - platform: homeassistant        
    id: ha_time
    update_interval: 60s
    on_time_sync:
      then:
        - ds1307.write_time: 
            id: rtc_time
        - logger.log: Time synchronized ds1307
        - delay: 1s
        - ds1307.read_time: 
            id: rtc_time
        - logger.log: Time read by ds1307

  - platform: ds1307
    id: rtc_time
    timezone: "unknown"
    i2c_id: bus_slow
    address: 0x68
    update_interval: never

1 Like