MQTT change GPS coordinates' units from None to compass_degrees or " °"

126 views
Skip to first unread message

Stefan Gliessmann

unread,
Aug 21, 2024, 9:46:28 AM8/21/24
to weewx-user
A GPS device in my RV sends the current coordinates via MQTT to my weewx server.
The coordinates are displayed with units = None.
Trying to change them to units = degree_compass or units = " °" results in the below error:

Aug 21 15:28:26 rvwx weewx[1590] DEBUG weewx.units: Unable to convert from None to  °
Aug 21 15:28:26 rvwx weewx[1590] ERROR weewx.restx: MQTT: Unexpected exception of type <class 'KeyError'>
Aug 21 15:28:26 rvwx weewx[1590] ERROR weewx.restx: *** Traceback (most recent call last):
Aug 21 15:28:26 rvwx weewx[1590] ERROR weewx.restx: ***   File "/usr/share/weewx/weewx/restx.py", line 382, in run_loop
Aug 21 15:28:26 rvwx weewx[1590] ERROR weewx.restx: ***     self.process_record(_record, dbmanager)
Aug 21 15:28:26 rvwx weewx[1590] ERROR weewx.restx: ***   File "/usr/share/weewx/user/mqtt.py", line 516, in process_record
Aug 21 15:28:26 rvwx weewx[1590] ERROR weewx.restx: ***     data = self.filter_data(record)
Aug 21 15:28:26 rvwx weewx[1590] ERROR weewx.restx: ***   File "/usr/share/weewx/user/mqtt.py", line 496, in filter_data
Aug 21 15:28:26 rvwx weewx[1590] ERROR weewx.restx: ***     v = weewx.units.convert(from_t, to_units)[0]
Aug 21 15:28:26 rvwx weewx[1590] ERROR weewx.restx: ***   File "/usr/share/weewx/weewx/units.py", line 1454, in convert
Aug 21 15:28:26 rvwx weewx[1590] ERROR weewx.restx: ***     conversion_func = conversionDict[val_t[1]][target_unit]
Aug 21 15:28:26 rvwx weewx[1590] ERROR weewx.restx: *** KeyError: None
Aug 21 15:28:26 rvwx weewx[1590] CRITICAL weewx.restx: MQTT: Thread terminating. Reason: None

Here is the relevant section from weewx.conf to display coordinates in belchertown skin:
     [[MQTT]]
        server_url = mqtt://zzzz:yy...@wxvm.bz3gfkrlqtrsc3sv.myfritz.net:1883/
        topic = RV
        unit_system = METRIC
        binding = loop, archive
        log_success = true
       [[[inputs]]]
            [[[[latitude]]]]
#             units = " °"
             format = %.3f

and MQTT subscribe to Write the GPS data received via MQTT into the weewx db:
[MQTTSubscribeService]
    # This section is for the MQTTSubscribe service.

    # Turn the service on and off.
    # Default is: true
    # Only used by the service.
    enable = true
    log = true

    # The MQTT server.
    # Default is localhost.
    host = "wxvm.bz3gfkrlqtrsc3sv.myfritz.net"

    # The port to connect to.
    # Default is 1883.
    port = 1883

    # Maximum period in seconds allowed between communications with the broker.
    # Default is 60.
    keepalive = 60

    # username for broker authentication.
    # Default is None.
    username = xxxx

    # password for broker authentication.
    # Default is None.
    password = yyyy

    # The binding, loop or archive.
    # Default is: loop
    # Only used by the service.
    binding = loop

    # The message handler to use
    [[message_callback]]
        # The format of the MQTT payload.
        # Currently support: individual, json, keyword
        # Must be specified.
        type = json

    # The topics to subscribe to.
    [[topics]]
        # Units for MQTT payloads without unit value.
        # Valid values: US, METRIC, METRICWX
        # Default is: US
        unit_system = US
#        adjust_start_time = 1
        use_server_datetime = True
        use_topic_as_fieldname = true

        [[[RVGPS/#]]]
            # The WeeWX name.
            # Default is the name from MQTT.
            #name = Latitude

            # True if the incoming data should not be processed into WeeWX.
            # Valid values: True, False
            # Default is False
            ignore = False

            # True if the incoming data is cumulative.
            # Valid values: True, False
            # Default is False
#            contains_total = False

            # The conversion type necessary for WeeWX compatibility
            # Valid values: bool, float, int, none
            # Default is float
#            conversion_type = int

            # The units of the incoming data.
            # Useful if this field's units differ from the topic's unit_system's units.
            # Valid values: see, http://www.weewx.com/docs/customizing.htm#units
            # Default is not set
#            units = degree_compass  

Screenshot 2024-08-21 at 15.40.43.png

This is how it Looks on belchertown skin with units = None, e.g. no change of units intented.

TIA,
Stefan

Greg Troxel

unread,
Aug 21, 2024, 10:05:08 AM8/21/24
to Stefan Gliessmann, weewx-user
I suspect your issue is that degrees as a unit is interpreted as
temperature.
lat/lon degrees are different, and also different from compass
direction.

Probably you need to define a new unit with the necessary functions, so
that trying to look up a function and invoking it does not lead to a
"None is not a function" exception. That will require reading and
understanding the code.

Karen K

unread,
Aug 21, 2024, 10:13:51 AM8/21/24
to weewx-user
Each observation type needs an assignment to a unit group. So if you define a new observation type you have to write somewhere:

weewx.units.obs_group_dict.setdefault('your_observation_type','group_something')


The unit group must exist. group_direction is an example of a unit group that is connected to the unit degree_compass

Please also not the name of the unit degree_compass, not otherwise round.

If you want to define a new unit group, say group_coordinate, you have to write:

for group in weewx.units.std_groups:

    weewx.units.std_groups[group].setdefault('group_coordinate','degree_compass')


You can include that code in user/extensions.py.

Stefan Gliessmann

unread,
Sep 13, 2024, 8:59:09 AM9/13/24
to weewx-user
I have now made These changes in the Extensions.py file.
I now gut the unit correct to °, but have only integer values and no digits after the decimal.
For the direction of a compass that is perfectly fine. For GPS coordinate, no digits after the decimal is pretty useless for displaying purposes.

As you can see, I tried to play arround with '%.3f', but that caused more errors and weewx to exit ... 

So, how to I format the GPS coordinates with three digits after the decimal, but keep the compass direction as an integer?

#
#    Copyright (c) 2009-2015 Tom Keffer <tke...@gmail.com>
#
#    See the file LICENSE.txt for your full rights.
#

"""User extensions module

This module is imported from the main executable, so anything put here will be
executed before anything else happens. This makes it a good place to put user
extensions.
"""

import locale
import weewx.units

# This sets the locale for all categories to the user’s default setting (typically specified in the
# LANG environment variable). See: https://docs.python.org/3/library/locale.html#locale.setlocale
locale.setlocale(locale.LC_ALL, '')

import weewx.units ## works, but has no digits after the decimal

for group in weewx.units.std_groups:
    weewx.units.std_groups[group].setdefault('group_coordinate','degree_compass')
weewx.units.obs_group_dict.setdefault('latitude','group_coordinate')
weewx.units.obs_group_dict.setdefault('longitude','group_coordinate')

# Create a custom formatter for the latitude and longitude observations
#formatter = weewx.units.Formatter({'latitude': '%.3f', 'longitude': '%.3f'})

# Set the formatter for the latitude and longitude observations
#weewx.units.obs_group_dict['latitude'] = ('degree_latitude', 'group_coordinate', formatter)
#weewx.units.obs_group_dict['longitude'] = ('degree_longitude', 'group_coordinate', formatter)


Karen K

unread,
Sep 14, 2024, 9:04:06 AM9/14/24
to weewx-user
You can configure the amount of decimal digits in skin.conf in section [Units], sub-section [[StringFormats]]. Add a line naming the unit and format string like this: degree_compass = %.3f.

Stefan Gliessmann

unread,
Sep 14, 2024, 10:33:18 AM9/14/24
to weewx...@googlegroups.com
Thank you!

That worked perfectly :)

There is already a line with degree_compass = %.0f 
which I modified accordingly.

And it did not change the digits for e.g. wind direction :)

--
You received this message because you are subscribed to a topic in the Google Groups "weewx-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/weewx-user/XlCrP5mkG_A/unsubscribe.
To unsubscribe from this group and all its topics, send an email to weewx-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/weewx-user/ebb2e7c1-a818-4e3a-8809-93be15fe5185n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages