MQTT Rain fall total

@kaijk
I have an issue I can’t seam to overcome. I have a Davis Vantage Vue. I am pulling data in from WeeWx via GitHub - michael-slx/weewx-mqtt: WeeWX extension for publishing data to an MQTT broker I am not having issues with getting the data into Home Assistant. I have one issue I have solved with and input_number helper but it won’t plot correctly. My advice was to do it with only a sensor.

Here is the issues. I need either a total of the tips or total rainfall to use the utility_meter and get things to work correctly with Plotly.

- name: "Davis Rain Tips Counter"
      unique_id: uniqueid_weather_rain_count
      state_topic: "weather/rainCount_count"
      value_template: "{{ value | float(0) }}"
      unit_of_measurement: "tips"
      state_class: total_increasing

This publish a one then goes away. I can seam to get it to sum each time it publishes. I followed up with another sensor

template:
  - sensor:
      - name: "Davis Rain Inches"
        unique_id: "weather_sensor_davis_rain_inches"
        device_class: precipitation
        state_class: total_increasing
        unit_of_measurement: "in"
        state: "{{ states('sensor.davis_rain_tips_counter') | float(0.0) * 0.01 }}"

This pulses the value of 0.01 and the value stays at zero after the publish of a tip.

I also have another mqtt sensor that gives me the same response as the above. It pulses .01 then goes away. I started using this one, but again cant get it to total with a sensor.

- name: "Davis Rain"
      unique_id: uniqueid_weather_rain_inch
      state_topic: "weather/rain_inch"
      value_template: "{{ value|round(2) }}"
      unit_of_measurement: "inch"
      qos: 1

I could use some help. I got this working from start to here never doing MQTT before.

Need total rainfall
hourly
daily
monthly
yearly

(everything seems to hinge on this totaling the .01 pulses i get in HA from MQTT)

Have you tried for sensor.weather_sensor_davis_rain_inches

state: "{{states('sensor.weather_sensor_davis_rain_inches') | float(0.0) + states('sensor.davis_rain_tips_counter') | float(0.0) * 0.01 }}"

Be sure to add an availability test as well to keep from trashing the rain_inches accumulator sensor in unexpected conditions:

availability: "{{ is_number( states('sensor.weather_sensor_davis_rain_inches') ) }}"

The MQTT sensor is a pulse sensor, notwithstanding the state_class: total_increasing.

Call it good that it goes to zero after a pulse…

If this works, throw sensor.weather_sensor_davis_rain_inches into several utility_meters to get your periodic totals.

I cant try the suggestion until i have rain. I have now way to trigger or publish the rain inches and tips simultaneously. Here is the idea i came up with this morning .
This is the MQTT sensor that brings in the value from WeeWx

  - name: "Davis Rain Tips Counter"
      unique_id: uniqueid_weather_rain_count
      state_topic: "weather/rainCount_count"
      value_template: "{{ value | float(0.0) }}"
      unit_of_measurement: "tips"
      state_class: total_increasing
     # availability: "{{ is_number( states('sensor.davis_rain_tips_counter) ) }}"

It doesn’t like the availability
Then I use a history sensor to record the counts.

  - platform: history_stats
    name: Davis Rain Tips Count
    entity_id: sensor.davis_rain_tips_counter
    state: "1.0"
    type: count
    start: "{{ 0 }}"
    end: "{{ now() }}"

Then i convert to inches in a template

template:
  - sensor:
      - name: "Davis Rain Inches Total"
        unique_id: "sensor_davis_rain_inches_total"
        unit_of_measurement: "in"
        state_class: total_increasing
        device_class: precipitation
        state: "{{ states('sensor.davis_rain_tips_count') | float(0) * 0.01 }}"
        #availability: "{{ is_number( states('sensor.davis_rain_inches_total') ) }}"

This works up until this point then the utility meter show unavailable. Maybe the history count is not supported in this feature.?

utility_meter:
  davis_rain_daily_inches:
    source: sensor.davis_rain_inches_total
    cycle: daily
  davis_rain_monthly_inches:
    source: sensor.davis_rain_inches_total
    cycle: monthly

Made some progress. It seams like Home Assistant template sensors do not automatically accumulate values between state updates. Each time the sensor’s state updates, it recalculates the entire state based on the current inputs

You might want state_class: total. See .here.

Also, here.

For my now-retired acurite rain gauge. I had to perturb the value to get it to accumulate by adding a small random lambda. Otherwise, it saw each successive new value as not-additive, but the same reading. This is a command_line sensor:

- sensor:
    name: 'Acurite 10-Min Rainfall'
    command: "[[ $((`date +%s` - `date +%s -r '/config/externaldata/acuriteweatherdata.txt'`)) -le 60 ]] && cat /config/externaldata/acuriteweatherdata.txt || echo '0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.0' "
    # Add a small lambda to try to keep successive values unique so the Utility Meter will add each value to the sum.
    value_template:  >-
      {% set val = ( value.split( ',' )[14] | float(0.0) ) | round(2) %}
      {% if val == 0.0 %}
         0.0
      {% else %}
         {{ ( val  + ( [ 0.0, -0.000001, 0.000001, -0.000002, 0.000002, -0.000003, 0.000003, -0.000004, 0.000004, -0.000005, 0.000005, -0.000006, 0.000006, -0.000007, 0.000007, -0.000008, 0.000008, -0.000009, 0.000009, -0.000010, 0.000010,-0.000011, 0.000011, -0.000012, 0.000012, -0.000013, 0.000013, -0.000014, 0.000014, -0.000015, 0.000015  ] | random ) ) | round(6)  }}
      {% endif %}
    unit_of_measurement: 'in'

I was able to use the history sensor to count the tips and then make a template sensor which worked with the utility meter. All is well thanks for the help.