MQTT Subscribe. How to obtain the sum of the values that arrive by MQTT?

152 views
Skip to first unread message

enu...@gmail.com

unread,
Oct 17, 2020, 8:06:08 AM10/17/20
to weewx-user

I use contains_total=true but I get the average of the values obtained. I need the sum.

The log:

Oct 17 13:58:58 rpi weewx[522]: (Service) user.MQTTSubscribe: TopicManager data-> outgoing ESP_Energy_Meter_01/kwh: dateTime: 1602935896.48, kwh: 0.001, usUnits: 17
Oct 17 13:58:58 rpi weewx[522]: (Service) user.MQTTSubscribe: TopicManager data-> outgoing ESP_Energy_Meter_01/kwh: dateTime: 1602935913.83, kwh: 0.003, usUnits: 17
Oct 17 13:58:58 rpi weewx[522]: (Service) user.MQTTSubscribe: TopicManager data-> outgoing ESP_Energy_Meter_01/kwh: dateTime: 1602935931.37, kwh: 0.026, usUnits: 17
Oct 17 13:58:58 rpi weewx[522]: (Service) user.MQTTSubscribe: TopicManager data-> outgoing accumulated ESP_Energy_Meter_01/kwh: dateTime: 1602935934.0, kwh: 0.01, usUnits: 16
Here there are 3 values 0.001, 0.003 and 0.026. I get 0.01 which is the average. I need the sum of the three 0.001+0.003`0.0026 which is 0.03.
The conf:
[MQTTSubscribeService]
    enable = true
    console = True
    payload_type = json
    host = 192.168.1.3    
    port = 1883
    keepalive = 60
    binding = loop
        [[message_callback]]
        type = individual
    [[topics]]
        unit_system = METRICWX
        use_topic_as_fieldname = true
        [[[esp/ds18b20/radiation]]]
            name = radiation
        [[[esp/ds18b20/extraTemp1]]]
            name = extraTemp1
        [[[ESP_Energy_Meter_01/watt]]]
            name = watt
        [[[ESP_Energy_Meter_01/kwh]]]
            contains_total = True
            conversion_type = float
            name = kwh

bell...@gmail.com

unread,
Oct 17, 2020, 7:12:05 PM10/17/20
to weewx-user
tldr:
Try the following.
Remove the contains_total and if the [Accumulator] section does not exist, add the following. If it exists, just add the [[kwh]] subsection.  Note that [Accumulator] is a ‘ top level’ section.
[Accumulator]
  [[kwh]]
    extractor = sum

Background:
When running as a service, MQTTSubscribe has no control over the arrival of the MQTT data nor the loop interval. It leverages WeeWX accumulator functionality to manage when multiple MQTT payloads have arrived since the last loop packet generation. The default accumulator extractor is average, the configuration setting above will override this, setting it to sum.
The idea is to treat the loop packet like a ‘mini archive’. I haven’t fully vetted this, so I will be very  interested in how it goes.
rich

ps. I think you should be able to remove the conversion_type because the default is float.
rich


enu...@gmail.com

unread,
Oct 19, 2020, 4:01:39 PM10/19/20
to weewx-user
It works now. Thank you very much.  I don't know the [Accumulator] section of weewx.conf.
And at the loop packet level? I want to tell my loop packet is generated every 48s and the archive period is 5 min. How do the same at this level or is auto to select sum in the accumulator?
Reply all
Reply to author
Forward
0 new messages