How to use result/output of python scripts in home assistant --> grafana

Hello All.

I’m using homeassistant now for about a year i guess and Im very happy with the possibilities.
However, as I ad more components and build more automations it’s getting trickier and more complicated to get what I need with the basic coding I can comprehend.
For now I installed along home assistant on my pi3 also an influxdb and grafana and all is working well.

Now there is an plugin (worldmap) that needs geohashes to show on the map. In home assistant I have owntracks and other device trackers who report in GPS coordinates.

There is a python script that converts gps coordinates to geohash :

def encode(latitude, longitude, precision=12):
    """
    Encode a position given in float arguments latitude, longitude to
    a geohash which will have the character count precision.
    """
    lat_interval, lon_interval = (-90.0, 90.0), (-180.0, 180.0)
    geohash = []
    bits = [ 16, 8, 4, 2, 1 ]
    bit = 0
    ch = 0
    even = True
    while len(geohash) < precision:
        if even:
            mid = (lon_interval[0] + lon_interval[1]) / 2
            if longitude > mid:
                ch |= bits[bit]
                lon_interval = (mid, lon_interval[1])
            else:
                lon_interval = (lon_interval[0], mid)
        else:
            mid = (lat_interval[0] + lat_interval[1]) / 2
            if latitude > mid:
                ch |= bits[bit]
                lat_interval = (mid, lat_interval[1])
            else:
                lat_interval = (lat_interval[0], mid)
        even = not even
        if bit < 4:
            bit += 1
        else:
            geohash += __base32[ch]
            bit = 0
            ch = 0
    return ''.join(geohash)

I put this is my /python_scripts folder… but then what? How do I “paste” the coordinates from any of the device trackers into my python script, then run it, and then getiing the output back into a variable (geohash in string) into HA so I can use it in grafana. I’ve searched a lot but cannot find the solution.

I tried to search via python, custom components… no luck. appdaemon for me is to far fetched to get started with that right now if that could be the solution

many thanks in advance,

cheers

Happy to inform myself :sunglasses: and everyone interested that I finally managed to encode GPS coordinates from latitude en longitude to geohash.
I started to use appdaemon (started with helloworld app) and after a lot of trial and error finally put it together.

Here is the code I used:

"""
Copyright (C) 2008 Leonard Norrgard <[email protected]>
Copyright (C) 2015 Leonard Norrgard <[email protected]>
This file is part of Geohash.
Geohash is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Geohash is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
License for more details.
You should have received a copy of the GNU Affero General Public
License along with Geohash.  If not, see
<http://www.gnu.org/licenses/>.
"""

import appdaemon.plugins.hass.hassapi as hass
import math

#
# Hellow World App
#
# Args:
#

class Hellogeo(hass.Hass):

  def initialize(self): 
     self.listen_state(self.GHA,"sensor.power_consumption_now") 

#just there to initiate a change (wil change for other sensor, e.g. locationtracker)
 

  def GHA (self, entity, attribute, old, new, kwargs):
     
     __base32 = '0123456789bcdefghjkmnpqrstuvwxyz'
     __decodemap = { }
     for i in range(len(__base32)):
      __decodemap[__base32[i]] = i
     del i
     longitude = self.entities.device_tracker.XXX_location_tracker.attributes.longitude
     latitude = self.entities.device_tracker.XXX_location_tracker.attributes.latitude
     precision = 12
     lat_interval, lon_interval = (-90.0, 90.0), (-180.0, 180.0)
     geohash = []
     bits = [ 16, 8, 4, 2, 1 ]
     bit = 0
     ch = 0
     even = True
     while len(geohash) < precision:
         if even:
             mid = (lon_interval[0] + lon_interval[1]) / 2
             if longitude > mid:
                 ch |= bits[bit]
                 lon_interval = (mid, lon_interval[1])
             else:
                 lon_interval = (lon_interval[0], mid)
         else:
             mid = (lat_interval[0] + lat_interval[1]) / 2
             if latitude > mid:
                 ch |= bits[bit]
                 lat_interval = (mid, lat_interval[1])
             else:
                 lat_interval = (lat_interval[0], mid)
         even = not even
         if bit < 4:
             bit += 1
         else:
             geohash += __base32[ch]
             bit = 0
             ch = 0
#     """" return ''.join(geohash) """"
     statelax = ''.join(geohash)
     self.log(statelax)

Now statlax is containing the geohash format and I can use it in Grafana.

One happy… :sunglasses:

1 Like