Do action on power up, then at an interval

Hi. I would like to perform an action from my ESPHome device @ 1800hrs each day… code below…

But I would also like to do the same action immediately on reboot / power on (and then continue at 1800hrs each day). What is a good way of doing that?

Thank you!

time:
- platform: homeassistant
  id: hatime
  on_time:
      - seconds: 0
        minutes: 0
        hours: 18
        then:
           - logger.log: "It's 1800hrs - Time Sent!"
           - uart.write: !lambda |-
              char buf[20];
              sprintf(buf, "T%lu\n", hatime->now().timestamp + 46800);
              std::string s = buf;
              return std::vector<unsigned char>( s.begin(), s.end() );

You are looking for on_boot

1 Like

Thank you… But the Linux Epoch time here is zero… is it possible to get the time to use during ‘on-boot’?

esphome:
    # ...
  on_boot:
    priority: -100
    then:
      - logger.log: "Startup - Time Sent!"
      - uart.write: !lambda |-
          char buf[20];
          sprintf(buf, "T%lu\n", hatime->now().timestamp);
          std::string s = buf;
          return std::vector<unsigned char>( s.begin(), s.end() );

You could do the first one using on_time_synch:, this will execute when your time provider has connected and provided a time.

HOWEVER ESPHome will synch multiple times while running. If you want it done only once set a global variable you can check to see if you have already done the action.

1 Like

Thanks! on_time_sync sorted this… the below is working :slight_smile:

time:
- platform: homeassistant
  id: hatime

  on_time_sync:
    then:
      - if:
         condition:
           lambda: return id(my_global_int) == 0;
         then:  
              - uart.write: !lambda |-
                  char buf[20];
                  sprintf(buf, "T%lu\n", hatime->now().timestamp);
                  std::string s = buf;
                  return std::vector<unsigned char>( s.begin(), s.end() );
                  id(my_global_int) += 1;