HowTo: Create binary sensor that change state based on a counter value

I am reading the interface RX packets counter from my router via SNMP. I was thinking of creating a binary sensor that is ON when this counter (which is read every 10 seconds) is continuing to increment and set to OFF when the counter value is not incrementing. I have a script that is pinging via this interface every 3 seconds so if there are no incoming packets then the interface can be considered as DOWN.
Can anyone point me to how I can accomplish this? Thanks.

Depending on the automation trigger used, you can access the previous state value. Then you just need a condition to check whether the current value is bigger than the last.

I tried this but getting an error with the condition.

template:
  trigger:
    - platform: state
      entity_id: sensor.failover_wan_packets_in
  condition: '{{ trigger.to_state.state != trigger.from_state.state }}'
  binary_sensor:
    - name: Failover WAN State
      auto_off: 60
      state: "on"

Invalid config for [template]: [condition] is an invalid option for [template]. Check: template->condition. (See /config/configuration.yaml, line 1).

Yeah, so that error is telling you template sensors can’t have a condition. Just put your condition in the state where you have 'on' (it accepts a template).

This did the trick:
state: "{{ trigger.to_state.state != trigger.from_state.state }}"

1 Like