Esphome Time actions triggering at random times

I am implementing a light metering device to estimate PPFD and DIL (daily integral of light). It needs a reset at the end of each day so i set a real time clock based on sntp. Unfortunately the value of DIL is being reset to zero at random times.
I attached as much of the code as I felt was pertinent. Originally I had included a check that time minutes was less than 2 so that the reset of DLI would be only at midnight. That made no difference. I considered if a bad reading from the BH1750 could make a difference. A data validation test for lux_1 greater than zero made no difference.
It all looked simple enough until it didn’t work. Help would be greatly appreciated.

esphome:
  name: lux-sensor-1

esp8266:
  board: d1_mini
.
.
.
.
.
.
i2c:
  sda: D2
  scl: D1
  scan: false 
  id: bus_a

time:
  - platform: sntp
    id: esp_time

sensor:
  - platform: bh1750
    id: Lux_1
    name: "Lux 1"
    address: 0x23
    update_interval: 60s

  - platform: template
    name: "DLI 1" 
    id: dli_1
    unit_of_measurement: "mol/m²/day"
    accuracy_decimals: 2
    update_interval: 60s
    lambda: |-
      static float dli_1 = 0;
      float ppfd = id(Lux_1).state / 43.5; // convert lx to ppfd for sunlight
      dli_1 += (ppfd);
      //reset at midnight
      if (id(esp_time).now().hour == 0){
       dli_1 = 0;
      }
      float result = dli_1 * 0.00006; // covert minutes to seconds and micromol to mol
      return result;

I don’t see a check for the time being valid so if the ESP reboots it might think it’s midnight before it gets a valid time.

Why wouldn’t you just use the integration sensor and use an on_time trigger in the time component for the reset? That shouldn’t trigger unless the time is valid.

The scaling of the sensor value can be done with a filter, so you could do the whole thing with no lambdas.

This is just what occurred to me when I sat down to make my monitor. Your suggestion is a good end run on whatever is happening here. I still would like to understand what went wrong. Unfortunately it sometimes take a whole day to find out what isn’t working. Thank for the help, I will give your approach a try.

Since use of the time functions seem to not work within lambda, I implemented the following


i2c:
  sda: D2
  scl: D1
  scan: false 
  id: bus_a

time:
  - platform: sntp
    id: esp_time

    on_time:
      - seconds: 0
        minutes: 0
        hours: 0
        then:
          - sensor.integration.reset: DLI_1

sensor:
  - platform: integration
    unit_of_measurement: 'mol/m²/day'
    name: "DLI_1"
    sensor: Lux_1
    time_unit: d
    id: DLI_1
    filters:
      - lambda: return x * 60 * 0.001 * 0.023; //convert min to sec, micromol to mol and lx to ppfd

  - platform: bh1750
    id: Lux_1
    name: "Lux 1"
    address: 0x23
    update_interval: 60s

I find now that the integrator does not reset at midnight. I also find that the integrator just continues forever rather than integrating for a day and resetting. Does the time function work at all? Perhaps I don’t understand the integration function, but it seems useless unless I can reset it to do periodic measurements.
I could certainly send just the lux value to home assistant and do all the work with a template, but it seems wasteful to not do the integral at the esp.

I gave up on using the time function in ESP home. I have to conclude that it is broken.
This solution, using home assistant as the timer appears to be working. I set up a time of day helper with an on period from midnight to four minutes after midnight, with the identity binary_sensor.midnight. I have verified that the integration is reset when the “midnight” helper is forced through the developers tools.

ESP code:

i2c:
  sda: GPIO4
  scl: GPIO5
  scan: false 
  id: bus_a

sensor:
  - platform: integration
    unit_of_measurement: 'mol/m²/day'
    name: "DLI_1"
    sensor: Lux_1
    time_unit: d
    id: DLI_1
    filters:
      - lambda: return x * 60 * 0.001 * 0.023; //convert min to sec, micromol to mol and lx to ppfd

  - platform: template
    name: "ppfd_1"
    update_interval: 60s
    lambda: |-
      float ppfd_1 = id(Lux_1).state * 0.023;
      return ppfd_1;

  - platform: bh1750
    id: Lux_1
    name: "Lux 1"
    address: 0x23
    update_interval: 60s

binary_sensor:
  - platform: homeassistant
    name: "integration reset"
    entity_id: binary_sensor.midnight
    on_state:
      then:
        - sensor.integration.reset: DLI_1