Weather Station Data

I have an AmbientWeather WS-2902C which is sending data to weewx running on a RPI 4B 8Gg Ram running on a 1TB SSD. I have Home Assistant Supervised running on the same RPI, and I am also using the AmbientWeather integration in HA. As I wanted to do alot more with the weather station such as sending all the raw data to weather web sites all over the world (AWEKAS example below), I am also running weewx on that same RPI and it is listening to the console data being sent to ambientweather utilizing the “customized” AWNET connection feeding to weewx. To track the continuous success or failure or REST api calls made by weewx to the weather web sites, I have changed the weewx.log file location to:

/usr/share/hassio/share/weewx.log

  • which then makes that log file visible within the HA container for tailing with GREP - and I have command line sensors with pretty complex logic to track the weewx transmissions as they take place (to numerous weather web sites as shown below) so that I can track the status of the those transmissions. Here is what the lovelace card for that looks like (“Cnsl” is the AmbientWeather console):

For example, the logic for the AWEKAS (although I am in NJ this web site for my weather station is in Germany LOL) and their website if my favorite, graphically:

anyway, the AWEKAS command-line sensor coniguration.yaml is:

  - platform: command_line
    name: WEEWX_LAST1_SENT_TO_AWEKAS
    command: "grep 'AWEKAS: Published' /share/weewx.log | tail -1 | cut -c-255"
    scan_interval: 30
    command_timeout: 5
    value_template: >
      {% set data = value | regex_findall('\(([0-9]+)\)') | first | int | as_datetime | as_local %}
      {% set ts = data.year ~ ' ' ~ (value | regex_findall('(.+)kruse-pi weewx') | first).strip() %}
      {% set time = strptime(ts, '%Y %b %d %H:%M:%S', data) | as_local %}
      {% set str = time.strftime('%a %-m/%-d %-I:%M:%S%p') ~ ' (5 min)' %}
      {% set indx = str.find("M (") %}
      {% set oldstr = str[indx-1:indx+1] %}
      {{ str.replace(oldstr,oldstr.lower()) }}
    unique_id: WEEWX_LAST1_SENT_TO_AWEKAS

And the most nightmarish one is “Last Weewx Error:” which has to look at the logic for all of the other sensors to see when they each last had an error, and then report on the most recent one - IF it is still in an error state (and this was a nightmare to figure out):

  - platform: command_line
    name: WEEWX_LAST1_REST_CALL_FAILURE
    command: "grep 'ERROR weewx.restx:' /share/weewx.log | tail -1 | cut -c-255"
    scan_interval: 60
    command_timeout: 5
    value_template: >
      {% set data = value | regex_findall('\(([0-9]+)\)') | first | int | as_datetime | as_local %}
      {% set errortime = strptime(now().year ~ ' ' ~ ' '.join(value.split(' ')[0:3]),'%Y %b %d %H:%M:%S',data) | as_local %}
      {% set errordestination = (((value | regex_findall('(.+): Failed to publish') | first).strip() | regex_findall(': (\w+)') | first).strip()).lower() %}
      {% set sensortoretry = 'sensor.weewx_last1_sent_to_' + errordestination %}
      {% set thisyear = now().year %}
      {% set statessensorretry = states(sensortoretry) %}
      {% set joiner1 = ' '.join(states(sensortoretry).split(' ')[0:3]) %}
      {% set joiner2 = now().year ~ ' ' ~ joiner1 %}
      {% set sensorretriedtime = strptime(joiner2,'%Y %a %m/%d %I:%M:%S%p',data) | as_local %}
      {% if (sensorretriedtime > errortime) %}
      {{ 'No current errors' }}
      {% else %}
      {% set ts = data.year ~ ' ' ~ (value | regex_findall('(.+)kruse-pi') | first).strip() %}
      {% set time = strptime(ts, '%Y %b %d %H:%M:%S', data) | as_local %}
      {{ time.strftime('%a %-m/%-d %-I:%M:%S%p') }}, {{ (value|regex_replace(find='EDT \(([0-9]+)\):', replace='EDT:', ignorecase=False)).split("weewx.restx: ",1)[1] }}
      {% endif %}
    unique_id: WEEWX_LAST1_REST_CALL_FAILURE

Anyway, here is my question. Although I am using an SSD, I would much rather reduce all the ‘log scraping’ (which is really amatueurish (sp?) in my opinion!) and instead have MQTT messages for each of those transmissions as they take place, sent from weewx to home assistant instead. I will also eventually need to do this instead of the log scraping as I add more functionality (video monitoring etc) to Homa Assistant the RPI at that point won’t be enough hardware (and I would use the RPI only for weewx then) - so such MQTT messages would need to be sent over the network from separate weewx & HA machines. So how do I get the MQTT functionality working from weewx to HA to feed the same data into those what are currently command line sensors - just the information about the restx transmissions - without scraping the log from within weewx to send the message across?

1 Like

If I understand your question, first you need to add MQTT extension to weewx.

Then add your Mqtt sensors into home assistant

  # Temperatures
  - name: "VP2 Outside Temperature"
    state_topic: "weewx/outTemp_C"
    unit_of_measurement: "°C"
    value_template: "{{ (value | round(2) | float) }}"

  - name: "VP2 Inside Temperature"
    state_topic: "weewx/inTemp_C"
    unit_of_measurement: "°C"
    value_template: "{{ (value | round(2) | float) }}"

  # Humidity
  - name: "VP2 Outside Humidity"
    state_topic: "weewx/outHumidity"
    unit_of_measurement: "%"
    value_template: "{{ (value | round(0) | float) }}"

  - name: "VP2 Inside Humidity"
    state_topic: "weewx/inHumidity"
    unit_of_measurement: "%"
    value_template: "{{ (value | round(0) | float) }}"

  # Winds
  - name: "VP2 Wind Direction"
    state_topic: "weewx/windDir"
    unit_of_measurement: "°"
    value_template: "{{ (value | round(1) | float) }}"

  - name: "VP2 Wind Gust Direction"
    state_topic: "weewx/windGustDir"
    unit_of_measurement: "°"
    value_template: "{{ (value | round(1) | float) }}"

  # Winds
  - name: "VP2 Wind Gust"
    state_topic: "weewx/windGust_kph"
    unit_of_measurement: "KPH"
    value_template: "{{ (value | round(1) | float) }}"

  - name: "VP2 Wind Speed"
    state_topic: "weewx/windSpeed_kph"
    unit_of_measurement: "KPH"
    value_template: "{{ (value | round(0) | float) }}"

Hope that helps.

1 Like

Thank you @atomic10, I also read through the link… However the topics in your example on the HA side as well as the weewx data are only sending and receiving weather station measurements themselves (which I already have in HA from the AmbientWeather integration)…

FYI an example of the weewx.log entry for the results of sending data, (to AWEKAS as that is our example in the original post, looks like this):

Apr 25 09:45:24 kruse-pi weewx[513] INFO weewx.restx: AWEKAS: Published record 2023-04-25 09:45:00 EDT (1682430300)

So - is it possible to just publish once (with success results) for each successful or unsuccessful data transmission attempt from weewx to weather websites - reporting that to Home Assistant in a smilar manner to what is shown in the lovelace card screen snapshot in my earlier post?

I dont believe weewx natively records that via mqtt, it just publishes weather data.
You could write yet another log scraper to look for those errors, and publish something via MQTT outside of HA, but that doesnt add a heap of value and is still scraping logs.

Thank you I thought as much. I figured out where to make changes in the WEEWX source code to grab this information to publish, but never went any further down that rabbit hole because it would be overwritten the next time a new version comes out!

You could always submit a PR for your change

Works perfectly. Thank you

Hey guys,
for who might be interested (@mtaliesin), I did post a working weewx config some time ago:

Also, please pay attention to the note regarding packages further down. @mtaliesin your example with all definitions in one file is not ideal…

1 Like