Filtering out data out of expected range

Im trying to filter out excessive spikes in data coming from my BME680.

Ive fitted 4k7 pull up resisters to the data lines and that has helped but I still get the problem every now and then.

I have been looking for a way to catch this information and eliminate it and found in this issue code that would help.

I am now trying to find a way to update a sensor in HA to display the count_missed results_t.

This guy updated the logs with

ESP_LOGW("main", "Missed Temperature Data %d", count_missed_t);

But I would prefer to replace this line with something that would update a sensor in HA. Ive created a sensor in ESPhome that works but I’m having trouble updating data to it and would be grateful for some help as not very good with C++

So far I’ve added to my original ESPhome code:

 - platform: template
   name: "Lounge Temperature missed"
   id: l_temp_missed
   lambda: |-
     return id(count_missed_t);
   update_interval: 60s

Ive tried creating a global variable count_missed_t and tried sensor.template.publish but have ended up confused with it all.

Any ideas on how I could do this.

Try placing the template under text_sensor: rather than sensor:.

I have tried that but its updating the sensor that’s not working, I think I’m over complicating it.

At the end of the day all I was hoping to do is write a line or two of code in lambda to replace the

ESP_LOGW("main", "Missed Temperature Data %d", count_missed);

in the code below to create and update a HA sensor instead of logging a message in the logs. The HA sensor would then show a count of any errors in the temperature reading.

sensor:
  - platform: bme680_bsec
    temperature:
      name: "L Temperature"
      id: l_temperature
      accuracy_decimals: 2
      sample_rate: lp
      filters:
        - lambda: |-
            float MAX_DIFFERENCE = 2.0; 
            static float last_value = NAN;
            static int count_missed = 0;
            count_missed = 0;
            if (isnan(last_value) || std::abs(x - last_value) < MAX_DIFFERENCE) { count_missed = 0;
              return last_value = x;
            } else {
              count_missed += 1;
              ESP_LOGW("main", "Missed Temperature Data %d", count_missed);
              return {last_value};
            }