Syntax to filter modbus sensors

I receive obvious errors in modbus data; I would like to filter (probably an outlier filter) - I can’t figure out where to add the filter details.

Other people have posted logs that show the extraneous values are related to modbus transmission, not HA itself.


configuration.yaml (relavant excerpt):

modbus:
  - name: "EPEVER"
    type: rtuovertcp
    host: 192.168.4.100
    port: 502
    delay: 0
    message_wait_milliseconds: 30
    timeout: 5
    sensors:
      - name: "Solar Energy (Today)"
        unique_id: gate_power_solar_energy_today
        unit_of_measurement: WH
        state_class: measurement
        scale: 10
        precision: 0
        min_value: 0
        max_value: 2000
        slave: 1
        input_type: input
        data_type: int16 # read one register only
        address: 13068 # 0x330C/D
      - name: "Battery Temperature"
        unique_id: gate_power_battery_temperature
        unit_of_measurement: "℃"
        state_class: measurement
        scale: 0.01
        precision: 2
        slave: 1
        input_type: input
        address: 12560 # 0x3110

I’m discarding the high range register in the Solar Energy filter - my setup can’t produce that much power.

So here’s the solution(s) for anyone who stumbles across this:

Filters cannot be added directly to the modbus entries, the filters have to be templated seperately. An easier solution is there appears to be modbus filtering included when the device_class field is added to the modbus sensor definition.

- name: "EPEVER"
  type: rtuovertcp
  host: 192.168.4.100
  port: 502
  delay: 0
  message_wait_milliseconds: 30
  timeout: 5
  sensors:
    - name: "Battery Temperature"
      unique_id: gate_power_battery_temperature
      unit_of_measurement: "℃"
      state_class: measurement
      scale: 0.01
      precision: 2
      slave: 1
      input_type: input
      address: 12560 # 0x3110
    - name: "Battery Temperature (Raw)"
      unique_id: gate_power_battery_temperature_raw
      unit_of_measurement: "℃"
      state_class: measurement
      device_class: temperature
      scale: 0.01
      precision: 2
      slave: 1
      input_type: input
      address: 12560 # 0x3110

Here is an excerpt from templates.yaml that demonstrates template filtering. This code doesn’t work as written because the “last” state is unknown when HA starts; I need to manually set the last state within the range of the current temperaute before it works (you could add logic to create a base case, but it is easier to add device_class as above and not use the template filtering:

- sensor:
  - name: "Battery Temperature (Filtered)"
    unique_id: gate_power_battery_temperature_filtered
    unit_of_measurement: "℃"
    state_class: measurement
    device_class: temperature
    state: >
      {% set current = states('sensor.battery_temperature_raw') | float(0) %}
      {% set last = states('sensor.battery_temperature_filtered') | float(0) %}
      {% if (current - last) | abs < 2 %}
        {{ current }}
      {% else %}
        {{ last }}
      {% endif %}
    availability: "{{ states('sensor.battery_temperature_raw') not in ['unknown', 'unavailable'] }}"