Integrating a filter into configuration.yaml renders all sensors unavailable

So I have a bunch of tmeplate senors defined in my configuration.yaml and now wanted to define a filter. I looked up the documentation of the filter Integration, copy-pasted a filter definition to the end of my config file and adapted it to my needs.

Even though no error message is displayed in the YAML editor, after reloading the YAML configs, not only does the filter not work (no new entitiy shows up) but also all the sensor I had working before now show up as “unavailable”.

This is my full config file after adding the snipplet:

default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

template:
  - sensor:
      - name: "Technical & Garden | Power"
        unique_id: technical_garden_power
        unit_of_measurement: W
        icon: mdi:lightning-bolt
        availability: >-
          {{ has_value('sensor.shellyproem50_08f9e0e5c190_em0_power') and
          has_value('sensor.shellyproem50_08f9e0e5c190_em1_power') }}
        state: >-
          {{ (states('sensor.shellyproem50_08f9e0e5c190_em0_power') | float(0) -
          states('sensor.shellyproem50_08f9e0e5c190_em1_power') | float(0) ) | round(1) }}

      - name: "Pellet heating status"
        unique_id: pellet_heating_status
        availability: "{{ has_value('sensor.shellypro4pm_08f9e0e9ed64_switch_2_power') }}"
        state: >-
          {% set power = states('sensor.shellypro4pm_08f9e0e9ed64_switch_2_power') | float(0) %} 
          {% if  power < 3 %}
            off
          {% elif power < 20 %}
            stand by
          {% elif power < 140 %}
            circulating
          {% elif power < 270 %}
            heating
          {% elif power > 270 %}
            ignition
          {% endif %}

      - name: "Electric boiler status"
        unique_id: electric_boiler_status
        availability: >-
            {{ has_value('sensor.shellypro4pm_08f9e0e9ed64_switch_3_power') and
            has_value('switch.shellypro4pm_08f9e0e9ed64_switch_3') }}
        state: >-
            {{ 'off' if is_state('switch.shellypro4pm_08f9e0e9ed64_switch_3', 'off')
            else 'standby' if states('sensor.shellypro4pm_08f9e0e9ed64_switch_3_power')|int(0) < 20
            else 'heating' }}

      - name: "Wind Direction TXT"
        unique_id: wind_direction_txt
        availability: "{{ has_value('sensor.ecowitt_wind_direction') }}"
        state: >-
          {% set dir = states('sensor.ecowitt_wind_direction') | int(0)  %}
          {% set navDir = [ 'N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N' ] %}
          {{ navDir[ (dir/22.5) | int(0) ] }

sensor:
  - platform: filter
    name: "filtered heating power"
    entity_id: sensor.shellypro4pm_08f9e0e9ed64_switch_2_power
    filters:
      - filter: time_simple_moving_average
        window_size: "00:05"
        precision: 2

Also in notifications I get a Log message saying:


Logger: homeassistant.config
Source: config.py:592
First occurred: October 29, 2024 at 01:43:04 (13 occurrences)
Last logged: 15:27:43

* Invalid config for 'template' at configuration.yaml, line 55: invalid template (TemplateSyntaxError: unexpected '}') for dictionary value 'sensor->3->state', got "{% set dir = states('sensor.ecowitt_wind_direction') | int(0) %} {% set navDir = [ 'N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N' ] %} {{ navDir[ (dir/22.5) | int(0) ] }"
* Invalid config for 'template' at configuration.yaml, line 60: 'platform' is an invalid option for 'template', check: sensor->4->platform Invalid config for 'template' at configuration.yaml, line 60: required key 'state' not provided Invalid config for 'template' at configuration.yaml, line 62: 'entity_id' is an invalid option for 'template', check: sensor->4->entity_id Invalid config for 'template' at configuration.yaml, line 64: 'filters' is an invalid option for 'template', check: sensor->4->filters
* Invalid config for 'template' at configuration.yaml, line 64: invalid template (TemplateSyntaxError: unexpected '}') for dictionary value 'sensor->3->state', got "{% set dir = states('sensor.ecowitt_wind_direction') | int(0) %} {% set navDir = [ 'N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N' ] %} {{ navDir[ (dir/22.5) | int(0) ] }"

What a I doning wrong here?

Thank you all in advance!

You miss a } here.
The forum setting everything red after it is good hint.

1 Like

Ah, yes indeed. Thank you so much for spotting that! I also wondered why the forum was throwing all red but didn’t know what to make of it. Kinda absurd, that the YAML editor provides less help in finding errors then the auto-formatting in the forum!

After reloading all yamls configs I now have my template sensors working again like before, but “filtered heating power” is not showing up anywhere (also not when searching for the entity). Where would i have to look for that?

I also added a unique_id, hoping that might make it searchable, but no luck:

default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

template:
  - sensor:
      - name: "Technical & Garden | Power"
        unique_id: technical_garden_power
        unit_of_measurement: W
        icon: mdi:lightning-bolt
        availability: >-
          {{ has_value('sensor.shellyproem50_08f9e0e5c190_em0_power') and
          has_value('sensor.shellyproem50_08f9e0e5c190_em1_power') }}
        state: >-
          {{ (states('sensor.shellyproem50_08f9e0e5c190_em0_power') | float(0) -
          states('sensor.shellyproem50_08f9e0e5c190_em1_power') | float(0) ) | round(1) }}

      - name: "Pellet heating status"
        unique_id: pellet_heating_status
        availability: "{{ has_value('sensor.shellypro4pm_08f9e0e9ed64_switch_2_power') }}"
        state: >-
          {% set power = states('sensor.shellypro4pm_08f9e0e9ed64_switch_2_power') | float(0) %} 
          {% if  power < 3 %}
            off
          {% elif power < 20 %}
            stand by
          {% elif power < 140 %}
            circulating
          {% elif power < 270 %}
            heating
          {% elif power > 270 %}
            ignition
          {% endif %}

      - name: "Electric boiler status"
        unique_id: electric_boiler_status
        availability: >-
            {{ has_value('sensor.shellypro4pm_08f9e0e9ed64_switch_3_power') and
            has_value('switch.shellypro4pm_08f9e0e9ed64_switch_3') }}
        state: >-
            {{ 'off' if is_state('switch.shellypro4pm_08f9e0e9ed64_switch_3', 'off')
            else 'standby' if states('sensor.shellypro4pm_08f9e0e9ed64_switch_3_power')|int(0) < 20
            else 'heating' }}

      - name: "Wind Direction TXT"
        unique_id: wind_direction_txt
        availability: "{{ has_value('sensor.ecowitt_wind_direction') }}"
        state: >-
          {% set dir = states('sensor.ecowitt_wind_direction') | int(0)  %}
          {% set navDir = [ 'N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N' ] %}
          {{ navDir[ (dir/22.5) | int(0) ] }}

sensor:
  - platform: filter
    name: "filtered heating power"
    unique_id: filtered_heating_power
    entity_id: sensor.shellypro4pm_08f9e0e9ed64_switch_2_power
    filters:
      - filter: time_simple_moving_average
        window_size: "00:05"
        precision: 2

Did you wait at least 5 minutes?

It took about 20 minutes of waiting and two HA restarts for the entity to show up. No idea why, but now all is good & I just have to tweak the settings for my filter to do what i want it to do.

Thank you so much!