I have an RF weather sensor that gives me cumulative rainfall via MQTT since it was powered on.
I want to see the rainfall since midnight, so I use the following to store the rain to midnight yesterday as a reference value in mqtt (to ensure it survives any HASS restarts etc):
sensor:
# Rain is cumulative forever - collect it and subtract yesterday for todays rain
- platform: mqtt
state_topic: 'RF/DKW2012-ID=004c'
name: 'rain_cum'
icon: mdi:weather-rainy
unit_of_measurement: 'mm'
value_template: '{{ value_json.RAIN }}'
- platform: mqtt
state_topic: 'ha/cum_rain_prior'
name: 'rain_cum_prior'
unit_of_measurement: 'mm'
value_template: '{{ value_json.day_1 }}'
- platform: template
sensors:
rain:
value_template: '{%- if not (is_state("sensor.rain_cum","unknown") or is_state("sensor.rain_cum_prior","unknown") )-%} {{ ((states.sensor.rain_cum.state | float) - (states.sensor.rain_cum_prior.state | float)) | max (0) | round(1) }} {%- endif -%}' ## ensure calc is no lower than zero!
friendly_name: 'Rain Today'
unit_of_measurement: 'mm'
Plus an automation to roll over the daily value after midnight:
Gives me cumulative rain for the day ending midnight like so:
The templating took a bit of trial and error to ensure missing values were excluded from the maths … otherwise you can get negative rain at some points in the day if mqtt glitches!
Hi Phil.
I have the same protocol weather station it seems, as my RFlink hub detects it as DKW2012. The actual station physically is a Fine Offset WH2081 system though.
A question or two.
What model station do you have, and was MQTT native or something you added to it ?
The rain total data from the outdoor sensor on the WH2081 is an 8bit value from 0h to ffh on my station (I logged the data and checked that by manually tipping the bucket sensor) and it recycles to 0h after 76.8mm of rain (each bucket step = 0.3mm). The receiver console seems to check for 8bit rollover and increment the difference when that occurs and so keeps track of the accumulated total…
Have you got the same situation with your station rain register ?
Cheers,
Martin
Yep I’m in Oz as well, Adelaide actually.
OK I’ll look into the esp8266 serial idea, seems like a good one and makes it more like the Sonoff RF Bridge. I have the Sonoff bridge too, but it doesn’t decode the weather station even with Espurna firmware, and of late it started to go nuts and trigger my 433Mhz gear randomly, so it’s been shutdown for now.
BTW, have you seen the high nibble of the rain counter ever increment ?
Just curious if that does it on yours, it should do it every 76.8mm of rainfall if that is an extension of the main rain counter byte.
Rain is a distant memory here in Adelaide since about last October really. Crazy wet over in the East though…
I did a bit of work on the WH2081 station tonight to try solve a few loose ends about the RF protocol. I had a low battery warning on the Fine Offset LCD console for some time (and the battery was two years old), but showing Batt OK on the HA GUI and RFlink debug. I logged the RF debug then changed the TX batteries and then captured a new log, sent off both to RFlink dev. The battery status cleared on the console of course, HA remained batt OK. I think the RFlink FW is reading the wrong status bit.
At the same time the total rain data from the TX cleared to 0h, both the high nibble and the main byte zeroed. I tipped the bucket twice and now the main byte shows 2h. So I’m fairly sure I was wrong and there is a high nibble for rain (so ranges to FFFh). No matter really, if you compare rain data every day unless you get 1228.5mm rain in that day and even that value rolls over
PS. I moved my rain sensor box down onto the main roof because of the shaking pole issue…
Any ideas if this would be usable for a sensor that’s not reporting by MQTT?
I’ve connected a Oregon PCR800 raingauge to my tellstick and I’m getting all the values to my home-assistant sensor sensor-rain_rateand sensor.rain_total. The total just keeps increasing and I want it to reset daily as yours do!
I haven’t found a nice neat understandable way yet either. Mine reports via the RFlink component in similar way to yours. I’d be happy with a way to reset a daily total at 9AM every day the same way the weather bureau does.
On the surface it seems easy, read and store the rain_total at 9AM, then displays the difference value increase during the day as a new entity, reset again next day at 9AM. The trick seems to be the way to create a ‘variable’ for the script to hold that 9AM capture…
The first one (rain_cum) is from an MQTT rain sensor. That could be any sensor that holds cumulative values - change to the sensor you have.
The second one (rain_cum_prior) is the snapshot “variable” that we update manually in the automation. Even if your system does not use MQTT sensors, I would recommend using this as an MQTT sensor as it will store the value outside of HASS, so it will transcend restarts of HASS and remember the prior value. I am using mosquitto on the same pi as my HASS and this works fine for me, YMMV.
The third variable is just the delta between the other two - a common template sensor
So, @mr.sneezy, I suggest you just change your automation to take the snapshot at 0900 every day rather than midnight.
Thanks phileep I might have enough understanding of HA config and MQTT to make sense now.
I must be blind though, I followed links back to your Git but did not see the yaml’s. Can you point me at them when you have time.
The yaml you quoted tells HA where to look for the cumulative prior data - which is in MQTT at the topic ha/cum_rain_prior (you can name the topic whatever you want), I happen to prefix my homeassistant mqtt with ha (think of it kind of like a directory structure)
The “magic” is actually in the automation that runs at midnight (in my case). This publishes to the topic you are using as a sensor with the cumulative value at a point in time. Then the template sensor can observe the difference between the 2 to calculate period rainfall. When you publish to an MQTT topic, it overwrites any previous value that was there for exactly the same topic - so it is like a variable holder. By making the value in the MQTT topic retained, it will survive homeassistant reboots and whenever a device subscribes to this topic, mqtt will give it the last value.
You will need MQTT enabled to use this method (if you do not already have it enabled) - but it is baked in to homeassistant these days, so simply enabling it in your configuration.yaml should be enough - it is very a lightweight and straightforward protocol.
For first use, the cumulative sensor will not be populated - so either do this manually from hass or command line … or the easy option is to manually trigger the automation to do the cumulative update. This would make prior and current cumulative the same, so expect 0 rainfall until the bucket gets more through it!
Hi phileep.
I’m back onto tinkering with my HA setup again and a daily rain counter.
I’ve tried hard tonight to get the MQTT based daily rain sensor configurations to work but not had success yet.
After some fine tuning I got the config and automations yaml’s to pass a user initiated configuration validation test. However when attempting a HA restart to try the new stuff I get another validation error warning on the Home screen.
The error log seems to hold this info:
Log Details (ERROR)
Fri May 18 2018 21:58:51 GMT+0930 (Cen. Australia Standard Time)
starting version 3.2.4
Testing configuration at /config
ERROR:homeassistant.scripts.check_config:BURB
Traceback (most recent call last):
File “/usr/lib/python3.6/site-packages/homeassistant/scripts/check_config.py”, line 207, in check
res[‘components’] = check_ha_config_file(hass)
File “/usr/lib/python3.6/site-packages/homeassistant/scripts/check_config.py”, line 328, in check_ha_config_file
del core_config[CONF_PACKAGES]
KeyError: ‘packages’
Fatal error while loading config: ‘packages’
Failed config
General Errors:
- ‘packages’
Successful config (partial)
My cumulative rain sensor come via the RFlink component not via MQTT and the sensor ID is sensor.dkw2012_00f7_raintot
This is my configuration block:
- platform: mqtt
state_topic: 'ha/cum_rain_prior'
name: 'rain_cum_prior'
unit_of_measurement: 'mm'
value_template: '{{ value_json.day_1 }}'
- platform: template
sensors:
rain:
value_template: '{%- if not (is_state("sensor.dkw2012_00f7_raintot","unknown") or is_state("sensor.rain_cum_prior","unknown") )-%} {{ ((states.sensor.dkw2012_00f7_raintot.state | float) - (states.sensor.rain_cum_prior.state | float)) | max (0) | round(1) }} {%- endif -%}' ## ensure calc is no lower than zero!
friendly_name: 'Rain Today'
unit_of_measurement: 'mm'