Entity history querying in automations

Is there a way to query the history of an entity in an automation or script? For example, I know you can pull the last_changed from an entity but that will only be the immediate last attribute change. I know History has all the information and that you can try to directly access the database or restful API but it seems like it’d be useful to be able to access it within automations.

I’m thinking it’d be useful to determine period of times between sensor states. In my use case I want to write a script that will pull videos from my dashcam based on how long I’ve been out of the house. I can trigger the script to run when I get home, but I’d like to be able to pass a parameter for when to start. I also have a sensor that says when I’m parked, so it’d be nice to only pull videos from the driving portions of the trip.

I’m not quite sure that I understand what you want to do, so correct me if I got it wrong:

If you’ve been out of the house for 7 hours, you want to pull only 7 hours of dashcam footage? Is that so?

If that’s the case why don’t you create a datetime input and update it every time your car leaves the house. This way, when you return, you’ll have the start time from where to pull the videos off the dashcam.

Thanks for the pointer to datetime_input. I think I could put something together using datetime_input but it’s not really scalable and I’d be basically creating the history database using automations.

To explain the scalability issue, if I wanted to start making it more complicated like taking history information from other sensors, for example a parking sensor (i.e., parked or driving), I’d have to keep creating more and more datetime_inputs to store multiple possible times. For example, if I’m out of the house for 7 hours, but parked in 3 locations and only want to download the videos from driving, it starts to become unmanageable.

And while this is an example of what you could do, I imagine others would find other things you could automate.

Well, for the complex requirements you have, you’ll need a complex solution. I’d suggest you take a look at python scripts, or maybe writing your own custom component. Either of these can query the history database directly. But in short - no you can’t access history data in automations.