Issue with sensor template - fritzbox

Hey, someone might have a tipp for me? I try to measure my upload, therfore the firtzbox component provides me with an attribute which I templated into a sensor.

  value_template: '{{ states.sensor.fritz_netmonitor.attributes.transmission_rate_up | filesizeformat () | replace(" kB", "")  }}'
  unit_of_measurement: "kB"

So far this is working. The problem is, when my fritzbox reconnects it sometimes shows weird data i.e. “-21232323533” which totaly throw of my graph.


Is there a way just to ignore all negative values all together? my understanding seems to be good enough to figure it out.


There might be better ways to ultimately address the root cause, but one thing you can do is to change any negative value to, say, zero:

  value_template: >
    {% set rate = state_attr('sensor.fritz_netmonitor', 'transmission_rate_up')|int(0) %}
    {% if rate < 0 %}
    {% else %}
      {{ (rate / 1000) | round  }}
    {% endif %}

FYI, by using the state_attr() function, and the int() filter with a default value, if the state is ever None, or something completely unexpected, you won’t get an error, but rather simply the value zero. Also, your template wouldn’t work too well with numbers that were above or below kB range (e.g., zero, which would return “0 Bytes”), so I took the liberty of dealing with that, too.

You might also want to check out the Filter Sensor if you haven’t already.

1 Like

Thanks I’ll give it a try. Looks good and works as expected :slight_smile: . Regarding the root cause, I have no idea how to debug this since the value is coming directly from the fritzbox.

What I didn’t understand yet, how does this code work? Is everything prefixed with the {% interpreted as python code? I have a hard time finding the proper documentation for this…

Nevermind, if finally found the reference to Jinja2. Thanks again for pointing me there.

1 Like