Show Current Sun Position and Shadow of House (Generate SVG)

Looks like you changed the file and messed up the import.

This cannot be changed:

In shaddow.py:

from myopenhab import openhab

in myopenhab.py

class openhab(object):
1 Like

All working now, Looks great. Thank you do much for your help.
Yes - I did mess up the import - but only the last time! I ended up with a 0Kb myopenhab.py file. Your error checking changes seemed to fix it all.

  1. in which program can you draw your house to get coordinates? or how did you do it?
  2. can I delete the old records in the database in the script? such as Sun_Azimuth,Sunrise_Azimuth, Sunset_Azimuth.
    I have not figured out where they can be used again.
    Thank you!

The way I did it - I just cropped my house from a plan / map and scaled it down into a 100 x 100 bitmap. You can use whatever software you have. Even MS Paint will do :slight_smile:

Capture

When it’s scaled to 100x100, you can easily determine what are coordinates of each corner of your house.
Just remember to add some margin. Otherwise it would overlap with the circles.

Results are great :slight_smile:

Capture

I used Inkscape.
You can set the page to 100px x 100px, import the image of the outline of your house as, for example, a bitmap, and then use 'Path,Trace Bitmap which will copy your bitmap as a vector image overlayed over the bitmap.
You can then get the coordinates for each node (corner of house) by clicking on each node.
(You could probably view the svg as xml and see the coordinates there but I think they need some conversion factor that way).

Untitled

3 Likes

Hi Patrick,

It’s a great widget (especially if you are into gardening and want to see what parts of the garden are in shade a lot).

Just one thing - as it’s a script and not a binding, how do I turn off the logging? I tried a few things which didn’t work.
Logging such as:

 [INFO ] [lipse.smarthome.model.script.Shaddow] - Successfully got data from InfluxDB

Thanks.

If you call the script through a rule, you just omit the log call:

logInfo("Shaddow", resp)  
1 Like

I forgot to look in the rules - I was looking in the python scripts. Thank you again.

Please find attached my implementation for usage of OpenHAB persistance instead of InfluxDB:

    def queryPersistence(self, query):
        """
        Get value from persistence
        """

        try:
            r = requests.get('http://' + str(self.openhab_ip) + '/rest/persistence/items/' + query)
            if(r.status_code == 200):
                data = r.json()
                print "Successfully got data from Persistence" # \033[0;37m" + str(query) + "\033[0m"
                return data
            else:
                print "Error getting data from Persistence: \033[0;31m" + str(query) + " (HTTP Response " + str(r.status_code) + ")\033[0m"
            return ""
        except:
            print "Error getting data from Persistence: \033[0;31m" + str(query) + " (" + str(sys.exc_info()[1]) + ")\033[0m"

    def getStateHistoryFromPersistence(self, item, datetime, datetime2):
        """
        Get value from persistence
        """

        try:
            query = item + '?starttime=' + datetime + '&endtime=' + datetime2 + '&boundary=false'
            data = self.queryPersistence(query)
            value = getJSONValue(data,['data',0,'state'])
            print "" + str(value)
            if (value == None):
                print "No results returned from Persistence History: \033[0;31m" + str(data) + "\033[0m"
            return value
        except:
            print "Error getting data from Persistence: \033[0;31m" + str(query) + " (" + str(sys.exc_info()[1]) + ")\033[0m"

… called by a modified shadow-class:

class shaddow(object):
    """
    
    Shaddow Object
    """
    def __init__(self):

        self.debug = False 
        self.oh = openhab()
        self.azimuth = float(self.oh.getState('Azimuth'))
        self.elevation = float(self.oh.getState('Elevation'))
        self.sr = self.oh.getState('SonnenaufgangAzimuth')
        self.ss = self.oh.getState('SonnenuntergangAzimuth')
        self.sunrise_azimuth = float(self.sr) if self.sr != 'NULL' else 0
        self.sunset_azimuth = float(self.ss) if self.ss != 'NULL' else 0
        ts = time.time()
        utc_offset = (datetime.fromtimestamp(ts) - datetime.utcfromtimestamp(ts)).total_seconds()/3600
        for h in xrange(0,24,HOURS):
            t = datetime.combine(date.today(), datetime.min.time()) + timedelta(hours=-utc_offset+h-24)
            tt = datetime.combine(date.today(), datetime.min.time()) + timedelta(hours=-utc_offset+h-23)
       	    a = self.oh.getStateHistoryFromPersistence('Azimuth',t.strftime('%Y-%m-%dT%H:%M:%S') + 'Z',tt.strftime('%Y-%m-%dT%H:%M:%S') + 'Z')
       	    if (a == None): a = 0
       	    DEGS.extend([a])
[...]
4 Likes

thank you, guys! taught! google earth + Inkscape = result

This looks awesome, but I have yet to experiment in depth with HABPanel, but can this be used in the basic UI to point to a refreshing image?

1 Like

Would you mind sharing your dashboard .json file? I’ve been trying to add to mine and I just can’t figure out how to get it to look right

2 Likes

great widget, very usefull. I will try it asap

Hi Patrick

The widget runs smoothly on one of my Ubuntu machines. Thank you very much.
On the other “identical” Ubuntu machine I receive the following error:
–> No results returned from InfluxDB History:

Log File:

2017-10-29 15:25:00.483 [INFO ] [lipse.smarthome.model.script.Shaddow] - Updating Shaddow SVG
2017-10-29 15:25:00.483 [INFO ] [lipse.smarthome.model.script.Shaddow] - Successfully got state from OpenHab: Sun_Azimuth
Successfully got state from OpenHab: Sun_Elevation
Successfully got state from OpenHab: Sunrise_Azimuth
Successfully got state from OpenHab: Sunset_Azimuth
Error getting data from InfluxDB: SELECT FIRST(*) FROM "Sun_Azimuth" where time>'2017-10-27T23:00:00Z' (HTTP Response 401)
No results returned from InfluxDB History: 

This behaviour is strange beacuse data is definitely in the database. If I query influx directly I’ll receive data for this select statement:

Connected to http://localhost:8086 version 1.3.7
InfluxDB shell version: 1.3.7
> USE openhab_db
Using database openhab_db
> SELECT FIRST(*) FROM "Sun_Azimuth" where time>'2017-10-28T22:00:00Z'
name: Sun_Azimuth
time                first_value
----                -----------
1509228000015000000 327.73

Do you have any clue why I receive this error message?

1 Like

This http code stands for “Unauthorized”, thats why you didnt receive any results.

thank you!

Hmm, in this case the Python script is not authorized to access Influx DB? How can this be solved?

I am not using this stuff here, but a first look into the script: looks like it is not using authentication.

Therefore it seems like your influxdb is configured to not allow an unsecure communication. At least that is my conception.

Thanks for all the help!

I initially setup Influx based on the below manual:
https://community.openhab.org/t/influxdb-grafana-persistence-and-graphing/13761

There it’s recommended to set auth-enabled = true. The script started to work immediately after setting the parameter to false.

1 Like

This is really cool, I like it a lot. I want to change it to also add the position of the moon and the direction where the wind is coming from, the moon also indicating the current phase and the wind indicating the strength in beaufour. I was able to “include” svg files for sun, moon and wind residing in the same html directory which already gives the following:

(this is a testing version thus some persisted data is missing)

However I’d prefer to “include” icons from the icons directory instead, what path do i need to use for these ? Furthermore I’d like to use a different iconset (https://github.com/erikflowers/weather-icons) which is not colored. When I use these the icons show up as “black on black underground”, thus invisible. How can I change the color of these “included svg files” ?

the corresponding lines of the created shaddow.svg look like this:

<image x="0.852620357054" y="68.4735781393" width="10" height="10" xlink:href="wind.svg" /> 
<image x="59.9686933371" y="92.7067942727" width="10" height="10" xlink:href="moon.svg" /> 
<image x="67.2083062338" y="89.7972223941" width="10" height="10" xlink:href="sun.svg" />

I will upload the modified script (using the REST interface, not InfluxDB) once I have this working.

Thx, Max

4 Likes

Hi Patrick,

awesome. Looks great. Can you share the widget for the other views as well. Looks like you created some for the energy costs, energy consumption views as well.

Regards
Ralf