Re: [weewx-user] How to retrieve Davis forecastRule as a text string into my webpage?

179 views
Skip to first unread message

Thomas Keffer

unread,
Nov 14, 2019, 7:25:55 PM11/14/19
to weewx-user
You could do it in the Cheetah template. Something like this (NOT TESTED):

#set rules = ["Mostly clear and cooler.", "Mostly clear with little temperature change.", "Mostly clear for 12 hours with little temperature change.", ... , ]

<tr>
    <td>The forecast is: </td>
    <td>$rules[$current.forecastRule.raw]</td>
<tr>

-tk

On Thu, Nov 14, 2019 at 1:39 PM PJD <peter.d...@gmail.com> wrote:
I'm trying to write code that will take Davis's forecastRule (a number) and display this on my webpage as text by reading the number off an array and returning the text corresponding to that number.

For example if the Davis forecastRule returned is 5, I want the webpage to display "Partly cloudy and cooler" which is the corresponding forecast in text form.

I'm not sure on the best way to do this but am trying to do it with Javascript with instructions to pull the text from an external text file (set up as a JS array) containing the nearly 200 text strings.

I'm getting bogged down with the syntax and would appreciate any help.  Should I be doing it a different way ie not using JS?

I don't know how to declare the variable if the array lives in an external file.

Thanks for any help!

PJD

--
You received this message because you are subscribed to the Google Groups "weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to weewx-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/weewx-user/f562b9c5-c551-45b2-a9a4-25848d847aad%40googlegroups.com.

Graham Eddy

unread,
Nov 14, 2019, 8:56:04 PM11/14/19
to weewx...@googlegroups.com
snippets from my gauges-lite.js code:


// forecast descriptions associated with forecastRule.
// one forecast desc per line, the first line corresponds to forecastRule 0
var FORECASTRULES_FILE = "forecastrules.txt”;
… … 


var Forecast_list = null;            // table of forecast strings

function start_gauges_updating() {

    // get and remember the forecast descriptions
    if (! Forecast_list) {
        var text = loadSmallTextFile(FORECASTRULES_FILE);
        if (text) {
            Forecast_list = text.split('\n');
        } else {
            console.error(FORECASTRULES_FILE + ": no forecasts provided");
            Forecast_list = [];
        }
    }
    … … 

    // remember the forecast element, updated for each forecast update.
    // usually populated by a description from forecast_list, major errors
    // are also flagged here
    forecast_el = document.getElementById("gauges_forecast"); // NOT var
    … … 
}
… … 


    // update forecast
    var forecast;
    var fcrule = observ_list['forecast’]; // <<<< however you get your fcrule value                                   
    if (fcrule) {
        forecast = Forecast_list[fcrule];
    }
    if (forecast) {
        forecast_el.textContent = forecast;
    } else {
        forecast_el.textContent = "Forecast not available";
    }
    … …


// obtain contents of a small file.
// uses synchronous GET so file should be quite small
//
function loadSmallTextFile(file_name) {

    var xhr = new XMLHttpRequest();
    xhr.overrideMimeType('application/json');
    xhr.onerror = function() {
        console.error(“loadSmallTextFile: " + xhr.statusText);
    };
    var d = new Date();
    var fnam = file_name + "?v=" + d.getTime();  // defeat cache
    xhr.open('GET', fnam, false);  // synchronous
    xhr.send(null);
    var file_text;  // defaults to undefined
    if (xhr.readyState == 4 && (xhr.status == 0 || xhr.status == 200)) {
        file_text = xhr.responseText;
    }
    return file_text;
}


where ‘gauges_forecast’ is an element in your html file

copy of ‘forecastrules.txt’ attached. note that this is for southern hemisphere - for northern you would need to change all references from NW to SW etc
forecastrules.txt

PJD

unread,
Nov 15, 2019, 10:51:01 PM11/15/19
to weewx-user
Thanks for your replies.  I managed to get a good result just using HTML and Javascript, without having to interfere with any conf files or with any of the Python code.  This is what worked:

  1. Capturing $current.forecastRule.raw with index.html.tmpl which passed that value as an integer to index.html at the time of archive generation
  2. Placing all the relevant variables and strings into an external Javascript file in the same html directory
  3. Getting the syntax right to ensure that the forecast integer was used by the script to place the right text on the page

Here's the relevant bit of forecast_text.js as in (2) above:

var myElement = document.getElementById("code");
var forecast = [
"Mostly clear and cooler",
"Mostly clear with little temperature change",
"Mostly clear for 12 hours with little temperature change",
...
];
document.getElementById("code").innerHTML = forecast[myElement.innerHTML];

and here's the relevant bit of html as in (3) above:

<p>$current.forecastRule.raw</p>
<p id="code">$current.forecastRule.raw</p>
<script type="text/javascript" src="forecast_text.js">
</script>

Happy to share the full files if anyone's interested.  Result (currently) visible at http://www.pjd.com.au/weewx/dashboard/ (though I'm still displaying the integer code as well as the text till I am sure it's all working normally).

PJD

Hartmut Schweidler

unread,
Nov 16, 2019, 7:24:47 AM11/16/19
to weewx-user

Hallo

I added the "forecastRule" field to the database "wview.py"


also in /weewx/bin/user/vanfortex.py

in the "skin.conf" by  "search_list_extensions = user.xstats.ExtStats, ...user.vanfortex.vantageText...
added

Sorry my translation in german

Hartmut
Reply all
Reply to author
Forward
0 new messages