Hey y’all, I’ve created a custom sensor to track the predicted evaporation rate for my pool. Somehow, it all works. I want to use the tracking over time to figure out my average evaporation rate each day, but I turn off my pump overnight, and the water temperature sensor (sensor.pool_temp) starts returning “Unknown”. It’s a 30k gallon pool, so it’s a bit of a thermal battery and doesn’t change that much over night…how can I use the last measured value of pool_temp to continue logging projections until the sensor comes back online in the morning?
template:
- sensor:
- name: "Predicted Evaporation"
icon: mdi:pool
unit_of_measurement: "g/day"
state: >
{% if states('sensor.pool_temp') not in ['unkown', 'unavailable'] %}
{% set waterTempF = states('sensor.pool_temp') | float(0) %}
{% set waterTempC = (waterTempF -32) * 5/9 %}
{% set vaporpressure = 0.61121*(e**(((18.678-(waterTempC/234.5))*(waterTempC/(257.14+waterTempC)))))*7.50062 %}
{% set poolsqft = 32*18 %}
{% set windspeed = states('sensor.openweathermap_forecast_wind_speed') | float(0) %}
{% set airTempF = states('sensor.air_temp') | float(0) %}
{% set predictedEvap = ((7.4* vaporpressure*poolsqft)*((0.477*windspeed)**0.78))/(airTempF+459.67) %}
{{ predictedEvap|round(2)}}
{% else %}
{{ this.state }}
{% endif %}
You might also like to add an availability template to ensure all the other sensors you are using are available. Then if one of them goes off-line (e.g. your outdoor temp) it will not mess up your calculations:
template:
- sensor:
- name: "Predicted Evaporation"
icon: mdi:pool
unit_of_measurement: "g/day"
state: >
{% if states('sensor.pool_temp') not in ['unkown', 'unavailable'] %}
{% set waterTempF = states('sensor.pool_temp') | float(0) %}
{% set waterTempC = (waterTempF -32) * 5/9 %}
{% set vaporpressure = 0.61121*(e**(((18.678-(waterTempC/234.5))*(waterTempC/(257.14+waterTempC)))))*7.50062 %}
{% set poolsqft = 32*18 %}
{% set windspeed = states('sensor.openweathermap_forecast_wind_speed') | float(0) %}
{% set airTempF = states('sensor.air_temp') | float(0) %}
{% set predictedEvap = ((7.4* vaporpressure*poolsqft)*((0.477*windspeed)**0.78))/(airTempF+459.67) %}
{{ predictedEvap|round(2)}}
{% else %}
{{ this.state }}
{% endif %}
availability: >
{{ states('sensor.pool_temp')|is_number and
states('sensor.openweathermap_forecast_wind_speed')|is_number and
states('sensor.air_temp')|is_number) }}
That actually helped me understand a lot of other things about this custom template formatting that I think I was missing.
I like the availability template, but what if I still wanted to calculate a datapoint based on each sensor that was returning a current value and the last-known-good value of any sensor that was in an unknown or unavailable state? Do I need to create individual sensors and use the “else this.state” construct?
And reading your question again I see that my template retains the last predicted evaporation state which isn’t exactly what you asked for (use the last pool temp). So to get what you want you need to apply this to the pool temp source sensor (at least), not the overall template.