Dark Sky - Yawb4oH2 (Yet another weather binding for openHAB 2)

Here is a link to our developer documentation and you can find a simple PR which adds some new channels to a binding:

I recently migrated to 2.5M5 and thought I would give the DarkSky binding a try. I am currently using HTTP binding and JSON parsing to gather weather data by accessing the DarkSky API. However the HTTP binding is 1.x so it may be deprecated in OpenHAB 3.

My question is simple: How do I get the DarkSky binding to return weather data in Imperial not SI units? I’m sure I missed this, but I spent all afternoon looking for the answer and came up blank. BTW I’m using PaperUI for Thing setup, discovery and item paring to channels, and an .items file for the actual items.

Thanks for your help.

Hi John,

There is no specific configuration for the binding to return Imperial units. You have to configure it globally in your Regional Settings. In Paper UI: Configuration -> System -> Regional Settings: Click on “Show more”.

1 Like

Thanks Christoph,

Your help is much appreciated. That is a setting that I had not given any attention to. I also uncovered another issue that was causing me a problem. In my items I had not used any dimensions (Temperature, Dimensionless, etc) so the values being returned were apparently SI. When I added dimensions, then the Imperial values were returned but these values are not pure numbers but also include units as part of the string. As a result I had to recode items in the HabPanel to strip out the dimensions and display only the numbers, otherwise it would be treated as a Null. This is just an FYI for anyone else encountering such issues in the future. If there is a better way to handle this please let me know, as I am still learning all of these different coding languages, mostly by example.

<h4 class="text-left">Currently {{(itemValue('vWeather_TempF').split(' ')[0] | number: 0 || 0)}} °F</h4>

Update: with some minor recoding and refactoring I was able to get everything to work and fully migrated away from HTTP scraping to a full implementation of the DarkSky binding. Thanks for your help and the effort you put into making this binding. Great work!

Hi @cweitkamp I am wondering if there is some way to use this icon-id to set the icon on an item in a BasicUI sitemap?

Hi Andrew,

I am pretty sure you can use this guide for DarkSky too:

It’s actually a tad different in DaskSky for some reason I don’t understand. In OWM, the icon Channel gives you a link to the icon so the rule at that post just needs to wget the file and convert it from gif to png.

DarkSky, on the other hand, populates the icon Channel with the actual base64 encoded png file. So to do the same for DarkSky I set up a pipe command to strip the first 22 characters or so from the base 64 (it’s a mine type) and decode the result into a png file.

My rule, which shows both approaches, is as follows:


from core.rules import rule
from core.triggers import when
from configuration import weather_icon_path              import subprocess
from javax.imageio import ImageIO                        from java.io import File                                 @rule("Weather Icon",
      description="Copy the current weather conditions icon",
      tags=["weather"])
@when("Item vWeather_Conditions_Icon changed")           @when("System started")                                  def cond_icon(event):
    """
    Download the weather conditions icon from DarkSky and convert it from gif to
    png.                                                     """
    # OpenWeatherMap                                     #    cond_icon.log.info("Fetching weather conditions icon to {} from {}"
#                  .format(weather_icon_path, items["vWeather_Conditions_Icon"]))                                 #
#    results = subprocess.check_output(['/usr/bin/wget', '-q', '-O',
#                                       weather_icon_path,
#                                       str(items["vWeather_Conditions_Icon"])])
#
#    input_file  = File(weather_icon_path)
#    if not input_file.exists():
#        cond_icon.log.warn("Failed to fetch the weather icon!")
#        return
#
#    output_file = File(weather_icon_path.replace('gif', 'png'))
#    ImageIO.write( ImageIO.read(input_file), 'png', output_file)
#
#    results = subprocess.check_output(['rm', weather_icon_path])

    # DarkSky
    cond_icon.log.info("Fetching the weather conditions icon... {}".format(ir.getItem("vWeather_Conditions_Icon").state))
    dl = subprocess.Popen(['/usr/bin/wget', '-qO-',
                  'http://argus:8080/rest/items/vWeather_Conditions_Icon/state'],
                  stdout=subprocess.PIPE)
    dd = subprocess.Popen(['/bin/dd', 'bs=22', 'skip=1'], stdin=dl.stdout, stdout=subprocess.PIPE)
    dl.wait()
    f = open(weather_icon_path, "w")
    subprocess.call(['/usr/bin/base64', '-d'], stdout=f, stdin=dd.stdout)
    dd.wait()

Note, it’s in Python using Scripted Automation and it uses subprocess rather than executeCommandLine. It works for me so far but ymmv. I plan on submitting something a little more robust to the Helper libraries sometime soon.

Thank you to both @cweitkamp and @rlkoshak for your very helpful advice; based on your inputs, I developed my definitive solution as follows…

From my .items file…

String Local_Weather_Current_Condition "Synopsis [%s]" <g24weather> {channel="darksky:weather-and-forecast:g24:local:current#condition"}
String Local_Weather_Current_Icon_Id {channel="darksky:weather-and-forecast:g24:local:current#icon-id"}

From my .rules file…

rule "Download Weather Icon"
when
    System started or
    Item Local_Weather_Current_Icon_Id changed
then
    val icon = "/etc/openhab2/icons/classic/g24weather.png" 
    val path = "https://darksky.net/images/weather-icons/"
    executeCommandLine(String.format("wget -O %s %s%s.png", icon, path, Local_Weather_Current_Icon_Id.state.toString), 5000)
end

Note: that DarkSky supplies theses images openly in .png format from their user website, so the download is taken from that site, and not from their API site

1 Like

Hi @cweitkamp and @rlkoshak, it seems that I spoke too soon – it appears that the system (BasicUI or the Item itself) is caching the icon, so although my rule is downloading new weather images in a timely manner, and saving the new weather icon as “/etc/openhab2/icons/classic/g24weather.png”, nevertheless the BasicUI web page display often shows an older version of the weather icon.

Note: I tried adding a “touch” command to the rule to update the icon file’s timestamp, but this makes no difference…

=> Is there some way to force BasicUI (or the Item itself) to refresh it’s icon image cache??

PS I wonder if the problem is because the Current_Icon-Id Item changes its value (e.g. from “clear-day” to “clear-night”) thus triggering the Icon-Id rule to download the respective image, whereas the Current_Condition Item did NOT change its value (e.g. from “Clear” to “Clear”) so that the Current_Condition Item is not aware that it needs to reload its icon. Or something like that :slight_smile:

So I did wonder if adding something like this to the rule would force a refresh of the Current_Condition Item’s text and thus force a refresh of its icon too. But perhaps you can suggest a neater way to do that?

..
    val temp = Local_Weather_Current_Condition.state.toString
    Local_Weather_Current_Condition.sendCommand("..")
    Local_Weather_Current_Condition.sendCommand(temp)
..

I’ve never looked that closely at the icon changing. All I can suggest is to try it out and see if it’s makes a difference.

I am currently testing it; but it’s going to take a long time because occurrences where Icon-Id does change and Condition doesn’t (e.g. “clear-day” plus “Clear” => “clear-night” plus “Clear”) will be rather few and far between :slight_smile:

I added the binding to OH2 with the PaperUI and i get a ton of values, which are great, but i would only like to use a few. When i go to Things in OH2 and select the binding i see no channels at all (not even after a while) I am used to deselect all i do not need in the channels and then add the ones i want through .items so i can use these in my sitemap as well.

As you can see i’m pretty new to this. How can i add only the 5 or so channels i want from Darksky and make sure i dont get the other 100 channels?

Review openHAB concepts. The Thing will have whatever predefined channels.
You usually make an Item linked to a particular Thing Channel you wish to read or control.
Some “event” type channels can be referenced directly.

You don’t. A Thing will present all the available Channels. To pick and choose what Channels you actually use, you need to create and link Items to those Channels. If you have Simple Mode turned on, you have no control over this at all. All Channels will have Items created for them automatically.

1 Like

I keep forgetting about that. :frowning:

I have simple mode turned on. But as an example; A shelly device has some channels which i can enable / disable and thus manage which ones i want to actively use. DarkSky binding does not work the same way i guess. So all the channels show up in paperUI.

When i add a few “channels” to the items file, will it only show those items or will it still show all. If so, i guess i have to manually add a Thing? If i manually add a thing, will i need to do this for all things? or can you work with simple mode and manual mode at the same time for different things?

First of all, PaperUI is for administration only. It really shouldn’t matter what get’s shown there. It’s not intended for day to day use. Sitemaps and/or HABPanel are for that.

The control tab in PaperUI will show all Channels that have any Item linked to them. So if you don’t want to see a Channel listed, turn off Simple Mode, delete all those Items that were created, and only create and link Items to those Channels you care about.

Simple Mode is the absolute worst feature of OH. Never use it.

2 Likes

Has anyone tried using PrecipitationIntensity? I set this up as an item, it has dimensions of Speed, and reports in km/h, which gives results something like 1.565543434344444E-7, which is pretty hard to display. The actual value is in mm/h, so there is a factor of a million between the actual value and the displayed value.

Is there a way of getting a Speed quantity to display in mm/h and not km/h?

I’m on 2.5.1-2.

Ok, not to worry, figured out how to change unit of measure to mm/h. Just put the unit after the format string, just like in the examples given in the binding documentation, now that I pay attention.

I was converting existing items from the old weather1 binding, and didn’t notice the format string was in the old format.

In respect to the alerts, has anyone been successful in making this work? I enabled DarkSky several weeks ago with the alerts items created as per the readme. We just had a string of tornados come through here last week with several different alerts and nothing ever populated.

EDIT: As a test to make sure the query actually should pull alerts, I pulled down the most recent forecast from DarkSky via web since we have a few alerts currently. The JSON reply shows the 4 current alerts. I’m wondering if there is an issue with the JSON not being parsed properly.

EDIT2: Found the issue. Documentation (https://www.openhab.org/addons/bindings/darksky/) identfies the item configuration as darksky:weather-and-forecast:api:local:alert1#title. The actual configuration is darksky:weather-and-forecast:api:local:alerts1#title (Note the missing S at the end of alert). This corrects the issue. Please update documentation to reflect.