DavidFW1960 / bom-weather-card

Custom Animated Weather Card for any weather provider
Apache License 2.0
159 stars 37 forks source link

Home Assistant Log error for Heat Index Template sensor #114

Open simonjowett opened 9 months ago

simonjowett commented 9 months ago

Hi David

I've been running through H.A and trying to resolve some log errors and come up on this one related to the Heat Index templates (when it reports 'n/a').

ValueError: Sensor sensor.heatindex has device class 'temperature', state class 'None' unit '°C' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'n/a' (<class 'str'>

And also this one which is a related error:

homeassistant.exceptions.TemplateError: TypeError: '>=' not supported between instances of 'str' and 'int'

The setting of the heatindex to n/a seems incompatible with the '>=' comparitors. Maybe either setting a numeric default or skipping over the function with a "If has_value" might stop these errors?

# Heatindex
# https://en.wikipedia.org/wiki/Heat_index
      heatindex:
        unit_of_measurement: °C
        device_class: temperature
        value_template: >
            {%- if states('sensor.nowra_temp') | float(default=0) > 27 and states('sensor.nowra_humidity') | float(default=0) > 40 -%}
            {% set T = states('sensor.nowra_temp') | float(default='n/a') %}
            {% set R = states('sensor.nowra_humidity') | float(default='n/a') %}
            {% set c1 = -8.78469475556 %}
            {% set c2 = 1.61139411 %}
            {% set c3 = 2.33854883889 %}
            {% set c4 = -0.14611605 %}
            {% set c5 = -0.012308094 %}
            {% set c6 = -0.0164248277778 %}
            {% set c7 = 0.002211732 %}
            {% set c8 = 0.00072546 %}
            {% set c9 = -0.000003582 %}
            {% set HI = c1 + (c2 * T ) + (c3 * R) + ( c4 * T * R ) + ( c5 * T**2 ) + ( c6 * R**2 ) + ( c7 * T**2 * R ) + ( c8 * T * R**2 ) + ( c9 * T**2 * R**2 ) %} 
            {{ HI | round }}
            {%- else -%}
            n/a
            {%- endif -%}
      heatindexrating:
        value_template: >
            {%- if states('sensor.heatindex') == 'n/a' -%}
            Out of range
            {%- elif states('sensor.heatindex') | float(default='n/a')  >= 54 -%}
            Extreme danger: heat stroke imminent
            {%- elif states('sensor.heatindex') | float(default='n/a')  >= 41 -%}
            Danger: cramps, exhaustion heat stroke probable
            {%- elif states('sensor.heatindex') | float(default='n/a')  >= 32 -%}
            Extreme caution: cramps and exhaustion possible
            {%- elif states('sensor.heatindex') | float(default='n/a')  >= 26 -%}
            Caution: fatigue possible
            {%- else -%}
            Normal
            {%- endif -%}
DavidFW1960 commented 9 months ago

Yeah I have been seeing this for a while and have been ignoring it. Feel free to make a PR or suggest code to change it

simonjowett commented 9 months ago

I was finding it difficult to see the wood for the trees in the error log so I went looking for solutions and have managed to cull them back a bit! It seems a recent HA update has caused a number of template sensor issues and this you-tuber provided a couple of solutions. https://www.youtube.com/watch?v=7349L0CFi-M&t=713s

My crude way is to set a dummy numeric value of 0. This seems to work to get rid of the error but leaves the heat index as =0 when it would have been 'n/a'.

Also, doing this I don't think it would ever hit the 'Normal' tag... but then whats the difference between Normal and n/a!

# Heatindex
# https://en.wikipedia.org/wiki/Heat_index
      heatindex:
        unit_of_measurement: °C
        device_class: temperature
        value_template: >
            {%- if states('sensor.nowra_temp') | float(default=0) > 27 and states('sensor.nowra_humidity') | float(default=0) > 40 -%}
            {% set T = states('sensor.nowra_temp') | float(default='n/a') %}
            {% set R = states('sensor.nowra_humidity') | float(default='n/a') %}
            {% set c1 = -8.78469475556 %}
            {% set c2 = 1.61139411 %}
            {% set c3 = 2.33854883889 %}
            {% set c4 = -0.14611605 %}
            {% set c5 = -0.012308094 %}
            {% set c6 = -0.0164248277778 %}
            {% set c7 = 0.002211732 %}
            {% set c8 = 0.00072546 %}
            {% set c9 = -0.000003582 %}
            {% set HI = c1 + (c2 * T ) + (c3 * R) + ( c4 * T * R ) + ( c5 * T**2 ) + ( c6 * R**2 ) + ( c7 * T**2 * R ) + ( c8 * T * R**2 ) + ( c9 * T**2 * R**2 ) %} 
            {{ HI | round }}
            {%- else -%}
            0
            {%- endif -%}
      heatindexrating:
        value_template: >
            {%- if states('sensor.heatindex') == '0' -%}
            Out of range
            {%- elif states('sensor.heatindex') | float(default='n/a')  >= 54 -%}
            Extreme danger: heat stroke imminent
            {%- elif states('sensor.heatindex') | float(default='n/a')  >= 41 -%}
            Danger: cramps, exhaustion heat stroke probable
            {%- elif states('sensor.heatindex') | float(default='n/a')  >= 32 -%}
            Extreme caution: cramps and exhaustion possible
            {%- elif states('sensor.heatindex') | float(default='n/a')  >= 26 -%}
            Caution: fatigue possible
            {%- else -%}
            Normal
            {%- endif -%}