Here is the full automation, but it’s part of a larger setup. Check my repo for the latest configs/code.
- alias: "Monitor Inbound Internet Traffic"
trigger:
platform: state
entity_id: sensor.snmp_wan_in
action:
- service: input_number.set_value
data:
entity_id: input_number.wan_traffic_delta_in
value: >-
{# safe delta catering for wrap-around of a 32-bit unsigned int (snmp counter is 32-bit unsigned int) #}
{# basically taking 2's complement #}
{% set from = trigger.from_state.state | int %}
{% set to = trigger.to_state.state | int %}
{% set traffic_delta = (to - from) if (to >= from) else (4294967295 - from + to + 1) %}
{% set time_delta = as_timestamp(trigger.to_state.last_updated) - as_timestamp(trigger.from_state.last_updated) %}
{{ (traffic_delta * 8) / time_delta }}