Plotting a non-continuous history graph: do not plot when condition

I am trying to plot a water temperature graph only when my heating is on. Not knowing my way very well, I asked ChatGPT for help and now have:

{% if is_state('switch.shelly1_b9cddb', 'on') %}
  {{ states('sensor.shellyplus1_441793cd2ba8_temperature') }}
{% else %}
  null
{% endif %}

Two problems:

  • the “null” value is seen as a string and generates errors;
  • it doesn’t do what I want:
    image

“Heating out temperature” (purple) is the source sensor and “Heating out cond” (orange) is the one from the above template sensor. Ideally, I would like those horizontal and vertical lines not to be plotted.

Is that possible?

Please don’t use Chat GPT, it frequently outputs nonsense.

You want something like this:

template:
  - sensor:
      - name: Water Temperature
        device_class: temperature
        state_class: measurement
        unit_of_measurement: "°C"
        state: "{{ states('sensor.shellyplus1_441793cd2ba8_temperature') }}"
        availability: "{{ is_state('switch.shelly1_b9cddb', 'on') }}"

This will set the template sensor state to unavailable if your switch is off, producing a gap in your graph.

EDIT: re-reading your post I think I got the wrong sensor, but you should get the idea. Use an availability template.

Thank you for your proposed solution. It looks good.

I had used a template helper to implement my version. It seems that the availability parameter is not available in the UI. I need to move (delete and create) it to configuration.yaml, right?

I did so and it plots as expected. Thanks!

My final result is as follows, just in case I am doing something suboptimal.

template:
  - sensor:
      - name: Heating in cond
        unique_id: sensor.heating_in_cond
        device_class: temperature
        state_class: measurement
        unit_of_measurement: "°C"
        state: "{{ states('sensor.shellyplus1_441793cd2ba8_temperature') }}"
        availability: "{{ is_state('switch.shelly1_b9cddb', 'on') }}"
      - name: "Filtered heating in cond"
        unique_id: sensor.filtered_heating_in_cond
        unit_of_measurement: "°C"
        device_class: temperature
        state_class: measurement ### only include this line if you want long term statistics. 
        state: "{{ states('sensor.heating_in_cond') if states('sensor.heating_in_cond')|is_number else null }}"        
        # ...repeat for "out" sensor...
sensor:
  - platform: statistics
    name: "heating in cond mean"
    entity_id: sensor.filtered_heating_in_cond
    state_characteristic: mean
    max_age:
      hours: 12

I took inspiration from your answer in the following post for the statistics part: How can avoid "unavailable" or "unknown" state in statistics template?. The aim being to take the average of the plotted values.

This causes errors in my logs. Can we avoid them?

Logger: homeassistant.components.sensor
Source: helpers/entity_platform.py:609
integration: Sensor (documentation, issues)
First occurred: 21:08:47 (2 occurrences)
Last logged: 21:08:47

Error adding entity sensor.filtered_heating_in_cond for domain sensor with platform template
Error adding entity sensor.filtered_heating_out_cond for domain sensor with platform template
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 635, in state
    numerical_value = int(value)
ValueError: invalid literal for int() with base 10: ''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 638, in state
    numerical_value = float(value)
ValueError: could not convert string to float: ''

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 609, in _async_add_entities
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 928, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1383, in add_to_platform_finish
    await self.async_added_to_hass()
  File "/usr/src/homeassistant/homeassistant/components/template/template_entity.py", line 578, in async_added_to_hass
    async_at_start(self.hass, self._async_template_startup)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/start.py", line 71, in async_at_start
    return _async_at_core_state(
        hass, at_start_cb, EVENT_HOMEASSISTANT_START, _is_running
    )
  File "/usr/src/homeassistant/homeassistant/helpers/start.py", line 36, in _async_at_core_state
    hass.async_run_hass_job(at_start_job, hass)
    ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 940, in async_run_hass_job
    hassjob.target(*args)
    ~~~~~~~~~~~~~~^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/template/template_entity.py", line 517, in _async_template_startup
    result_info.async_refresh()
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 1126, in async_refresh
    self._refresh(None)
    ~~~~~~~~~~~~~^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 1312, in _refresh
    self.hass.async_run_hass_job(self._job, event, updates)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 940, in async_run_hass_job
    hassjob.target(*args)
    ~~~~~~~~~~~~~~^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/template/template_entity.py", line 463, in _handle_results
    self.async_write_ha_state()
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1023, in async_write_ha_state
    self._async_write_ha_state()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1148, in _async_write_ha_state
    self.__async_calculate_state()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1085, in __async_calculate_state
    state = self._stringify_state(available)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1029, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 642, in state
    raise ValueError(
    ...<5 lines>...
    ) from err
ValueError: Sensor sensor.filtered_heating_in_cond has device class 'temperature', state class 'measurement' unit '°C' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: '' (<class 'str'>)
Logger: homeassistant.helpers.template
Source: helpers/template.py:2769
First occurred: 20:32:30 (9 occurrences)
Last logged: 21:08:47

Template variable warning: 'null' is undefined when rendering '{{ states('sensor.heating_in_cond') if states('sensor.heating_in_cond')|is_number else null }}'
Template variable warning: 'null' is undefined when rendering '{{ states('sensor.heating_out_cond') if states('sensor.heating_out_cond')|is_number else null }}'

Use the availability template for this instead of the “else null”:

e.g.

      - name: "Filtered heating in cond"
        unique_id: sensor.filtered_heating_in_cond
        unit_of_measurement: "°C"
        device_class: temperature
        state_class: measurement ### only include this line if you want long term statistics. 
        state: "{{ states('sensor.heating_in_cond') }}"        
        availability: "{{ states('sensor.heating_in_cond')|is_number }}"

Same goes for the un-shown heating out sensor.

1 Like

Thanks. I thought it was solved, but it seems I have another dependency. I thought it was the statistics that I have on it but commenting that out doesn’t remove the message. Or was a fast reload not enough?

sensor:
  - platform: statistics
    name: "heating in cond mean"
    entity_id: sensor.filtered_heating_in_cond
    state_characteristic: mean
    max_age:
      hours: 24

Here is the error I get in the log:

2025-02-19 10:19:01.066 ERROR (MainThread) [homeassistant.helpers.event] Error while dispatching event for switch.shelly1_b9cddb to <Job track state_changed event {'sensor.shellyplus1_441793cd2ba8_temperature', 'switch.shelly1_b9cddb'} HassJobType.Callback <bound method TrackTemplateResultInfo._refresh of <TrackTemplateResultInfo {Template<template=({{ is_state('switch.shelly1_b9cddb', 'on') }}) renders=134>: <RenderInfo Template<template=({{ is_state('switch.shelly1_b9cddb', 'on') }}) renders=134> all_states=False all_states_lifecycle=False domains=frozenset() domains_lifecycle=frozenset() entities=frozenset({'switch.shelly1_b9cddb'}) rate_limit=None has_time=False exception=None is_static=False>, Template<template=({{ states('sensor.shellyplus1_441793cd2ba8_temperature') }}) renders=5416>: <RenderInfo Template<template=({{ states('sensor.shellyplus1_441793cd2ba8_temperature') }}) renders=5416> all_states=False all_states_lifecycle=False domains=frozenset() domains_lifecycle=frozenset() entities=frozenset({'sensor.shellyplus1_441793cd2ba8_temperature'}) rate_limit=None has_time=False exception=None is_static=False>}>>>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 640, in state
    numerical_value = float(value)  # type:ignore[arg-type]
ValueError: could not convert string to float: 'unavailable'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 355, in _async_dispatch_entity_id_event
    hass.async_run_hass_job(job, event)
    ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 940, in async_run_hass_job
    hassjob.target(*args)
    ~~~~~~~~~~~~~~^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 1312, in _refresh
    self.hass.async_run_hass_job(self._job, event, updates)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 940, in async_run_hass_job
    hassjob.target(*args)
    ~~~~~~~~~~~~~~^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/template/template_entity.py", line 463, in _handle_results
    self.async_write_ha_state()
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1023, in async_write_ha_state
    self._async_write_ha_state()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1148, in _async_write_ha_state
    self.__async_calculate_state()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1085, in __async_calculate_state
    state = self._stringify_state(available)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1029, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 642, in state
    raise ValueError(
    ...<5 lines>...
    ) from err
ValueError: Sensor sensor.heating_in_cond has device class 'temperature', state class 'measurement' unit '°C' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'unavailable' (<class 'str'>)