Using Trend gradient for forecasting & identifying rate of lake level change

I’m using the “Trend” addin to analyze data that I collect on a lake level. The data is published by a government agency and I read it into HA every 15 minutes. I read the time of the sample and the sample. I keep the last 48 hours worth of data for the Trend calculation. The lake level is a value in metres above sea level for the surface of the lake. Thus for every reading I have a date/time and a number of metres.

Utilizing this information I display a number of values such as level last night at midnight, current level, how much of the beach is exposed (extrapolated from the lake level), highest and lowest values this year, etc. The Trend add-in also provides the gradient which provides an indication of the rate of drop or rise. In my case that gradient is extremely small as this is a big lake and changes to the level happen gradually. Most of the data ends up in the following template:

####################################################
#
#####   SENSORS FOR DATA FOR LAKE LEVEL CALCULATIONS
#
####################################################
#  MAIN LAKE LEVEL AND BEACH TEMPLATE
  - trigger:
      - platform: time_pattern
        minutes: "/1"
    sensor:
      - name: Shuswap Lake and Beach Data
        unit_of_measurement: "m"
        state: "{{(states('sensor.shuswap_lake_crescent_bay_depth') | float(0) | round(2))}}"
        attributes:
          salmon_arm_lake_depth: "{{states('sensor.shuswap_lake_crescent_bay_depth')}}"
          shuswap_lake_crescent_bay_beach: "{{(((((states('sensor.shuswap_lake_crescent_bay_depth') | float (0)) * -583.53) + 203268)/100) | round(2))}}"
          shuswap_lake_depth_reading_date: "{{states('sensor.shuswap_lake_crescent_bay_reading_date')}}"        
          shuswap_lake_level_delta_to_high_water_level: "{{((states('sensor.shuswap_lake_crescent_bay_depth')|float(0))-348.342)|round(2)}}"
          shuswap_lake_lowest_level_this_year_date: "{% if ((states('sensor.shuswap_lake_crescent_bay_depth')) | float(0)) 
               < (state_attr('sensor.shuswap_lake_and_beach_data','shuswap_lake_lowest_level_this_year') |float(0)) %}  
               {{ now() }}
               {%else%}
               {{state_attr('sensor.shuswap_lake_and_beach_data','shuswap_lake_lowest_level_this_year_date')}}
               {% endif %}"

#          shuswap_lake_lowest_level_this_year: 9999
          shuswap_lake_lowest_level_this_year: "{% if ((states('sensor.shuswap_lake_crescent_bay_depth') | float(0))) 
               < (state_attr('sensor.shuswap_lake_and_beach_data','shuswap_lake_lowest_level_this_year') | float(0)) %}  
               {{states('sensor.shuswap_lake_crescent_bay_depth') | round(2)}}
               {% else %}
               {{state_attr('sensor.shuswap_lake_and_beach_data','shuswap_lake_lowest_level_this_year') | float(0)}}
               {% endif %}"

          shuswap_lake_highest_level_this_year_date: "{% if ((states('sensor.shuswap_lake_crescent_bay_depth')) | float(0)) 
               > (state_attr('sensor.shuswap_lake_and_beach_data','shuswap_lake_highest_level_this_year') |float(0)) %}  
               {{now()}}
               {%else%}
               {{state_attr('sensor.shuswap_lake_and_beach_data','shuswap_lake_highest_level_this_year_date')}}
               {% endif %}"                
          shuswap_lake_highest_level_this_year:  "{% if ((states('sensor.shuswap_lake_crescent_bay_depth') | float(0))) 
               > (state_attr('sensor.shuswap_lake_and_beach_data','shuswap_lake_highest_level_this_year') | float(0)) %}  
               {{states('sensor.shuswap_lake_crescent_bay_depth') | round(2)}}
               {% else %}
               {{state_attr('sensor.shuswap_lake_and_beach_data','shuswap_lake_highest_level_this_year') | float(0)}}
               {% endif %}"
          shuswap_lake_level_change_mm_per_day: "{{(state_attr('binary_sensor.shuswap_lake_level_dropping_trend','gradient') | float(0)) * 86400000.0}}"
          shuswap_lake_lowest_level_last_year: 0
          shuswap_lake_highest_level_last_year: 0

##       
#

What I am trying to do is use the gradient to calculate the rate at which the water rises or falls. Because it is slow, I am looking for a value in mm/day

I am calculating this as state_attr(‘binary_sensor.shuswap_lake_level_dropping_trend’,‘gradient’) | float(0)) * 86,400,000.0

If I understand the documentation correctly, the gradient should be showing me metres per second (the data I read is in metres and I believe the sensor uses seconds as a basis). I believe HA calculates the Gradient as change (metres) / seconds (using the beginning and end points of the line created by Trend.

I got the 86,400,000 by (60 seconds in a minute * 60 minutes in an hour * 24 hours in a day * 1,000 mm in a metre).

I multiply the 86.4 million by the gradient to get mm/day. This seems to give me numbers that I could believe in (but I’ve only done limited testing so far as I am at the mercy of the lake changing levels :slight_smile: )

Am I approaching this correctly?

If this is correct, I could then use this value to forecast lake levels a few days out - but the real value here is in just knowing how fast the lake is rising or falling.

Looking for thoughts on this approach and my calculation.

thanks