Value sent from esp8266 code not the same as recieved(solved)

Hello i have a strange problem, i use custom sensor component and i send 39768182 but i receive 39768184 in HA and i just cant figure out why this is a simplified code that shows the problem
.yaml

esphome:
  name: livingroom
  platform: ESP8266
  board: esp01_1m
  includes:
    - my_custom_sensor.h

wifi:
  ssid: ""
  password: ""

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Livingroom Fallback Hotspot"
    password: "tAksgOaENwUm"

  
captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
    safe_mode: True


sensor:
- platform: custom

  lambda: |-
    auto my_sensor = new MyCustomSensor();
    App.register_component(my_sensor);
    return {my_sensor->temperature_sensor, my_sensor->pressure_sensor};

  sensors:
  - name: "My Custom Temperature Sensor"
    unit_of_measurement: °C
    accuracy_decimals: 0
  - name: "My Custom Pressure Sensor"
    unit_of_measurement: hPa
    accuracy_decimals: 0

my_custom_sensor.h

class MyCustomSensor : public PollingComponent {
 public:

  Sensor *temperature_sensor = new Sensor();
  Sensor *pressure_sensor = new Sensor();

  MyCustomSensor() : PollingComponent(15000) { }

  void setup() override {

  }

  void update() override {
	  unsigned long test_data=39768182;
  

    temperature_sensor->publish_state(test_data);

    pressure_sensor->publish_state(test_data);
  }
};

and this is what i get when i run it (same that shows up in ha)

0000 hPa with 0 decimals of accuracy
[19:52:57][D][sensor:092]: 'My Custom Temperature Sensor': Sending state 3976818
4.00000 °C with 0 decimals of accuracy
[19:52:57][D][sensor:092]: 'My Custom Pressure Sensor': Sending state 39768184.0
0000 hPa with 0 decimals of accuracy
[19:53:12][D][sensor:092]: 'My Custom Temperature Sensor': Sending state 3976818
4.00000 °C with 0 decimals of accuracy
[19:53:12][D][sensor:092]: 'My Custom Pressure Sensor': Sending state 39768184.0
0000 hPa with 0 decimals of accuracy

I just cant figure out why and its messing with the custom sensor I’m trying to write
I’m using the latest version of esphome downloaded just a few days ago
any and all tips big and small are welcome thank you!

Not sure if this is fixing it all, but Sensor#publish_state expects a float, not a long: https://esphome.io/api/classesphome_1_1sensor_1_1_sensor.html#a0df7b004586f6ab9eecfbabb25fcdf6c

Although, I just compared this with a custom sensor I just built, and I am sending a uint8_t value successfully through that API call.

Your problem is that an IEEE-754 float has an effective 24 bit mantissa (23 bits + 1 bit assuming its normalized). You can’t exactly represent that value in 24 bits, so its stored with the exponent 1 and you lose a bit of precision off the bottom.

See IEEE 754 - Wikipedia for the details.

What is more, that value is not in the realistic range for hPa (in the context of measuring air pressure). One atmosphere is about 1013 hPa. 39768182 is not a realistic value to test with :slight_smile:

Ah OK so it was that simple hmm i guess i will have to send it as text instead then, i was hoping to avoid that But thank you all very much now i know how to proceed.