Unavailable sensor still displayed on a graph

There is a sensor for an air quality monitor.
It has a “battery_level” attribute.
This sensor is called sensor.xiaomi_cg_2.

There is a template sensor for this attribute:

sensor:
  - platform: template
    sensors:
      battery_cleargrass_2:
        unit_of_measurement: '%'
        device_class: battery

        value_template: "{% if states('sensor.xiaomi_cg_2' ) in ['unavailable','unknown'] -%}
                         {{ 'unavailable' }}
                         {%- else -%}
                         {{ state_attr('sensor.xiaomi_cg_2','battery_level') }}
                         {%- endif %}"

Since the device is always powered on, it’s battery_level = 100%.
Now let’s switch this device off.
The source sensor becomes unavailable, so does the template sensor:
image

Then let’s have a look at the graph for the template sensor:
image
The graph’s code:

  - type: history-graph
    entities:
      - entity: sensor.battery_cleargrass_2
    hours_to_show: 1
    refresh_interval: 0

Why the graph still displays the curve at 100% ???
I expected some gaps, actually…

Same with a custom:mini-graph-card:
image

  - type: custom:mini-graph-card
    entities:
      - entity: sensor.battery_cleargrass_2
        show_points: true
    lower_bound: 0
    points_per_hour: 60
    hours_to_show: 2
    average_func: last
    update_interval: 0

Just in case - I added this for the sensor:

        availability_template: "{{ not states('sensor.xiaomi_cg_2') in ['unavailable','unknown'] }}"

No changes.

BTW, I think that this availability_template is not required since my sensor is anyway assigned to unavailable under corresponding conditions in accordance to the "value_template" expression, am I right?


After 1 hour both graphs show this:


No curves are displayed.
But if the "hours_to_show" is changed to “2” then again the graphs show “100%” with no gaps.
Why does it happen?

1 Like