Epoch Time to Serial Port

Hi All

Does anyone have any suggestions on how I would use ESPHome to send the time to UART every 60 seconds or so, (Linux epoch, pretended with T) - essentially the following; but replacing the epoch with the current timestamp:

  - interval: 1m
      - uart.write: "T1626863633\n"

It looks like I have to use lambda to do it… but not sure how I get a lambda value, prepend the T, and pass it through to the uart.write command…

auto time = id(sntp_time).now();



Hi Nick

Sorry, very new to this, how to I write that time.timestamp to uart.write?

I have tried with the following…

  - platform: homeassistant
    id: hatime

  - interval: 30s
      - uart.write: "T"
      - uart.write: !lambda 'auto time = id(hatime).now(); return time.timestamp;'
      - uart.write: "\n"
      - uart.write: "A"

But I get the following error:

/config/esphome/esp.yaml: In lambda function:
/config/esphome/esp.yaml:57:46: error: could not convert 'time.esphome::time::ESPTime::timestamp' from 'time_t {aka long int}' to 'std::vector<unsigned char>'
       - uart.write: !lambda 'auto time = id(hatime).now(); return time.timestamp;'
*** [/data/esp/.pioenvs/esp/src/main.cpp.o] Error 1

In case someone stumbles across this in the future…

The following is outputing T followed by timestamp, \n

I required the time to be local time, NOT UTC… so depending if dst is true or not I’m adding on 12 or 13 hours…

      - uart.write: !lambda |-
              char buf[20];
              if( hatime->now().is_dst ) {
                sprintf(buf, "T%lu\n", hatime->now().timestamp + 46800);
              else {
                sprintf(buf, "T%lu\n", hatime->now().timestamp + 43200);
              std::string s = buf;
              return std::vector<unsigned char>( s.begin(), s.end() );
1 Like