legrego / homeassistant-elasticsearch

Publish Home-Assistant events to Elasticsearch
https://legrego.github.io/homeassistant-elasticsearch/
MIT License
145 stars 38 forks source link

Add queries as sensors #76

Open chdao opened 4 years ago

chdao commented 4 years ago

Is your feature request related to a problem? Please describe. I'd like to be able to make queries and return the result to HomeAssistant, this would be useful for geofencing, for example, as I send all my data to Elastic, but not all data flows through HomeAssistant.

Describe the solution you'd like A plugin to create queries into ElasticSearch as sensors in HomeAssistant.

Additional context

legrego commented 4 years ago

Hey @wickedsun, thanks for opening this issue.

This is an interesting idea. How do you envision this working with a query that returns multiple hits, or multiple buckets in an aggregation?

For queries that return a single hit, I could see something like this working:

elastic:
   query_sensors:
     - name: sensor_1
     query: '{"query": { "terms": {...} }}'
     field: 'my.document.field'

Where my.document.field represents a field in the document returned by the query

chdao commented 4 years ago

How do you envision this working with a query that returns multiple hits, or multiple buckets in an aggregation?

That's a good question. My use case may be very specific. My thought was using geolocation from ElasticSearch to do geofencing since I have stuff going straight to Elastic. Then I thought "hey wait that'd be nice to do geofencing with", and short of rewriting the script to send to MQTT as well as ES, I don't think I can do it.

My guess would be that people who use Elastic for HASS would also like to be able to do this as usually you're going to be putting a lot more data into it other than HASS with no way of tying it into HASS.

For queries that return a single hit, I could see something like this working:

I'd also add a time constraint so that you can do last (non-empty) value only or X minutes average. I can see a scenario where someone might want to turn off the AC if the average over the last 15minutes is lower than X rather that the absolute current value, for example. Obviously this would need buckets/aggregation.

Honestly for what I want to do, last geolocation values would do the trick, but the possibilities with this would be pretty awesome since ES is very powerful.

legrego commented 4 years ago

I'd also add a time constraint so that you can do last (non-empty) value only or X minutes average.

Is this something that you could solve with Elasticsearch's date math, or am I mis-understanding?

Honestly for what I want to do, last geolocation values would do the trick, but the possibilities with this would be pretty awesome since ES is very powerful.

Maybe we just take the top hit (hits.hits[0]) as the sensor value, and log a warning if more than one hit is returned? Alternatively, the component could enforce a size: 1 for document queries. A similar constraint could be placed for aggregations, but I haven't given that a ton of thought yet.

chdao commented 4 years ago

My knowledge of ES is pretty bare, but I'll try answering.

It does look like date math would solve that part as you could do "now-X".

Let me explain clearly what I'm trying to do here. I'm getting readings from my car's location every ~4 seconds and this is dumped into ES. I wanted to use that for geofencing since... I already have it so why go through the trouble of setting up something on my phone like OwnTrack when I can just track my car.

I saw that there was a SQL sensor and figured there must be a ES sensor too, then. To my surprise there wasn't, hence this ticket.

legrego commented 4 years ago

I was also surprised to find there wasn't an ES component, hence this one 😄. I started this one with the opposite problem though -- getting data from HASS into ES, rather than from ES to HASS. That being said, I don't see why this can't accomplish both.

I have a couple of other enhancements I'd like to work on before tackling this, so I likely won't get to this right away. PRs are welcome if you're handy with Python. I'm not a strong Python dev by any means...this was my first and only real project with the language.

chdao commented 4 years ago

This would probably be a good way for me to learn both Python and guts of ES, so I'd love to give it a go but don't take this as a "cool someone else is doing it", if I thought I had the capacity to currently do this, I wouldn't have created this ticket in the first place ;)

This is clearly not urgent and much more of a nice to have, but I'm not a huge fan of the pull method and I would rather stream my data to HASS than to read from a database.

strawgate commented 2 months ago

I think this might be best served by a separate device-type integration where you can add "devices" which are just elasticsearch queries