Is there any way to send backdated sensor data to HA?

Ok, well I got it to “work”, but only by inserting some hacky code into the HA codebase to convert the dict states into true HA State objects when processing Events. I won’t bother posting the code here, because I don’t think anyone should “fix” this like this, but for the record, I’m checking the class of the “new_state” key within the if event.event_type == EVENT_STATE_CHANGED branch of the _process_one_event method in the recorder/core.py file.

My main goal in doing that was just confirming this was the problem, which appears to be the case. So to review, the issue is that scripts (and automations) do not send a true State object as part of the state_changed event, instead they’re just sending a dict, which the code cannot handle. On the other hand, the Events page of the dev tools do send real State objects, so it does work from there.

My “fix” is checking these Events when the recorder pulls them off, and converting the state dicts to State objects if needed. I think the more proper fix would be finding where in the code these Events are put on the bus and ensuring they are converted to States there. I may try to create a PR to do that, but I’m not familiar with the HA code base, or with Python, so it’s unlikely I’ll be able to figure that out (I only found the spot in the recorder code because I had stack traces to guide me). If anyone has any ideas and wants to point me in the right direction that would be appreciated.

More realistically though, I do wonder if there is any way to get this to work as is? Has anyone actually sent state_changed events from an automation or script? Does anyone know of a way to create a true State object with templating?