Custom Widget: Weather Chart (Meteogram)

weather
weatherforecast
widgetgallery
Tags: #<Tag:0x00007f0146725288> #<Tag:0x00007f0146725148> #<Tag:0x00007f0146724fe0>

(Mike) #22

Is it possible to refresh the widget every 6 hours?


(Sven) #23

Yes, I would realy like that, since we now have to subscribe to the API key with our creditcard information… The last month that I was using it I regularly saw old data and have to refresh it manually each time…


(Jochen Eggler) #24

Hi there,
first of all, thank you @drmrbrewer for meteograms.com!
I went another way without the custom widget, but with that I get “self-updating” meteograms. I designed my meteogram here:
https://api.meteograms.com
With the “save settings” button it’s easy to adjust the meteogram later. I check the result via the “direct link” and copy that link (* => see remark below). Then I use a little shell-script to download the png:

#!/bin/bash
wget -q -O /home/openhabian/meteogramm.png your-direct-link-here
# if something went wrong, write an error on the old one...
if [ -s /home/openhabian/meteogramm.png ]
then
    mv /home/openhabian/meteogramm.png /etc/openhab2/html/meteogramm.png
else
    convert /etc/openhab2/html/meteogramm.png -pointsize 24 -fill white -undercolor '#ff000080' -gravity South  -annotate +0+50 ' Update-Error ' /home/openhabian/meteo-error.png
    mv /home/openhabian/meteo-error.png /etc/openhab2/html/meteogramm.png
fi
# preparation meteogram as String-Item
rm /etc/openhab2/html/meteogramm-????-??-??-??-??-??.png
stamp=$(date +%Y-%m-%d-%H-%M-%S)
cp /etc/openhab2/html/meteogramm.png /etc/openhab2/html/meteogramm-$stamp.png
curl -s -X PUT --header "Content-Type: text/plain" --header "Accept: application/json" -d "/static/meteogramm-$stamp.png" "http://openhabianpi:8080/rest/items/MeteogrammPfad/state"

As you can see, I use a string item called MeteogrammPfad whitch gets updated every time the script runs. Then I use an image widget with the “MeteogrammPfad” item - so it gets updated immediately.
The script can be triggered via a rule or, in my case, via linux crontab.
Of course, that’s not the only way to do that - maybe you could even use the direct link for the image widget (did not test it), but with the script I see some advantages: More than one client can use the same png an I only need 1 download. And second, if there would be some bug in habpanel in the future that updates every second, I don’t get poor :wink:

(*) “copy that link” => you need the fully encoded link, without the " characters. If I right-click on “direct link”, I get that encoded link. If I open it, Chrome decodes parts of it => in the address bar I have " characters => do not copy from here, it won’t work in the script!
Alternatively, you can copy the encoded options that are shown next to where you do the settings.


Examples of HABPanel Solutions
(Jürgen Baginski) #25

High,I tried to use your script, however using my direct link raised an error.
The error was:

08:30:15.746 [WARN ] [del.core.internal.ModelRepositoryImpl] - Configuration model 'Meteogram.script' has errors, therefore ignoring it: [1,49]: missing EOF at ':'

My first line in code looks like:

wget -q -O /home/openhabian/meteogramm.png https://nodeserver.cloud3squared.com/getMeteogram......

I used the “direct Link” created on api.meteograms.com, however the error reported point directly into that link. What am I missing?


(Jochen Eggler) #26

Hmm… I think the first line looks OK, at least it looks the same in my file.
Do you get an error if you run the script from the linux shell?


(Jürgen Baginski) #27

Will try that before noon! I’ll be back.


(Jürgen Baginski) #28

Using the first line in the linux shell creates an empty file “Meteogram.png”.


(Jochen Eggler) #29

Hi Jürgen, I suggest we try to solve that via PN and return here when there is a result. OK?


(Jürgen Baginski) #30

:+1:

My “problem” was missing the Shell-script hint. I understood the posted code as a script running as a script in openHAB! Doing the same in a linux shell will work!


(Jürgen Baginski) #31

Using the script from @der_optimist works fine, however you need to copy the “Encoded Options” in order to make your URL. Using the “direct Link” made use of the " -character which is escaped in the “Encoded Options” (In dircet Link it looks in part like " … %7B"token"%3A"…" and the same part in the Encoded Options "…%7B%22token%22%3A%22…)

What does not work for me is the “else”-part (if the crrated .png is empty). I get an error stating that the method “convert” isn not found.


(Jochen Eggler) #32

Thank you for this hint, Jürgen. Looks like the server behavior has changed. When I copied the link, I got the fully encoded version via “direct link”, but now I also get the " characters.
Good to hear that it works with the Encoded Options.


(Jürgen Baginski) #33

I got it running. However I notice a yellow placard over the meteogram stating “Only 40.3 credits left!” What is that for? I checked my account, only 24 calls have been made so far!


(Mike Brewer) #34

@opus – what weather data provider are you specifying in the call? Some providers charge (me) for each call made, so there is a separate credit system to control usage of those providers from the Android app. I will consider how to handle this for direct meteogram requests.

@der_optimist – I’d not noticed that before. In fact the URL for the “direct link” uses exactly the same string encoding as for the “encoded options”, and always has done (i.e. with the quote character encoded). I think the browser may “un-encode” the quote character for readability, since a quote character is a valid character in a URL, at least in a browser if not in some script or code somewhere.


(Jürgen Baginski) #35

I’m using the german DWD, is that a problem?


(Mike Brewer) #36

No that’s not one of the providers that charges, so it shouldn’t need anything special. I probably just need to reset the credit count and the message should go away.


(Jürgen Baginski) #37

Tried to run the script triggered by a rule, failing so far!
Running it from the shell is of no problem, doing the same from a rule fails in some obscure results. The .png with the timestamp gets created, however it is the old one. Checking with Meteogramm no update has been requested. It looks to as if the first line (wget) doesn’t run, the other are running???

[Edit]
Using “var/lib/openhab2” as the directory for creating the .png is my solution. Can*t get the “convert” command to run (“Command not found”), so I’m not using this part.


(Jochen Eggler) #38

Thanks Jürgen for the hint with the " characters. I updated my original post to avoid this problem in future.
I also updated the link to https://api.meteograms.com as @drmrbrewer asked me to do. This is the link that will also work in future.
@opus, your last problem (script not working when triggered by a rule) sounds like a problem with the file permissions. When you trigger it by a rule, user “openhab” starts the script. When you start it from the linux shell, the current user starts it (e.g. “openhabian”). Maybe “openhabian” (or whatever user you use) has the permission to create a file and “openhab” doesn’t? Maybe in /var/lib/openhab2 every user has the permission to write a file and that’s why it works there.


(Jürgen Baginski) #39

I would also say it was file permissions. Now it is running smooth (only once in a while I get no new png, can’t say why ATM).


(Peter Lindberg) #40

Hi all

Maybe someone can help me regarding the widget for habpanel?

When I enter the token, long, lat, name… Nothing is showing up. its the same both in the habpanel and on meteogram api site…

{
  "schemaVersion": "1.0",
  "token": "xxxxxxxxxxxxxxxxx",
  "chartWidth": "700",
  "chartHeight": "350",
  "density": "1.5",
  "location": {
    "placeName": "Viborg",
    "longPlaceName": "8800 Viborg, Denmark",
    "latitude": "56.452",
    "longitude": "9.3963",
    "countryCode": "DK"

What im I doing wrong?


(Mike Brewer) #41

The server has has a little bit of a “moment” in the last hour or two. It should be fixed now. Is it working more as expected for you now?