For my irrigation control I want to predict the possible rainfall from two sensors, one always gives a a chance of rain in % the other the amount in mm. Multiplying these two together is sufficient for my needs, however it is complicated by the fact that the amount is sometimes given as a range, e.g. “3 to 5”.
I’ve come up with this sensor that works for about 90% of cases where the amount is given as single digit ranges:
- platform: template
sensors:
predicted_rain:
friendly_name: 'Predicted Rain'
entity_id:
- sensor.bom_hobart_chance_of_rain_0 # always a single int
- sensor.bom_hobart_possible_rainfall_0 # two formats. Single int or "int_1 to int_2" e.g. "3 to 4"
unit_of_measurement: mm
value_template: >
{% set chance = states('sensor.bom_hobart_chance_of_rain_0')|int / 100 %}
{% if 'to' in states('sensor.bom_hobart_possible_rainfall_0') %}
{% set amount = states('sensor.bom_hobart_possible_rainfall_1') %}
{% set amount_avg = (amount[0:1]|int + amount[5:]|int )/2 %}
{% else %}
{% set amount_avg = states('sensor.bom_hobart_possible_rainfall_0') | int %}
{% endif %}
{{ chance * amount_avg }}
Is it possible to alter this to encompass all possible values of n and m in “n to m” , not just n and m < 10?
{% if 'to' in states('sensor.bom_gosford_possible_rainfall_2') %}
{% set var = states('sensor.bom_gosford_possible_rainfall_2').split(' to ') %}
{% set var1 = var[0] | float %}
{% set var2 = var[1] | float %}
{% set mean = (var1 + var2) / 2 %}
{{ mean }}
{% else %}
{{ var }}
{% endif %}
Beat me to the post. I was busy cooking up something that turned out to be very similar to your solution.
The principal difference is that I don’t check if the value contains to and simply split it regardless. I then check the resulting list’s length to determine how it should be handled.
value_template: >
{% set chance = states('sensor.bom_hobart_chance_of_rain_0')|int / 100 %}
{% set x = (states('sensor.bom_hobart_possible_rainfall_1')).split(' to ') %}
{% set amount = x[0]|int if x|length == 1 else (x[0]|int + x[1]|int)/2 %}
{{ chance * amount }}
- platform: template
sensors:
predict_rain_tomorrow:
friendly_name: 'Predicted Rain Tomorrow'
entity_id:
- sensor.bom_hobart_chance_of_rain_1 # always a single int
- sensor.bom_hobart_possible_rainfall_1 # two formats. Single int or "int_1 to int_2" e.g. "3 to 4"
unit_of_measurement: mm
value_template: >
{% set chance = states('sensor.bom_hobart_chance_of_rain_1')|int / 100 %}
{% if 'to' in states('sensor.bom_hobart_possible_rainfall_1') %}
{% set amount = states('sensor.bom_hobart_possible_rainfall_1').split(' to ') %}
{% set amount1 = amount[0] | float %}
{% set amount2 = amount[1] | float %}
{% set amount_avg = (amount1 + amount2) / 2 %}
{% else %}
{% set amount_avg = states('sensor.bom_hobart_possible_rainfall_1') | int %}
{% endif %}
{{ chance * amount_avg }}