This page describes one way to let PHP reference data known to WeeWX, as well as a few other hints for how to make upgrading to new versions of WeeWX simpler. It is current as of WeeWX 5.1.

How To Take Information From WeeWX and Put It Into PHP Variables

With regards to the sharing of data from WeeWX to a PHP environment, the method described is quite clumsy and inelegant. Essentially, the mechanism is as follows:
  1. The output of a Cheetah template file conversion, is 'included' via Cheetah by another template file, or by a PHP file itself (an example follows.). This conversion file (or an outer wrapper for it, depending on how you structure things) is specified in your WeeWX skin's skin.conf file so it is effectively a 'report' generated by WeeWX every five minutes, even though the report is actually PHP script.
  2. The template file uses a clumsy 'trick' to get WeeWX information put into PHP variables. The way to do this is shown by this example. Note how we interleave PHP and template 'code' to allow the transfer of information to take place. A variable in PHP-land, called $from_cheetah_month_wind_max_unclean is set to a template variable using a PHP multi-line string, delimited with the string EOS.

    This multi-line string allows the #raw and #end raw lines to be processed by Cheetah, which will convert the string $month.wind.max into a number represented in text. The PHP code won't see the #raw and #end raw lines because they will have previously been removed by the Cheetah processor. Finally, PHP will 'clean up' the text put in the PHP variable, by removing '\n' and '\r', which is performed by the function month_clean_cheetah_string.

    The conversion file, before Cheetah processing, might look like this:

    #raw
    $from_cheetah_month_wind_max_unclean = <<<EOS
    #end raw
    $month.wind.max
    #raw
    EOS;
    $from_cheetah_month_wind_max =
      month_clean_cheetah_string($from_cheetah_month_wind_max_unclean);
    #end raw
    


    The conversion file, after Cheetah processing, looks like this:

    $from_cheetah_month_wind_max_unclean = <<<EOS
    22 mph
    EOS;
    $from_cheetah_month_wind_max =
      month_clean_cheetah_string($from_cheetah_month_wind_max_unclean);
    


    A Working Example of the Clumsy Sharing of Information From WeeWX To PHP

    I needed to quickly write additional HTML for my WeeWX-based backyard weather station. I found it quicker to write what I needed to do in PHP, and realized I needed a way to both provide a few bits of WeeWX data to my PHP-based HTML page, and from that I needed to periodically regenerate that page with up-to-date WeeWX data. To me, it seemed the easiest way to do it was to leverage the way WeeWX generated web pages.

    Here's the steps I went through:

    1. WeeWX uses the Cheetah Python-based package to generate pages. Cheetah is simple enough to use, and provided the basic functionality I needed, namely the ability to 'include' other files, and also the ability to declare sections of the template file as 'raw' - meaning the Cheetah system just passed the information in the 'raw' sections as-is, with no editing. I just needed to tell WeeWX to pass the template I created to the Cheetah report generator. I did this in the Standard skin by adding this to skin.conf in the '[[ToDate]]' section of that file:
      
          [[ToDate]]
              [[[php_example]]]
                  template = php_example.php.tmpl
      
    2. php_example.php.tmpl looks like this, and is stored in the skins/Standard directory:
      <html>
      <head>
       <title>PHP EXAMPLE Test</title>
      </head>
      <body>
      <!--
        The include below helps grab WeeWX 'data' that you need to reference in
        your PHP file. The include file puts the data into PHP variables.
      -->
      #include "./private_extensions/php_example_cheetah_vars_to_php.tmpl"
      <p>Hi this would be part of a standard WeeWX HTML file, to be generated every five minutes. Pretend this is a 'standard skin file' from a WeeWX skin, extended to work with PHP.</p>
      <br>
      <p>Below, should be the extra information from PHP.</p>
      <br>
      #include "./private_extensions/php_example_extra.tmpl"
      <p>Did you see the information about the current temperature, above?</p>
      </body>
      </html>
      
      
      

    3. php_example.php.tmpl includes a pair of files, which are stored in a new subdirectory of the skins/Standard directory called private_extensions.

      The first of these files is called php_example_cheetah_vars_to_php.tmpl which shows the very clumsy, but rather efficient and working way, to put a Cheetah derived bit of information into a PHP variable that can later be referenced by a PHP script that 'includes' the Cheetah template file. Walk through it slowly if you need to see how it works. Also note that the sections between #raw and #end raw are not proceeded by Cheetah, but the rest is. This means that $variables outside of the #raw portions are converted into the values of those WeeWX variables, but inside the #raw portions, they can refer to PHP variables. It's important to keep this straight when examining these files!
      ## Note that including a file must reference its path from the top level
      ## skin directory, in this case /root/weewx-data/skins/Standard
      
      #include "./private_extensions/all_cheetah_vars_head.tmpl"
      
      #raw
      <?php
      #end raw
      
      ## ##########################################################################
      ##
      ## php_example_clean_cheetah_string ($theinputstring)
      ##
      ## Cleans up an input string
      
      #raw
      function php_example_clean_cheetah_string ($theinputstring) {
      
        return str_replace(str_split('\n\r'), '', $theinputstring);
      }
      #end raw
      
      ## ##########################################################################
      ##
      ## from_cheetah_latest_out_temperature <=
      ##  $latest('purpleair_binding').purple_humidity
      ##
      
      #raw
      $from_cheetah_latest_out_temp_unclean = <<<EOS
      #end raw
      $current.outTemp.formatted
      #raw
      EOS;
      $from_cheetah_latest_out_temp =
        php_example_clean_cheetah_string
         ($from_cheetah_latest_out_temp_unclean);
      #end raw
      
      #raw
      ?>
      #end raw
      

    4. The second of these files is php_example_extras.tmpl which is also stored in the private_extensions subdirectory within the skins/Standard directory. This second file is a short piece of PHP code which uses the variable defined by the first template file, given above.
      #raw
      <?php
      
        echo "<p>The current temperature is " .
          strval($from_cheetah_latest_out_temp) . ".</p><br>";
      
        echo "<p>Twice that value is " . strval($from_cheetah_latest_out_temp * 2) .
          ".</p><br>"
      
      ?>
      #end raw
      

    5. An example of the resulting PHP file, after having been processed by Cheetah, looks like this:
      <html>
      <head>
       <title>PHP EXAMPLE Test</title>
      </head>
      <body>
      <!--
        The include below helps grab WeeWX 'data' that you need to reference in
        your PHP file. The include file puts the data into PHP variables.
      -->
      
      
      <?php
      
      
      function php_example_clean_cheetah_string ($theinputstring) {
      
        return str_replace(str_split('\n\r'), '', $theinputstring);
      }
      
      
      $from_cheetah_latest_out_temp_unclean = <<<EOS
      71.1
      EOS;
      $from_cheetah_latest_out_temp =
        php_example_clean_cheetah_string
         ($from_cheetah_latest_out_temp_unclean);
      
      ?>
      <p>Hi this would be part of a standard WeeWX HTML file, to be generated every five minutes. Pretend this is a 'standard skin file' from a WeeWX skin, extended to work with PHP.</p>
      <br>
      <p>Below, should be the extra information from PHP.</p>
      <br>
      <?php
      
        echo "<p>The current temperature is " .
          strval($from_cheetah_latest_out_temp) . ".</p><br>";
      
        echo "<p>Twice that value is " . strval($from_cheetah_latest_out_temp * 2) .
          ".</p><br>"
      
      ?>
      <p>Did you see the information about the current temperature, above?</p>
      </body>
      </html>
      
      
      



    One Other Thing: Making Upgrades of WeeWX Easier

    Using Cheetah's #include mechanism also makes one thing much easier: Upgrading WeeWX when you've got changes you've made to a skin's HTML files. For years, I had hundreds of lines of changes to the Standard skin's index.html.tmpl file. This meant upgrades were painful and error prone, using multiple editing windows and careful copy/pasting. Then, remembering that these files were generated from templates via Cheetah, I used the #include mechanism to include the changes. Therefore, my edits to the file are kept in multiple included files stored in the private_extensions subdirectory referenced above, so my changes to the primary file are just these #include statements. For example, I add many things to the left sidebar of the 'current conditions' page of the Standard skin. I store all these in a file called current_extra_sidebar.tmpl and simply include that in the appropriate point of the index.html.tmpl file. So when I upgrade WeeWX, the new version's index.html.tmpl typically differs from my edited old one only by these #include statements I've added. The work required to upgrade WeeWX versions changes from over an hour or two to just a few minutes as a result, as the skins rarely change their basic structure.