Custom component sensor - batch data

Hi!

I’m developing a custom component and have run into an issue with batch loading. The API I use returns data in batches, each with a timestamp and a value. I can’t poll too frequently, and the list of events is too long to store in extra state attributes (it exceeds the 255-character limit for state).

Is there a way to set the state of a sensor to a value from the batch, but also set the sensor’s state timestamp to match the timestamp when that value actually occurred (not when the batch was received)? Or is there another recommended pattern for handling this kind of time series data in Home Assistant sensors?

Example of data:

[{'timestamp': '2025-06-03T22:04:12+00:00', 'weight_bowl_1': -3.0, 'weight_bowl_2': 0.0}, {'timestamp': '2025-06-03T23:29:08+00:00', 'weight_bowl_1': -6.0, 'weight_bowl_2': 0.0}, {'timestamp': '2025-06-04T01:26:47+00:00', 'weight_bowl_1': -2.0, 'weight_bowl_2': 0.0}, {'timestamp': '2025-06-04T02:44:01+00:00', 'weight_bowl_1': -1.0, 'weight_bowl_2': 0.0}, {'timestamp': '2025-06-04T04:06:41+00:00', 'weight_bowl_1': -2.0, 'weight_bowl_2': 0.0}, {'timestamp': '2025-06-04T08:16:55+00:00', 'weight_bowl_1': -3.0, 'weight_bowl_2': 0.0}, {'timestamp': '2025-06-04T17:30:35+00:00', 'weight_bowl_1': -2.0, 'weight_bowl_2': 0.0}, {'timestamp': '2025-06-04T19:10:55+00:00', 'weight_bowl_1': -2.0, 'weight_bowl_2': 0.0}, {'timestamp': '2025-06-04T19:48:33+00:00', 'weight_bowl_1': -3.0, 'weight_bowl_2': 0.0}, {'timestamp': '2025-06-04T20:51:19+00:00', 'weight_bowl_1': -1.0, 'weight_bowl_2': 0.0}]

Use a data update coordinator.

Hi! I am using a data update coordinator but if the coordinator poll once every 5 minute there is a Diff in time from where it happened to data presented in the sensor. Possible to fix that?

To set the state of the sensor value, you can define a function in your SensorDescription.

e.g. get last weight_bowl_1 from last element of data
MySensorDescription(value_fn=lambda data: data[-1].weight_bowl_1)

In your sensor, you will then use this function for the state value.

MySensor:
    @property
    def native_value(self) -> int:
        return self.entity_description.value_fn(data)

I’m not sure about the timestamp part but I would try something like hass.states.async_set(entity_id, state, timestamp). Hopefully it creates a new datapoint in the recorder so that you can see value changes in the history.